Auth.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /**
  122. * 上传头像
  123. */
  124. public function uploadAvatar()
  125. {
  126. $file = $this->request->file('avatar');
  127. if (!$file) {
  128. $this->error('请选择头像文件');
  129. }
  130. // 移动到框架应用根目录/uploads/ 目录下
  131. $info = $file->validate(['size' => 2 * 1024 * 1024, 'ext' => 'jpg,jpeg,png,gif'])
  132. ->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatar' . DS . 'inspection');
  133. if (!$info) {
  134. $this->error($file->getError());
  135. }
  136. // 获取上传文件的相对路径
  137. $avatar = '/uploads/avatar/inspection/' . $info->getSaveName();
  138. // 更新头像
  139. $result = $this->auth->updateAvatar($avatar);
  140. if ($result) {
  141. $this->success('头像上传成功', [
  142. 'avatar' => cdnurl($avatar, true)
  143. ]);
  144. } else {
  145. $this->error($this->auth->getError() ?: '头像更新失败');
  146. }
  147. }
  148. /**
  149. * 更新个人信息
  150. */
  151. public function updateProfile()
  152. {
  153. $name = $this->request->post('name');
  154. $avatar = $this->request->post('avatar');
  155. if ($name) {
  156. $this->application->name = $name;
  157. }
  158. if ($avatar) {
  159. // 处理头像路径,去除域名部分
  160. $avatar = str_replace(request()->domain(), '', $avatar);
  161. $result = $this->auth->updateAvatar($avatar);
  162. if (!$result) {
  163. $this->error($this->auth->getError() ?: '头像更新失败');
  164. }
  165. }
  166. if ($name) {
  167. try {
  168. $this->application->save();
  169. } catch (\Exception $e) {
  170. $this->error('信息更新失败:' . $e->getMessage());
  171. }
  172. }
  173. $this->success('信息更新成功', $this->getInspectorInfo());
  174. }
  175. }