User.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller\coach;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use think\Exception;
  6. use think\Validate;
  7. use think\Db;
  8. /**
  9. * 会员接口
  10. */
  11. class User extends Apic
  12. {
  13. protected $noNeedLogin = ['login','resetpwd'];
  14. protected $noNeedRight = '*';
  15. //员工手机+密码登录
  16. public function login()
  17. {
  18. $mobile = input('mobile');
  19. $password = input('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. *
  34. * @ApiMethod (POST)
  35. * @param string $newpassword 新密码
  36. * @param string $oldpassword 旧密码
  37. */
  38. public function changepwd(){
  39. $newpassword = input('newpassword');
  40. $oldpassword = input('oldpassword','');
  41. if (!$newpassword) {
  42. $this->error('请输入新密码');
  43. }
  44. if($this->auth->password && empty($oldpassword)){
  45. $this->error('旧密码必填');
  46. }
  47. if(empty($this->auth->password)){
  48. $ret = $this->auth->changepwd($newpassword, '', true);
  49. }else{
  50. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  51. }
  52. if ($ret) {
  53. $this->success(__('Reset password successful'));
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 退出登录
  60. * @ApiMethod (POST)
  61. */
  62. public function logout()
  63. {
  64. if (!$this->request->isPost()) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. $this->auth->logout();
  68. $this->success(__('Logout successful'));
  69. }
  70. //用户详细资料
  71. public function getUserinfo($type = 1){
  72. $info = $this->auth->getUserinfo();
  73. if($type == 'return'){
  74. return $info;
  75. }
  76. $this->success(__('success'),$info);
  77. }
  78. /**
  79. * 重置密码
  80. *
  81. * @ApiMethod (POST)
  82. * @param string $mobile 手机号
  83. * @param string $captcha 验证码
  84. * @param string $newpassword 新密码
  85. */
  86. /*public function resetpwd()
  87. {
  88. $mobile = $this->request->post('mobile');
  89. $captcha = $this->request->post('captcha');
  90. $newpassword = $this->request->post("newpassword");
  91. if (!$mobile || !$captcha || !$newpassword) {
  92. $this->error(__('Invalid parameters'));
  93. }
  94. //验证Token
  95. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  96. $this->error(__('Password must be 6 to 30 characters'));
  97. }
  98. if (!Validate::regex($mobile, "^1\d{10}$")) {
  99. $this->error(__('Mobile is incorrect'));
  100. }
  101. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  102. if (!$user) {
  103. $this->error(__('User not found'));
  104. }
  105. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  106. if (!$ret) {
  107. $this->error(__('Captcha is incorrect'));
  108. }
  109. Sms::flush($mobile, 'resetpwd');
  110. //模拟一次登录
  111. $this->auth->direct($user->id);
  112. $ret = $this->auth->resetpwd($newpassword, '', true);
  113. if ($ret) {
  114. $this->success(__('Reset password successful'));
  115. } else {
  116. $this->error($this->auth->getError());
  117. }
  118. }*/
  119. /**
  120. * 修改会员个人信息
  121. *
  122. * @ApiMethod (POST)
  123. * @param string $avatar 头像地址
  124. * @param string $username 用户名
  125. * @param string $nickname 昵称
  126. * @param string $bio 个人简介
  127. */
  128. public function profile()
  129. {
  130. $field = [
  131. 'mobile',
  132. //'email',
  133. 'avatar',
  134. 'firstname',
  135. 'lastname',
  136. 'lang',
  137. ];
  138. $data = request_post_hub($field);
  139. /*if(isset($data['email'])){
  140. $check_email = Db::name('coach')->where('email',$data['email'])->where('id','neq',$this->auth->id)->find();
  141. if($check_email){
  142. $this->error('邮箱已被其他人使用');
  143. }
  144. }*/
  145. if(isset($data['mobile'])){
  146. $check_mobile = Db::name('coach')->where('mobile',$data['mobile'])->where('id','neq',$this->auth->id)->find();
  147. if($check_mobile){
  148. $this->error('手机号已被其他人使用');
  149. }
  150. }
  151. $update_rs = Db::name('coach')->where('id',$this->auth->id)->update($data);
  152. $this->success('资料更新完成');
  153. }
  154. }