Auth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\common\controller\InspectionApi;
  4. use app\common\library\InspectionAuth;
  5. /**
  6. * 验货员认证接口
  7. */
  8. class Auth extends InspectionApi
  9. {
  10. // 不需要登录的方法
  11. protected $noNeedLogin = ['login', 'setPassword'];
  12. // 不需要权限的方法
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 验货员登录
  16. */
  17. public function login()
  18. {
  19. $phone = $this->request->post('phone');
  20. $password = $this->request->post('password', '');
  21. if (!$phone) {
  22. $this->error('手机号不能为空');
  23. }
  24. $auth = InspectionAuth::instance();
  25. $result = $auth->login($phone, $password);
  26. if ($result) {
  27. $this->success('登录成功', [
  28. 'inspector' => $auth->getInspectorInfo(),
  29. 'token' => $auth->getToken()
  30. ]);
  31. } else {
  32. $this->error($auth->getError() ?: '登录失败');
  33. }
  34. }
  35. /**
  36. * 验货员退出登录
  37. */
  38. public function logout()
  39. {
  40. if ($this->auth->logout()) {
  41. $this->success('退出成功');
  42. } else {
  43. $this->error($this->auth->getError() ?: '退出失败');
  44. }
  45. }
  46. /**
  47. * 获取验货员信息
  48. */
  49. public function info()
  50. {
  51. $this->success('获取成功', $this->getInspectorInfo());
  52. }
  53. /**
  54. * 检查登录状态
  55. */
  56. public function check()
  57. {
  58. if ($this->auth->isLogin()) {
  59. $this->success('已登录', [
  60. 'inspector' => $this->getInspectorInfo(),
  61. 'token' => $this->auth->getToken()
  62. ]);
  63. } else {
  64. $this->error('未登录', null, 401);
  65. }
  66. }
  67. /**
  68. * 修改密码
  69. */
  70. public function changepwd()
  71. {
  72. $oldpassword = $this->request->post('oldpassword');
  73. $newpassword = $this->request->post('newpassword');
  74. $renewpassword = $this->request->post('renewpassword');
  75. if (!$oldpassword) {
  76. $this->error('旧密码不能为空');
  77. }
  78. if (!$newpassword) {
  79. $this->error('新密码不能为空');
  80. }
  81. if ($newpassword !== $renewpassword) {
  82. $this->error('两次输入的密码不一致');
  83. }
  84. if (strlen($newpassword) < 6) {
  85. $this->error('密码长度不能少于6位');
  86. }
  87. $result = $this->auth->changepwd($newpassword, $oldpassword);
  88. if ($result) {
  89. $this->success('密码修改成功,请重新登录');
  90. } else {
  91. $this->error($this->auth->getError() ?: '密码修改失败');
  92. }
  93. }
  94. /**
  95. * 设置初始密码(验货员首次设置密码)
  96. */
  97. public function setPassword()
  98. {
  99. $password = $this->request->post('password');
  100. $repassword = $this->request->post('repassword');
  101. if (!$password) {
  102. $this->error('密码不能为空');
  103. }
  104. if ($password !== $repassword) {
  105. $this->error('两次输入的密码不一致');
  106. }
  107. if (strlen($password) < 6) {
  108. $this->error('密码长度不能少于6位');
  109. }
  110. // 检查是否已设置过密码
  111. if ($this->application->password) {
  112. $this->error('密码已设置,请使用修改密码功能');
  113. }
  114. $result = $this->auth->setPassword($password);
  115. if ($result) {
  116. $this->success('密码设置成功');
  117. } else {
  118. $this->error($this->auth->getError() ?: '密码设置失败');
  119. }
  120. }
  121. }