request = is_null($request) ? Request::instance() : $request; $this->_initialize(); } protected function _initialize() { // Cookie::set('web_ust', ['uid' => 60007, 'token' => 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU'], 3600 * 24 * 30); $this->debug = Config::get('client.app_debug'); $this->time = $this->request->server('REQUEST_TIME'); $commonParam = $this->request->header('common'); $ajaxParams = $this->request->header('AjaxParams'); $this->ajaxParams = json_decode($ajaxParams, true); $this->aCommon = json_decode($commonParam, true); $this->params = $this->request->param(); $this->checkLogin(); $this->redis = Redis::instance(); } /** * 检测登陆 */ private function checkLogin() { if (!$this->isLogin) { //判断是否有cookie if (Cookie::has('user_id') && Cookie::has('token')) { $userId = Cookie::get('user_id'); $token = Cookie::get('token'); $userInfoResult = WebUserService::instance()->setUserInfo($userId, $token); if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) { $this->userInfo = WebUserService::instance()->getUserInfo()->toArray(); $this->isLogin = true; $this->userid = $this->userInfo['id']; $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男 } else { Log::info('h5页内登录失败:'.$userInfoResult->msg); } } else { Log::info('h5页内登录失败:缺少参数'); } } } /** * header签名校验 */ public function checkSign() { if ($this->debug) { LogService::info('debug模式,跳过签名校验'); return true; } $originalSign = $this->aCommon['sign'] ?? ''; if (empty($originalSign)) { LogService::error('签名错误, 缺少sign参数'); return false; } $arrSign = []; ksort($this->aCommon); foreach ($this->aCommon as $k => $param) { $strTmp = trim($k) . '=' . trim($param); $arrSign[] = $strTmp; } $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1'; $strSign = implode('&', $arrSign); $sign = md5($strSign); if ($originalSign != $sign) { LogService::error('签名错误,验证失败'); return false; } return true; } /** * 操作成功返回的数据 * @param string $msg 提示信息 * @param mixed $data 要返回的数据 * @param string $type 输出类型 * @param array $header 发送的 Header 信息 */ protected function success($msg = '', $data = '', $type = 'json', array $header = []) { $this->result($data, 1, $msg, $type, $header); } /** * 操作失败返回的数据 * @param string $msg 提示信息 * @param mixed $data 要返回的数据 * @param string $type 输出类型 * @param array $header 发送的 Header 信息 */ protected function error($msg = '', $data = '', $type = 'json', array $header = []) { $this->result($data, 0, $msg, $type, $header); } /** * 返回封装后的 API 数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param int $code 返回的 code * @param mixed $msg 提示信息 * @param string $type 返回数据格式 * @param array $header 发送的 Header 信息 * @return void * @throws HttpResponseException */ protected function result($data, $code = 0, $msg = '', $type = '', array $header = []) { $result = [ 'code' => $code, 'msg' => $msg, 'time' => Request::instance()->server('REQUEST_TIME'), 'data' => $data, ]; $type = $type ?: $this->getResponseType(); $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /** * 未找到请求的接口 */ public function _empty() { $this->error('Api not found'); } /** * 获取当前的 response 输出类型 * @access protected * @return string */ protected function getResponseType() { return Request::instance()->isAjax() ? Config::get('default_ajax_return') : Config::get('default_return_type'); } }