request = is_null($request) ? Request::instance() : $request; $this->redis = Redis::instance(); // 控制器初始化 parent::__construct($this->request); } protected function _initialize() { // Cookie::set('user_id', '60007'); // Cookie::set('token', 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU'); $this->debug = Config::get('client.app_debug'); $this->time = $this->request->server('REQUEST_TIME'); $commonParam = $this->request->header('common'); if ($commonParam) { $this->aCommon = json_decode($commonParam, true); } $this->_sign(); $this->params = $allParams = $this->request->param(); if (isset($allParamss['params'])) { $this->pCommon = json_decode($allParams['params'], true); unset($allParams['params']); } $this->urlParams = $allParams; LogService::info("公参:". json_encode($this->aCommon, 256)); LogService::info("私参:". json_encode($this->pCommon, 256)); LogService::info("url参数:".json_encode($this->urlParams, 256)); // $this->aCommon['uid'] = '20000034'; // $this->aCommon['token'] = 'oKWvT037RMlmaBm-pQ8nY2o3G3vw'; //登录 if (!$this->isLogin) $this->h5Login(); /** * 配置项处理 */ $site = Config::get("site"); $this->assign('log_host', Config::get('site.loghost')); //设置打点域名 $moduleName = Request::instance()->module(); $controllerName = strtolower(Request::instance()->controller()); $actionName = strtolower(Request::instance()->action()); // 配置信息 $config = [ 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])), 'modulename' => $moduleName, 'controllername' => $controllerName, 'actionname' => $actionName, 'jsname' => 'frontend/' . str_replace('.', '/', $controllerName), 'moduleurl' => rtrim(url("/{$moduleName}", '', false), '/'), ]; // 配置信息后 Hook::listen("config_init", $config); $this->assign('site', $site); $this->assign('config', $config); //域名 $this->view->assign('app_domain', $this->request->domain()); $this->view->assign('is_login', $this->isLogin); } /** * 验证签名 * @return bool */ private function _sign() { return true; //return $this->checkSign() || $this->checkCookie(); } /** * header签名校验 */ private function checkSign() { if ($this->debug) { LogService::info('debug模式,跳过签名校验'); return true; } $originalSign = Request::instance()->header('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; } /** * cookie签名验证 * 使用token简单的参与签名 */ private function checkCookie() { if ($this->debug) { LogService::info('debug模式,跳过签名校验'); return true; } if (Cookie::has('h5Sign') && Cookie::has('token')) { $h5Sign = Cookie::get('h5Sign'); $arrSign['token'] = Cookie::get('token'); $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1'; $strSign = implode('&', $arrSign); if ($h5Sign != md5($strSign)) { LogService::error('h5签名错误,验证失败'); return false; } return true; } LogService::error('h5签名错误,缺少参数'); return false; } /** * h5登陆 */ private function h5Login() { //是否是客户端直接拉起 if (!empty($this->aCommon)) { $uid = $this->aCommon['uid'] ?? 0; $token = $this->aCommon['token'] ?? ''; if (!!$uid && !!$token) { $userInfoResult = WebUserService::instance()->setUserInfo($uid, $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; //未知默认男 //设置cookie $arrSign['token'] = $token; $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1'; $strSign = implode('&', $arrSign); Cookie::set('user_id', $uid, 3600 * 24 * 30); Cookie::set('token', $token, 3600 * 24 * 30); Cookie::set('h5Sign', md5($strSign), 3600 * 24 * 30); Cookie::set('channel_id', $this->userInfo['channel_id'], 3600 * 24 * 30); Cookie::set('agent_id', $this->userInfo['agent_id'], 3600 * 24 * 30); } else { Log::info('h5客户端登录失败:'.$userInfoResult->msg); } } else { Log::info('h5客户端登录失败:缺少参数'); } } 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页内登录失败:缺少参数'); } } } }