123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace app\api\controller\inspection;
- use app\common\controller\InspectionApi;
- use app\common\library\InspectionAuth;
- /**
- * 验货员认证接口
- */
- class Auth extends InspectionApi
- {
- // 不需要登录的方法
- protected $noNeedLogin = ['login', 'setPassword'];
- // 不需要权限的方法
- protected $noNeedRight = ['*'];
- /**
- * 验货员登录
- */
- public function login()
- {
- $phone = $this->request->post('phone');
- $password = $this->request->post('password', '');
- if (!$phone) {
- $this->error('手机号不能为空');
- }
- $auth = InspectionAuth::instance();
- $result = $auth->login($phone, $password);
-
- if ($result) {
- $this->success('登录成功', [
- 'inspector' => $auth->getInspectorInfo(),
- 'token' => $auth->getToken()
- ]);
- } else {
- $this->error($auth->getError() ?: '登录失败');
- }
- }
- /**
- * 验货员退出登录
- */
- public function logout()
- {
- if ($this->auth->logout()) {
- $this->success('退出成功');
- } else {
- $this->error($this->auth->getError() ?: '退出失败');
- }
- }
- /**
- * 获取验货员信息
- */
- public function info()
- {
- $this->success('获取成功', $this->getInspectorInfo());
- }
- /**
- * 检查登录状态
- */
- public function check()
- {
- if ($this->auth->isLogin()) {
- $this->success('已登录', [
- 'inspector' => $this->getInspectorInfo(),
- 'token' => $this->auth->getToken()
- ]);
- } else {
- $this->error('未登录', null, 401);
- }
- }
- /**
- * 修改密码
- */
- public function changepwd()
- {
- $oldpassword = $this->request->post('oldpassword');
- $newpassword = $this->request->post('newpassword');
- $renewpassword = $this->request->post('renewpassword');
- if (!$oldpassword) {
- $this->error('旧密码不能为空');
- }
- if (!$newpassword) {
- $this->error('新密码不能为空');
- }
- if ($newpassword !== $renewpassword) {
- $this->error('两次输入的密码不一致');
- }
- if (strlen($newpassword) < 6) {
- $this->error('密码长度不能少于6位');
- }
- $result = $this->auth->changepwd($newpassword, $oldpassword);
- if ($result) {
- $this->success('密码修改成功,请重新登录');
- } else {
- $this->error($this->auth->getError() ?: '密码修改失败');
- }
- }
- /**
- * 设置初始密码(验货员首次设置密码)
- */
- public function setPassword()
- {
- $password = $this->request->post('password');
- $repassword = $this->request->post('repassword');
- if (!$password) {
- $this->error('密码不能为空');
- }
- if ($password !== $repassword) {
- $this->error('两次输入的密码不一致');
- }
- if (strlen($password) < 6) {
- $this->error('密码长度不能少于6位');
- }
- // 检查是否已设置过密码
- if ($this->application->password) {
- $this->error('密码已设置,请使用修改密码功能');
- }
- $result = $this->auth->setPassword($password);
- if ($result) {
- $this->success('密码设置成功');
- } else {
- $this->error($this->auth->getError() ?: '密码设置失败');
- }
- }
- }
|