User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use fast\Random;
  6. use think\Config;
  7. use think\Validate;
  8. use think\Db;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Apic
  13. {
  14. protected $noNeedLogin = ['accountlogin','resetpwd'];
  15. protected $noNeedRight = '*';
  16. //员工手机+密码登录
  17. public function accountlogin(){
  18. $mobile = $this->request->post('mobile');
  19. $password = $this->request->post('password');
  20. if (!$mobile || !$password) {
  21. $this->error(__('Invalid parameters'));
  22. }
  23. $ret = $this->auth->login($mobile, $password);
  24. if ($ret) {
  25. $data = $this->auth->getUserinfo();
  26. $this->success(__('Logged in successful'), $data);
  27. } else {
  28. $this->error($this->auth->getError());
  29. }
  30. }
  31. /**
  32. * 退出登录
  33. * @ApiMethod (POST)
  34. */
  35. public function logout()
  36. {
  37. if (!$this->request->isPost()) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $this->auth->logout();
  41. $this->success(__('Logout successful'));
  42. }
  43. //用户详细资料
  44. public function getUserinfo($type = 1){
  45. $info = $this->auth->getUserinfo();
  46. if($type == 'return'){
  47. return $info;
  48. }
  49. $this->success(__('success'),$info);
  50. }
  51. /**
  52. * 重置密码
  53. *
  54. * @ApiMethod (POST)
  55. * @param string $mobile 手机号
  56. * @param string $captcha 验证码
  57. * @param string $newpassword 新密码
  58. */
  59. public function resetpwd()
  60. {
  61. $mobile = $this->request->post('mobile');
  62. $captcha = $this->request->post('captcha');
  63. $newpassword = $this->request->post("newpassword");
  64. if (!$mobile || !$captcha || !$newpassword) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. //验证Token
  68. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  69. $this->error(__('Password must be 6 to 30 characters'));
  70. }
  71. if (!Validate::regex($mobile, "^1\d{10}$")) {
  72. $this->error(__('Mobile is incorrect'));
  73. }
  74. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  75. if (!$user) {
  76. $this->error(__('User not found'));
  77. }
  78. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  79. if (!$ret) {
  80. $this->error(__('Captcha is incorrect'));
  81. }
  82. Sms::flush($mobile, 'resetpwd');
  83. //模拟一次登录
  84. $this->auth->direct($user->id);
  85. $ret = $this->auth->resetpwd($newpassword, '', true);
  86. if ($ret) {
  87. $this->success(__('Reset password successful'));
  88. } else {
  89. $this->error($this->auth->getError());
  90. }
  91. }
  92. /**
  93. * 修改会员个人信息
  94. *
  95. * @ApiMethod (POST)
  96. * @param string $avatar 头像地址
  97. * @param string $username 用户名
  98. * @param string $nickname 昵称
  99. * @param string $bio 个人简介
  100. */
  101. public function profile()
  102. {
  103. $field = [
  104. 'mobile',
  105. 'image',
  106. 'is_open',
  107. 'open_hours',
  108. ];
  109. $data = request_post_hub($field);
  110. $data['updatetime'] = time();
  111. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  112. $this->success('资料更新完成');
  113. }
  114. /**
  115. * 设置店铺地址
  116. */
  117. public function setaddress()
  118. {
  119. $field = [
  120. 'province_name',
  121. 'city_name',
  122. 'area_name',
  123. 'province_id',
  124. 'city_id',
  125. 'area_id',
  126. 'address',
  127. ];
  128. $data = request_post_hub($field);
  129. $data['full_address'] = $data['province_name'].$data['city_name'].$data['area_name'].$data['address'];
  130. $data['updatetime'] = time();
  131. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  132. $this->success('资料更新完成');
  133. }
  134. }