_logined) { return true; } if ($this->_error) { return false; } $application = InspectionApplication::where('token', $token) ->where('audit_status', 2) ->where('status', 1) ->find(); if (!$application) { $this->setError('验货员未登录或未通过审核'); return false; } $this->_application = $application; $this->_user = User::get($application->user_id); $this->_logined = true; $this->_token = $token; return true; } /** * 直接登录验货员 * @param int $user_id * @return bool */ public function direct($user_id) { $application = InspectionApplication::where('user_id', $user_id) ->where('audit_status', 2) ->where('status', 1) ->find(); if (!$application) { $this->setError('验货员未通过审核'); return false; } $token = Random::uuid(); $expire = time() + $this->keeptime; $application->token = $token; $application->token_expiretime = $expire; $application->save(); $this->_application = $application; $this->_user = User::get($application->user_id); $this->_logined = true; $this->_token = $token; return true; } /** * 判断是否已登录 * @return bool */ public function isLogin() { return $this->_logined; } /** * 获取当前Token * @return string */ public function getToken() { return $this->_token; } /** * 获取验货员申请信息 * @return InspectionApplication|null */ public function getApplication() { return $this->_application; } /** * 获取验货员用户信息 * @return User|null */ public function getUser() { return $this->_user; } /** * 获取供应商ID * @return int */ public function getSupplierId() { return $this->_application ? $this->_application->supplier_id : 0; } /** * 退出登录 * @return bool */ public function logout() { if (!$this->_logined) { $this->setError('未登录'); return false; } $this->_application->token = null; $this->_application->token_expiretime = null; $this->_application->save(); $this->_logined = false; $this->_token = ''; return true; } /** * 设置错误信息 * @param string $error * @return $this */ public function setError($error) { $this->_error = $error; return $this; } /** * 获取错误信息 * @return string */ public function getError() { return $this->_error; } /** * 获取允许输出的字段 * @return array */ public function getAllowFields() { return $this->allowFields; } /** * 设置允许输出的字段 * @param array $fields */ public function setAllowFields($fields) { $this->allowFields = $fields; } }