User.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $rs = $tenim->useredit('master_'. $this->auth->id, '', localpath_to_netpath($data['avatar']));
  115. $this->success();
  116. }
  117. //假注销
  118. public function cancleuser(){
  119. /*$captcha = input('captcha','');
  120. if (!$captcha) {
  121. $this->error(__('Invalid parameters'));
  122. }
  123. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  124. $this->error(__('Captcha is incorrect'));
  125. }*/
  126. Db::name('worker')->where('id',$this->auth->id)->update(['status'=>-1]);
  127. $this->auth->logout();
  128. $this->success('注销成功');
  129. }
  130. //员工手机+密码登录
  131. public function login()
  132. {
  133. $mobile = input('mobile');
  134. $password = input('password');
  135. if (!$mobile || !$password) {
  136. $this->error(__('Invalid parameters'));
  137. }
  138. $ret = $this->auth->login($mobile, $password);
  139. if ($ret) {
  140. $data = $this->auth->getUserinfo_simple();
  141. $this->success(__('Logged in successful'), $data);
  142. } else {
  143. $this->error($this->auth->getError());
  144. }
  145. }
  146. /**
  147. * 修改密码
  148. *
  149. * @ApiMethod (POST)
  150. * @param string $newpassword 新密码
  151. * @param string $oldpassword 旧密码
  152. */
  153. public function changepwd(){
  154. $newpassword = input('newpassword');
  155. $oldpassword = input('oldpassword','');
  156. $captcha = input('captcha','');
  157. if (!$captcha) {
  158. $this->error(__('Invalid parameters'));
  159. }
  160. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  161. $this->error(__('Captcha is incorrect'));
  162. }
  163. if (!$newpassword) {
  164. $this->error('请输入新密码');
  165. }
  166. if($this->auth->password && empty($oldpassword)){
  167. $this->error('旧密码必填');
  168. }
  169. if(empty($this->auth->password)){
  170. $ret = $this->auth->changepwd($newpassword, '', true);
  171. }else{
  172. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  173. }
  174. if ($ret) {
  175. $this->success();
  176. } else {
  177. $this->error($this->auth->getError());
  178. }
  179. }
  180. }