* @version : HUOSDK 8.0 */ namespace api\common\controller; use huoCheck\HuoApiV2; use huolib\constant\AppleParamConfusion; use huolib\constant\FormatConst; use huolib\status\OrderStatus; use think\exception\HttpResponseException; use think\Response; class AppleApiBaseController extends V2ApiBaseController { protected $rsc_rq_data = []; public function _initialize() { parent::_initialize(); } /** * 获取请求参数 * * @param mixed $param * @param bool $is_json * * @return bool|mixed|string */ public function getParam($param, $is_json = false) { if (empty($param)) { return ''; } if ($is_json) { $_param = json_decode($param, true); $_rsc_param = json_decode($param, true); if (JSON_ERROR_NONE != json_last_error()) { return false; } } else { foreach ($param as $_k => $_v) { $_rk = $this->ParameterConversion($_k); //请求参数变量名转换 /*原请求数据用于签名*/ if (strpos($_k, '-')) { list($_k1, $_k2) = explode('-', $_k); if (is_array($_k2)) { return false; } $_rsc_param[$_k1][$_k2] = $_v; } else { $_rsc_param[$_k] = $_v; } /*转换签名变量,与其他SDK 统一*/ if ('sign' == $_rk) { $_rsc_param['sign'] = $_rsc_param[$_k]; unset($_rsc_param[$_k]); } /*签名不需要的*/ if ('version' == $_rk || 's' == $_rk) { unset($_rsc_param[$_rk]); } /*转换请求参数变量用于业务逻辑处理*/ if (strpos($_rk, '-')) { list($_k1, $_k2) = explode('-', $_rk); $_param[$_k1][$_k2] = $_v; } else { $_param[$_rk] = $_v; } } } if (empty($_param)) { return false; } $this->rsc_rq_data = $_rsc_param; return $_param; } /** * 校验签名 */ protected function checkSign() { if (empty($this->device_type) || FormatConst::FORMAT_HTML == $this->response_type) { return true; } $_haV2 = new HuoApiV2(); //获取client_key $_key = $this->getKey(); $_haV2->setKey($_key); $_rs = $_haV2->check($this->request->path(), $this->rsc_rq_data, $this->request->method()); if (true != $_rs) { $this->error(OrderStatus::getMsg(OrderStatus::SIGN_ERROR), '', OrderStatus::SIGN_ERROR); } } /** * 操作成功跳转的快捷方法 * * @access protected * * @param mixed $msg 提示信息 * @param mixed $data 返回的数据 * @param int $code 返回码 * * @param array $header 发送的Header信息 * * @param null $url * @param int $wait * @param bool $is_flip * * @return void */ protected function success( $msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3, $is_flip = true ) { /*有返回参数的需要处理返回*/ if (!empty($data) && $is_flip) { $_confusion = (new AppleParamConfusion())->getConfusion(); $_confusion = array_flip($_confusion); //将数组键值反转 $_redata = []; if (isset($data['list']) && !empty($data['list'])) { $_redata['count'] = $data['count']; foreach ($data['list'] as $_k => $_v) { foreach ($_v as $_dk => $_dv) { if (isset($_confusion[$_dk])) { $_redata['list'][$_k][$_confusion[$_dk]] = $_dv; } else { $_redata['list'][$_k][$_dk] = $_dv; } } } } else { foreach ($data as $_k => $_v) { if (isset($_confusion[$_k])) { $_redata[$_confusion[$_k]] = $_v; } else { $_redata[$_k] = $_v; } } } $data = $_redata; } $type = $this->getResponseType(); if ('html' == $type) { parent::success($msg, $data, $code, $header, $url, $wait); } if (empty($data)) { $data = null; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; $header['Access-Control-Allow-Origin'] = '*'; $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang'; $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS'; $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /** * 操作错误跳转的快捷方法 * * @access protected * * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息'] * @param mixed $data 返回的数据 * @param int $code * * @param array $header 发送的Header信息 * * @param null $url * @param int $wait * * @return void */ protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3) { /*有返回参数的需要处理返回*/ if (!empty($data)) { $_confusion = (new AppleParamConfusion())->getConfusion(); $_confusion = array_flip($_confusion); //将数组键值反转 $_redata = []; foreach ($data as $_k => $_v) { if (isset($_confusion[$_k])) { $_redata[$_confusion[$_k]] = $_v; } else { $_redata[$_k] = $_v; } } $data = $_redata; } $type = $this->getResponseType(); if ('html' == $type) { parent::error($msg, $data, $code, $header, $url, $wait); } if (empty($data)) { $data = null; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; $header['Access-Control-Allow-Origin'] = '*'; $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang'; $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS'; $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /*** * 传入参数转换 * * @param string $params 原参数 * * @return string 转换后参数 */ protected function ParameterConversion($params) { $_confusion = (new AppleParamConfusion())->getConfusion(); return isset($_confusion[$params]) ? $_confusion[$params] : $params; /* $_redata = []; foreach ($params as $_k => $_v) { if (isset($_confusion[$_k])) { $_redata[$_confusion[$_k]] = $_v; } else { $_redata[$_k] = $_v; } } return $_redata; */ } }