User.php 5.4 KB

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