request = is_null($request) ? Request::instance() : $request; // 控制器初始化 $this->_initialize(); } protected function _initialize() { $this->debug = Config::get('client.app_debug'); $strParams = $this->request->param('appParams'); $this->params = json_decode($strParams, true); $commonParam = $this->request->header('common'); $this->aCommon = json_decode($commonParam, true); $this->_validateParams(); $this->_sign(); if ($this->aCommon['ust'] == ClientApiConstants::USER_TYPE_LOGIN) { if (empty($this->aCommon['uid'])) { $this->error("公共参数uid为空"); } if (empty($this->aCommon['token'])) { $this->error("公共参数token为空"); } $userInfoResult = WebUserService::instance()->setUserInfo($this->aCommon['uid'], $this->aCommon['token']); if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) { $this->userInfo = WebUserService::instance()->getUserInfo()->toArray(); } else { $this->error($userInfoResult->msg); } } } /** * 公共参数校验 */ private function _validateParams() { foreach (ClientApiConstants::$generalParams as $generalParam) { if (!isset($this->aCommon[$generalParam])) { $this->error("公共参数{$generalParam}不存在"); } if (strlen(trim($this->aCommon[$generalParam])) == 0) { $this->error("公共参数{$generalParam}为空"); } } } /** * api签名校验 */ private function _sign() { if ($this->debug) { LogService::info('debug模式,跳过签名校验'); return; } $originalSign = Request::instance()->header('sign'); if (empty($originalSign)) { $this->error('签名错误,1'); } $arrSign = []; $orgParams = [ 'code' => $this->request->param('code'), 'common' => $this->request->header('common'), ]; $orgParams = array_merge($orgParams, $this->params); ksort($orgParams); foreach ($orgParams as $k => $param) { $strTmp = trim($k) . '=' . trim($param); $arrSign[] = $strTmp; } $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1'; $strSign = implode('&', $arrSign); $sign = md5($strSign); if ($originalSign != $sign) { $this->error('签名错误,2'); } } /** * 操作成功返回的数据 * @param string $msg 提示信息 * @param array $data * @param array $header */ protected function success($msg = '', $data = [], array $header = []) { $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS, $msg, $header); } /** * 操作成功返回对象数据 * @param array $data */ protected function info($data = []) { $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS); } /** * 操作失败返回的数据 * @param string $msg 提示信息 * @param int $code 错误代码 * @param mixed $data 要返回的数据 * @param array $header 发送的 Header 信息 */ protected function error( $msg = '', $code = ClientApiConstants::CLIENT_API_CODE_FAIL, $data = [], array $header = [] ) { if (empty($msg)) { if (isset(ClientApiConstants::$errMsgList[$code])) { $msg = ClientApiConstants::$errMsgList[$code]; } } $this->result($data, $code, $msg, $header); } /** * 返回封装后的 API 数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param int $code 返回的 code * @param mixed $msg 提示信息 * @param array $header 发送的 Header 信息 * @return void * @throws HttpResponseException */ protected function result($data, $code = 0, $msg = '', array $header = []) { $result = [ 'code' => $code, 'msg' => $msg, 'time' => Request::instance()->server('REQUEST_TIME'), 'data' => camelizeArr($data), ]; if (empty($data)) { $result['data'] = new \ArrayObject(array()); } $response = Response::create($result, 'json')->header($header); throw new HttpResponseException($response); } /** * 检查参数必填 * @param string|array $paramName 参数名 * @param bool $checkEmpty 是否检查空值 */ protected function checkParamRequired($paramName, $checkEmpty = true) { if (is_array($paramName)) { foreach ($paramName as $item) { $this->_checkParamRequired($item, $checkEmpty); } } else { $this->_checkParamRequired($paramName, $checkEmpty); } } /** * 校验接口输入参数必填 * @param $paramName 参数名 * @param $checkEmpty 参数值是否使用php的empty函数校验 */ private function _checkParamRequired($paramName, $checkEmpty) { $params = $this->params; if ($checkEmpty) { if (empty($params[$paramName])) { $this->error("参数{$paramName}不能为空"); } } else { if (!isset($params[$paramName])) { $this->error("参数{$paramName}不存在"); } } } /** * 校验用户登录 */ protected function checkUserLogin() { if (empty($this->userInfo)) { $this->error('需要登录', ClientApiConstants::CLIENT_API_CODE_NEED_LOGIN); } } /** * 格式 * @return array */ protected function userInfoFormat() { $this->checkUserLogin(); $userInfo = $this->userInfo; $userId = $userInfo['id']; $userResult = [ 'name' => $userInfo['nickname'], 'uid' => $userId, 'token' => $userInfo['openid'], 'avatar' => $userInfo['avatar'], ]; $freeKandian = FinancialService::instance()->getTotalFreeKandian($userId)->data; $kandian = FinancialService::instance()->getTotalKandian($userId)->data; $signResult = ClientAppService::instance()->getTodaySign($userId); $userResult['isVip'] = $userInfo['vip_endtime'] > time(); $userResult['isMarked'] = $signResult->data; $userResult['balance'] = $freeKandian + $kandian; $userResult['ch'] = $userInfo['channel_id']; $userResult['ag'] = $userInfo['agent_id']; return $userResult; } public function getParams($key) { if (array_key_exists($key, $this->params)) { return $this->params[$key]; } return ''; } }