User.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = ['emaillogin','resetpwd'];
  14. protected $noNeedRight = '*';
  15. //员工手机+密码登录
  16. public function emaillogin()
  17. {
  18. $account = input('account');
  19. $password = input('password');
  20. if (!$account || !$password) {
  21. $this->error(__('Invalid parameters'));
  22. }
  23. $ret = $this->auth->login($account, $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. //'email',
  106. 'avatar',
  107. 'firstname',
  108. 'lastname',
  109. 'whatsapp',
  110. ];
  111. $data = request_post_hub($field);
  112. /*if(isset($data['email'])){
  113. $check_email = Db::name('coach')->where('email',$data['email'])->where('id','neq',$this->auth->id)->find();
  114. if($check_email){
  115. $this->error('邮箱已被其他人使用');
  116. }
  117. }*/
  118. if(isset($data['mobile'])){
  119. $check_mobile = Db::name('coach')->where('mobile',$data['mobile'])->where('id','neq',$this->auth->id)->find();
  120. if($check_mobile){
  121. $this->error('手机号已被其他人使用');
  122. }
  123. }
  124. $update_rs = Db::name('coach')->where('id',$this->auth->id)->update($data);
  125. $this->success('资料更新完成');
  126. }
  127. }