WebPerson.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Contacts;
  4. use app\admin\model\Admin;
  5. use app\common\library\Sms;
  6. use app\common\library\Token;
  7. use fast\Random;
  8. use think\Validate;
  9. /**
  10. * 用户中心
  11. */
  12. class WebPerson extends WebIndexApi
  13. {
  14. protected $layout = 'default';
  15. protected $noNeedLogin = ['login', 'register', 'third','logintest'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * @var \addons\qingdongams\model\Person
  19. */
  20. protected $model;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new \addons\qingdongams\model\Person();
  25. }
  26. /**
  27. * 获取密码加密后的字符串
  28. * @param string $password 密码
  29. * @param string $salt 密码盐
  30. * @return string
  31. */
  32. public function getEncryptPassword($password, $salt = '') {
  33. return md5(md5($password) . $salt);
  34. }
  35. /**
  36. * 会员登录
  37. */
  38. public function login()
  39. {
  40. if ($this->request->isPost()) {
  41. $account = $this->request->post('account');
  42. $code=$this->request->post('code');
  43. $password=$this->request->post('password');
  44. $rule = [
  45. 'account' => 'require|length:3,50',
  46. 'password' => 'require|length:3,50',
  47. ];
  48. $msg = [
  49. 'account.require' => '账户不能为空',
  50. 'account.length' => '账户必须3-50个字符',
  51. 'password.require' => '密码不能为空',
  52. ];
  53. $data = [
  54. 'account' => $account,
  55. 'password' => $password,
  56. ];
  57. $validate = new Validate($rule, $msg);
  58. $result = $validate->check($data);
  59. if (!$result) {
  60. $this->error(__($validate->getError()));
  61. }
  62. // if (!Sms::check($account, $code, 'mobilelogin')) {
  63. // $this->error(__('验证码不正确'));
  64. // }
  65. $info = $this->model->where(['account' => $account])->find();
  66. if(!$info){
  67. $this->error('账号不存在');
  68. return false;
  69. }
  70. if ($info->password != $this->getEncryptPassword($password, $info->salt)) {
  71. $this->error('密码错误');
  72. return false;
  73. }
  74. if (!$info) {
  75. $data = [
  76. 'nickname' => '',
  77. 'password' => '',
  78. 'email' => '',
  79. 'account' => $account];
  80. $data['salt'] = Random::alnum();
  81. $data['password'] = md5(md5($data['password']) . $data['salt']);
  82. if ($customerId = Contacts::where(['mobile' => $data['account']])->value('customer_id')) {
  83. $data['customer_id'] = $customerId;
  84. } else {
  85. $this->error('账号不存在');
  86. }
  87. $this->model->allowField(true)->save($data);
  88. $info = $this->model->where(['account' => $account])->find();
  89. }
  90. $token = md5('person'.$info['id'] . rand(1000, 9999));
  91. if (Token::set($token,$info['id'], $this->keeptime)) {
  92. $this->success(__('登录成功'), ['token' => $token]);
  93. }
  94. $this->error('登录失败');
  95. }
  96. }
  97. /**
  98. * 退出登录
  99. */
  100. public function logout()
  101. {
  102. $token = input('token');
  103. //删除Token
  104. Token::delete($token);
  105. $this->success(__('Logout successful'));
  106. }
  107. // 企业信息
  108. public function companyInfo(){
  109. $info = Admin::get(1);
  110. $info->avatar = cdnurl($info->avatar,true);
  111. $this->success('',$info);
  112. }
  113. //直接体验
  114. public function logintest()
  115. {
  116. $info = $this->model->where([])->order('id asc')->find();
  117. if (!$info) {
  118. $contacts = Contacts::where([])->order('id asc')->find();
  119. $data = [
  120. 'nickname' => '',
  121. 'password' => '',
  122. 'email' => '',
  123. 'account' => $contacts['mobile']];
  124. $data['salt'] = Random::alnum();
  125. $data['password'] = md5(md5($data['password']) . $data['salt']);
  126. $data['customer_id'] = $contacts['customer_id'];
  127. $this->model->allowField(true)->save($data);
  128. $info = $this->model->where(['account' => $contacts['mobile']])->find();
  129. }
  130. $token = md5('person'.$info['id'] . rand(1000, 9999));
  131. if (Token::set($token,$info['id'], $this->keeptime)) {
  132. $this->success(__('登录成功'), ['token' => $token]);
  133. }
  134. $this->error('登录失败');
  135. }
  136. }