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(); // token $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token'))); // 初始化验货员身份 $this->auth->init($token); 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->supplier_id) { $this->error('未绑定供应商', null, 403); } // 加载语言包 $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'); } 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, ]; $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); } }