User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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'];
  15. protected $noNeedRight = '*';
  16. //用户详细资料
  17. public function userInfo(){
  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\CompanyStaff::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->resetpwd($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. 'realname',
  87. 'idcard',
  88. 'english_status',
  89. 'idcard_z_image',
  90. 'idcard_f_image',
  91. 'doctor_image',
  92. 'avatar','nickname','gender',
  93. 'keshi_id','hospital','goodat','level_id','info','job_status'
  94. ];
  95. $data = [];
  96. foreach($field_array as $key => $field){
  97. //前端传不了post,改了
  98. /*if(!request()->has($field,'post')){
  99. continue;
  100. }*/
  101. if(!input('?'.$field)){
  102. continue;
  103. }
  104. $newone = input($field);
  105. if($field == 'avatar'){
  106. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  107. }
  108. $data[$field] = $newone;
  109. }
  110. //
  111. /*if(isset($data['birthday'])){
  112. $data['birthday'] = strtotime($data['birthday']);
  113. }*/
  114. if(empty($data)){
  115. $this->success();
  116. }
  117. if(isset($data['realname']) && isset($data['idcard']) && isset($data['idcard_z_image']) && isset($data['idcard_f_image'])){
  118. $data['idcard_status'] = 0;
  119. }
  120. if(isset($data['doctor_image'])){
  121. $data['doctor_status'] = 0;
  122. }
  123. $update_rs = Db::name('worker')->where('id',$this->auth->id)->update($data);
  124. $this->success();
  125. }
  126. //假注销
  127. public function cancleUser(){
  128. /*$captcha = input('captcha','');
  129. if (!$captcha) {
  130. $this->error(__('Invalid parameters'));
  131. }
  132. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  133. $this->error(__('Captcha is incorrect'));
  134. }*/
  135. Db::name('worker')->where('id',$this->auth->id)->update(['status'=>-1]);
  136. $this->auth->logout();
  137. $this->success('注销成功');
  138. }
  139. //员工手机+密码登录
  140. public function login()
  141. {
  142. $mobile = input('mobile');
  143. $password = input('password');
  144. if (!$mobile || !$password) {
  145. $this->error(__('Invalid parameters'));
  146. }
  147. $ret = $this->auth->login($mobile, $password);
  148. if ($ret) {
  149. $data = $this->auth->getUserinfo();
  150. $this->success(__('Logged in successful'), $data);
  151. } else {
  152. $this->error($this->auth->getError());
  153. }
  154. }
  155. /**
  156. * 修改密码
  157. *
  158. * @ApiMethod (POST)
  159. * @param string $newpassword 新密码
  160. * @param string $oldpassword 旧密码
  161. */
  162. public function changepwd(){
  163. $newpassword = input('newpassword');
  164. $oldpassword = input('oldpassword','');
  165. $captcha = input('captcha','');
  166. if (!$captcha) {
  167. $this->error(__('Invalid parameters'));
  168. }
  169. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  170. $this->error(__('Captcha is incorrect'));
  171. }
  172. if (!$newpassword) {
  173. $this->error('请输入新密码');
  174. }
  175. if($this->auth->password && empty($oldpassword)){
  176. $this->error('旧密码必填');
  177. }
  178. if(empty($this->auth->password)){
  179. $ret = $this->auth->changepwd($newpassword, '', true);
  180. }else{
  181. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  182. }
  183. if ($ret) {
  184. $this->success(__('Reset password successful'));
  185. } else {
  186. $this->error($this->auth->getError());
  187. }
  188. }
  189. }