User.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\auth\MysqlAdapter;
  4. use app\api\service\auth\ApiAuthService;
  5. use app\api\validate\User as UserValidate;
  6. use think\exception\ValidateException;
  7. use app\common\model\User as UserModel;
  8. class User extends Base
  9. {
  10. protected $noNeedLogin = ['login'];
  11. public function userinfo(UserModel $userModel)
  12. {
  13. $user = $userModel->find($this->userinfo['id']);
  14. return $this->success('ok', $user);
  15. }
  16. //编辑用户
  17. public function edit(UserModel $userModel){
  18. $data = $this->request->post();
  19. try{
  20. validate(UserValidate::class)->scene('edit')->check($data);
  21. $user = $userModel->find($this->userinfo['id']);
  22. if(!$user){
  23. return $this->error('用户不存在');
  24. }
  25. $user->save($data);
  26. return $this->success('ok');
  27. }catch (ValidateException $e) {
  28. return $this->error($e->getError());
  29. }
  30. }
  31. //修改密码
  32. public function password(UserModel $userModel){
  33. $data = $this->request->post();
  34. try{
  35. validate(UserValidate::class)->scene('password')->check($data);
  36. $user = $userModel->find($this->userinfo['id']);
  37. if(!$user){
  38. return $this->error('用户不存在');
  39. }
  40. if($user->password != md5(md5($data['oldpassword'].$user->salt))){
  41. return $this->error('旧密码错误');
  42. }
  43. $user->password = md5(md5($data['password'].$user->salt));
  44. $user->save();
  45. return $this->success('ok');
  46. }catch (ValidateException $e) {
  47. return $this->error($e->getError());
  48. }
  49. }
  50. public function login(ApiAuthService $authService,UserModel $userModel)
  51. {
  52. $data = $this->request->post();
  53. try{
  54. validate(UserValidate::class)->scene('login')->check($data);
  55. $terminal = $this->request->post('terminal/d', 0);
  56. $user = $authService->login($data['username'],$data['password'], $terminal);
  57. }catch (ValidateException $e) {
  58. return $this->error($e->getError());
  59. }catch (\Exception $e){
  60. return $this->error($e->getMessage());
  61. }
  62. return $this->success('ok', $user);
  63. }
  64. //退出登录
  65. public function logout()
  66. {
  67. MysqlAdapter::logout($this->userinfo['id']);
  68. return $this->success('ok');
  69. }
  70. //获取全部人员
  71. public function getAllUser(UserModel $userModel)
  72. {
  73. $list = $userModel->where('status', 'normal')->field('id,nickname,avatar')->select();
  74. return $this->success('ok', $list);
  75. }
  76. }