| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 | <?phpnamespace 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() ?: '密码设置失败');        }    }    /**     * 上传头像     */    public function uploadAvatar()    {        $file = $this->request->file('avatar');        if (!$file) {            $this->error('请选择头像文件');        }        // 移动到框架应用根目录/uploads/ 目录下        $info = $file->validate(['size' => 2 * 1024 * 1024, 'ext' => 'jpg,jpeg,png,gif'])                    ->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatar' . DS . 'inspection');                if (!$info) {            $this->error($file->getError());        }                // 获取上传文件的相对路径        $avatar = '/uploads/avatar/inspection/' . $info->getSaveName();                // 更新头像        $result = $this->auth->updateAvatar($avatar);        if ($result) {            $this->success('头像上传成功', [                'avatar' => cdnurl($avatar, true)            ]);        } else {            $this->error($this->auth->getError() ?: '头像更新失败');        }    }    /**     * 更新个人信息     */    public function updateProfile()    {        $name = $this->request->post('name');        $avatar = $this->request->post('avatar');                if ($name) {            $this->application->name = $name;        }                if ($avatar) {            // 处理头像路径,去除域名部分            $avatar = str_replace(request()->domain(), '', $avatar);            $result = $this->auth->updateAvatar($avatar);            if (!$result) {                $this->error($this->auth->getError() ?: '头像更新失败');            }        }                if ($name) {            try {                $this->application->save();            } catch (\Exception $e) {                $this->error('信息更新失败:' . $e->getMessage());            }        }                $this->success('信息更新成功', $this->getInspectorInfo());    }} 
 |