User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\api\controller\worker;
  3. use app\common\controller\Apiw;
  4. use app\common\library\Sms;
  5. use think\Exception;
  6. use think\Validate;
  7. use think\Db;
  8. use app\common\library\Wechat;
  9. use app\common\library\Tenim;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Apiw
  14. {
  15. protected $noNeedLogin = ['login','resetpwd'];
  16. protected $noNeedRight = '*';
  17. //用户详细资料
  18. public function getuserinfo(){
  19. $info = $this->auth->getUserinfo();
  20. $this->success(__('success'),$info);
  21. }
  22. /**
  23. * 退出登录
  24. * @ApiMethod (POST)
  25. */
  26. public function logout()
  27. {
  28. if (!$this->request->isPost()) {
  29. $this->error(__('Invalid parameters'));
  30. }
  31. $this->auth->logout();
  32. $this->success(__('Logout successful'));
  33. }
  34. /**
  35. * 重置密码
  36. *
  37. * @ApiMethod (POST)
  38. * @param string $mobile 手机号
  39. * @param string $captcha 验证码
  40. * @param string $newpassword 新密码
  41. */
  42. public function resetpwd()
  43. {
  44. $mobile = $this->request->post('mobile');
  45. $captcha = $this->request->post('captcha');
  46. $newpassword = $this->request->post("newpassword");
  47. if (!$mobile || !$captcha || !$newpassword) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. //验证Token
  51. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  52. $this->error(__('Password must be 6 to 30 characters'));
  53. }
  54. if (!Validate::regex($mobile, "^1\d{10}$")) {
  55. $this->error(__('Mobile is incorrect'));
  56. }
  57. $user = \app\common\model\Worker::getByMobile($mobile);
  58. if (!$user) {
  59. $this->error(__('User not found'));
  60. }
  61. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  62. if (!$ret) {
  63. $this->error(__('Captcha is incorrect'));
  64. }
  65. Sms::flush($mobile, 'resetpwd');
  66. //模拟一次登录
  67. $this->auth->direct($user->id);
  68. $ret = $this->auth->changepwd($newpassword, '', true);
  69. if ($ret) {
  70. $this->success(__('Reset password successful'));
  71. } else {
  72. $this->error($this->auth->getError());
  73. }
  74. }
  75. /**
  76. * 修改会员个人信息
  77. *
  78. * @ApiMethod (POST)
  79. * @param string $avatar 头像地址
  80. * @param string $username 用户名
  81. * @param string $nickname 昵称
  82. * @param string $bio 个人简介
  83. */
  84. public function profile()
  85. {
  86. $field_array = [
  87. 'idcard_z_image',
  88. 'idcard_f_image',
  89. 'jineng_image',
  90. 'avatar',
  91. ];
  92. $data = [];
  93. foreach($field_array as $key => $field){
  94. //前端传不了post,改了
  95. /*if(!request()->has($field,'post')){
  96. continue;
  97. }*/
  98. if(!input('?'.$field)){
  99. continue;
  100. }
  101. $newone = input($field);
  102. if($field == 'avatar'){
  103. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  104. }
  105. $data[$field] = $newone;
  106. }
  107. if(empty($data)){
  108. $this->success();
  109. }
  110. $update_rs = Db::name('worker')->where('id',$this->auth->id)->update($data);
  111. //如果有修改头像或昵称,同步到im
  112. //user_用户端小程序,master_师傅,kefu_客服
  113. $tenim = new Tenim();
  114. $avatar = isset($data['avatar']) ? localpath_to_netpath($data['avatar']) : '';
  115. $rs = $tenim->useredit('master_'. $this->auth->id, '', $avatar);
  116. $this->success();
  117. }
  118. //假注销
  119. public function cancleuser(){
  120. /*$captcha = input('captcha','');
  121. if (!$captcha) {
  122. $this->error(__('Invalid parameters'));
  123. }
  124. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  125. $this->error(__('Captcha is incorrect'));
  126. }*/
  127. Db::name('worker')->where('id',$this->auth->id)->update(['status'=>-1]);
  128. $this->auth->logout();
  129. $this->success('注销成功');
  130. }
  131. //员工手机+密码登录
  132. public function login()
  133. {
  134. $mobile = input('mobile');
  135. $password = input('password');
  136. if (!$mobile || !$password) {
  137. $this->error(__('Invalid parameters'));
  138. }
  139. $ret = $this->auth->login($mobile, $password);
  140. if ($ret) {
  141. $data = $this->auth->getUserinfo_simple();
  142. $this->success(__('Logged in successful'), $data);
  143. } else {
  144. $this->error($this->auth->getError());
  145. }
  146. }
  147. /**
  148. * 修改密码
  149. *
  150. * @ApiMethod (POST)
  151. * @param string $newpassword 新密码
  152. * @param string $oldpassword 旧密码
  153. */
  154. public function changepwd(){
  155. $newpassword = input('newpassword');
  156. $oldpassword = input('oldpassword','');
  157. $captcha = input('captcha','');
  158. if (!$captcha) {
  159. $this->error(__('Invalid parameters'));
  160. }
  161. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  162. $this->error(__('Captcha is incorrect'));
  163. }
  164. if (!$newpassword) {
  165. $this->error('请输入新密码');
  166. }
  167. if($this->auth->password && empty($oldpassword)){
  168. $this->error('旧密码必填');
  169. }
  170. if(empty($this->auth->password)){
  171. $ret = $this->auth->changepwd($newpassword, '', true);
  172. }else{
  173. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  174. }
  175. if ($ret) {
  176. $this->success();
  177. } else {
  178. $this->error($this->auth->getError());
  179. }
  180. }
  181. }