123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- 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();
- }
-
- 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;
- }
-
- 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();
-
- $_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);
- }
- }
-
- 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);
- }
-
- 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);
- }
-
- protected function ParameterConversion($params) {
- $_confusion = (new AppleParamConfusion())->getConfusion();
- return isset($_confusion[$params]) ? $_confusion[$params] : $params;
-
- }
- }
|