request = is_null($request) ? Request::instance() : $request; $this->_initialize(); } protected function _initialize() { // 跨域检测 check_cors_request(); // IP 检查 check_ip_allowed(); // 过滤请求 $this->request->filter('trim,strip_tags,htmlspecialchars'); $this->auth = InspectionAuth::instance(); // 检查是否需要登录 $action = $this->request->action(); if (!$this->auth->match($this->noNeedLogin)) { // token获取 $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('inspection_token'))); // 初始化验货员身份 if (!$this->auth->init($token)) { $this->error($this->auth->getError() ?: '请先登录', null, 401); } if (!$this->auth->isLogin()) { $this->error('请先登录', null, 401); } $this->application = $this->auth->getApplication(); $this->user = $this->auth->getUser(); // 检查审核状态 if (!$this->application || $this->application->audit_status != 2) { $this->error('验货员未通过审核', null, 403); } // 检查启用状态 if (!$this->application || $this->application->status != 1) { $this->error('验货员账号已被禁用', null, 403); } // 检查供应商绑定 if (!$this->application->supplier_id) { $this->error('未绑定供应商', null, 403); } // 检查权限 if (!$this->auth->match($this->noNeedRight)) { // 这里可以添加具体的权限检查逻辑 // 暂时允许所有已登录的验货员访问 } } // 加载语言包 $controllername = strtolower($this->request->controller()); $lang = $this->request->langset(); $lang = preg_match("/^([a-zA-Z\-_]{2,10})$/i", $lang) ? $lang : 'zh-cn'; Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php'); } /** * 获取验货员信息 * @return array */ protected function getInspectorInfo() { if (!$this->auth || !$this->auth->isLogin()) { return null; } return $this->auth->getInspectorInfo(); } /** * 检查验货员权限 * @param string $path * @param string $module * @return bool */ protected function checkAuth($path = null, $module = null) { return $this->auth->check($path, $module); } protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = []) { $this->result($msg, $data, $code, $type, $header); } protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = []) { $this->result($msg, $data, $code, $type, $header); } protected function result($msg, $data = null, $code = 0, $type = null, array $header = []) { $result = [ 'code' => $code, 'msg' => $msg, 'time' => Request::instance()->server('REQUEST_TIME'), 'data' => $data, ]; // 添加验货员信息到响应中(如果已登录) if ($this->auth && $this->auth->isLogin()) { $result['inspector'] = $this->getInspectorInfo(); } $type = $type ?: $this->responseType; if (isset($header['statuscode'])) { $code = $header['statuscode']; unset($header['statuscode']); } else { $code = $code >= 1000 || $code < 200 ? 200 : $code; } $response = Response::create($result, $type, $code)->header($header); throw new HttpResponseException($response); } }