WebPerson.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. */
  29. public function login()
  30. {
  31. if ($this->request->isPost()) {
  32. $account = $this->request->post('account');
  33. $code=$this->request->post('code');
  34. $rule = [
  35. 'account' => 'require|length:3,50',
  36. ];
  37. $msg = [
  38. 'account.require' => '账户不能为空',
  39. 'account.length' => '账户必须3-50个字符',
  40. ];
  41. $data = [
  42. 'account' => $account,
  43. ];
  44. $validate = new Validate($rule, $msg);
  45. $result = $validate->check($data);
  46. if (!$result) {
  47. $this->error(__($validate->getError()));
  48. }
  49. if (!Sms::check($account, $code, 'mobilelogin')) {
  50. $this->error(__('验证码不正确'));
  51. }
  52. $info = $this->model->where(['account' => $account])->find();
  53. if (!$info) {
  54. $data = [
  55. 'nickname' => '',
  56. 'password' => '',
  57. 'email' => '',
  58. 'account' => $account];
  59. $data['salt'] = Random::alnum();
  60. $data['password'] = md5(md5($data['password']) . $data['salt']);
  61. if ($customerId = Contacts::where(['mobile' => $data['account']])->value('customer_id')) {
  62. $data['customer_id'] = $customerId;
  63. } else {
  64. $this->error('账号不存在');
  65. }
  66. $this->model->allowField(true)->save($data);
  67. $info = $this->model->where(['account' => $account])->find();
  68. }
  69. $token = md5('person'.$info['id'] . rand(1000, 9999));
  70. if (Token::set($token,$info['id'], $this->keeptime)) {
  71. $this->success(__('登录成功'), ['token' => $token]);
  72. }
  73. $this->error('登录失败');
  74. }
  75. }
  76. /**
  77. * 退出登录
  78. */
  79. public function logout()
  80. {
  81. $token = input('token');
  82. //删除Token
  83. Token::delete($token);
  84. $this->success(__('Logout successful'));
  85. }
  86. // 企业信息
  87. public function companyInfo(){
  88. $info = Admin::get(1);
  89. $info->avatar = cdnurl($info->avatar,true);
  90. $this->success('',$info);
  91. }
  92. //直接体验
  93. public function logintest()
  94. {
  95. $info = $this->model->where([])->order('id asc')->find();
  96. if (!$info) {
  97. $contacts = Contacts::where([])->order('id asc')->find();
  98. $data = [
  99. 'nickname' => '',
  100. 'password' => '',
  101. 'email' => '',
  102. 'account' => $contacts['mobile']];
  103. $data['salt'] = Random::alnum();
  104. $data['password'] = md5(md5($data['password']) . $data['salt']);
  105. $data['customer_id'] = $contacts['customer_id'];
  106. $this->model->allowField(true)->save($data);
  107. $info = $this->model->where(['account' => $contacts['mobile']])->find();
  108. }
  109. $token = md5('person'.$info['id'] . rand(1000, 9999));
  110. if (Token::set($token,$info['id'], $this->keeptime)) {
  111. $this->success(__('登录成功'), ['token' => $token]);
  112. }
  113. $this->error('登录失败');
  114. }
  115. }