AppleApiBaseController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * AppleApiBaseController.php UTF-8
  4. * 苹果接口校验
  5. *
  6. * @date : 2018/6/13 21:23
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\common\controller;
  13. use huoCheck\HuoApiV2;
  14. use huolib\constant\AppleParamConfusion;
  15. use huolib\constant\FormatConst;
  16. use huolib\status\OrderStatus;
  17. use think\exception\HttpResponseException;
  18. use think\Response;
  19. class AppleApiBaseController extends V2ApiBaseController {
  20. protected $rsc_rq_data = [];
  21. public function _initialize() {
  22. parent::_initialize();
  23. }
  24. /**
  25. * 获取请求参数
  26. *
  27. * @param mixed $param
  28. * @param bool $is_json
  29. *
  30. * @return bool|mixed|string
  31. */
  32. public function getParam($param, $is_json = false) {
  33. if (empty($param)) {
  34. return '';
  35. }
  36. if ($is_json) {
  37. $_param = json_decode($param, true);
  38. $_rsc_param = json_decode($param, true);
  39. if (JSON_ERROR_NONE != json_last_error()) {
  40. return false;
  41. }
  42. } else {
  43. foreach ($param as $_k => $_v) {
  44. $_rk = $this->ParameterConversion($_k); //请求参数变量名转换
  45. /*原请求数据用于签名*/
  46. if (strpos($_k, '-')) {
  47. list($_k1, $_k2) = explode('-', $_k);
  48. if (is_array($_k2)) {
  49. return false;
  50. }
  51. $_rsc_param[$_k1][$_k2] = $_v;
  52. } else {
  53. $_rsc_param[$_k] = $_v;
  54. }
  55. /*转换签名变量,与其他SDK 统一*/
  56. if ('sign' == $_rk) {
  57. $_rsc_param['sign'] = $_rsc_param[$_k];
  58. unset($_rsc_param[$_k]);
  59. }
  60. /*签名不需要的*/
  61. if ('version' == $_rk || 's' == $_rk) {
  62. unset($_rsc_param[$_rk]);
  63. }
  64. /*转换请求参数变量用于业务逻辑处理*/
  65. if (strpos($_rk, '-')) {
  66. list($_k1, $_k2) = explode('-', $_rk);
  67. $_param[$_k1][$_k2] = $_v;
  68. } else {
  69. $_param[$_rk] = $_v;
  70. }
  71. }
  72. }
  73. if (empty($_param)) {
  74. return false;
  75. }
  76. $this->rsc_rq_data = $_rsc_param;
  77. return $_param;
  78. }
  79. /**
  80. * 校验签名
  81. */
  82. protected function checkSign() {
  83. if (empty($this->device_type) || FormatConst::FORMAT_HTML == $this->response_type) {
  84. return true;
  85. }
  86. $_haV2 = new HuoApiV2();
  87. //获取client_key
  88. $_key = $this->getKey();
  89. $_haV2->setKey($_key);
  90. $_rs = $_haV2->check($this->request->path(), $this->rsc_rq_data, $this->request->method());
  91. if (true != $_rs) {
  92. $this->error(OrderStatus::getMsg(OrderStatus::SIGN_ERROR), '', OrderStatus::SIGN_ERROR);
  93. }
  94. }
  95. /**
  96. * 操作成功跳转的快捷方法
  97. *
  98. * @access protected
  99. *
  100. * @param mixed $msg 提示信息
  101. * @param mixed $data 返回的数据
  102. * @param int $code 返回码
  103. *
  104. * @param array $header 发送的Header信息
  105. *
  106. * @param null $url
  107. * @param int $wait
  108. * @param bool $is_flip
  109. *
  110. * @return void
  111. */
  112. protected function success(
  113. $msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3, $is_flip = true
  114. ) {
  115. /*有返回参数的需要处理返回*/
  116. if (!empty($data) && $is_flip) {
  117. $_confusion = (new AppleParamConfusion())->getConfusion();
  118. $_confusion = array_flip($_confusion); //将数组键值反转
  119. $_redata = [];
  120. if (isset($data['list']) && !empty($data['list'])) {
  121. $_redata['count'] = $data['count'];
  122. foreach ($data['list'] as $_k => $_v) {
  123. foreach ($_v as $_dk => $_dv) {
  124. if (isset($_confusion[$_dk])) {
  125. $_redata['list'][$_k][$_confusion[$_dk]] = $_dv;
  126. } else {
  127. $_redata['list'][$_k][$_dk] = $_dv;
  128. }
  129. }
  130. }
  131. } else {
  132. foreach ($data as $_k => $_v) {
  133. if (isset($_confusion[$_k])) {
  134. $_redata[$_confusion[$_k]] = $_v;
  135. } else {
  136. $_redata[$_k] = $_v;
  137. }
  138. }
  139. }
  140. $data = $_redata;
  141. }
  142. $type = $this->getResponseType();
  143. if ('html' == $type) {
  144. parent::success($msg, $data, $code, $header, $url, $wait);
  145. }
  146. if (empty($data)) {
  147. $data = null;
  148. }
  149. $result = [
  150. 'code' => $code,
  151. 'msg' => $msg,
  152. 'data' => $data,
  153. ];
  154. $header['Access-Control-Allow-Origin'] = '*';
  155. $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang';
  156. $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
  157. $response = Response::create($result, $type)->header($header);
  158. throw new HttpResponseException($response);
  159. }
  160. /**
  161. * 操作错误跳转的快捷方法
  162. *
  163. * @access protected
  164. *
  165. * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息']
  166. * @param mixed $data 返回的数据
  167. * @param int $code
  168. *
  169. * @param array $header 发送的Header信息
  170. *
  171. * @param null $url
  172. * @param int $wait
  173. *
  174. * @return void
  175. */
  176. protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3) {
  177. /*有返回参数的需要处理返回*/
  178. if (!empty($data)) {
  179. $_confusion = (new AppleParamConfusion())->getConfusion();
  180. $_confusion = array_flip($_confusion); //将数组键值反转
  181. $_redata = [];
  182. foreach ($data as $_k => $_v) {
  183. if (isset($_confusion[$_k])) {
  184. $_redata[$_confusion[$_k]] = $_v;
  185. } else {
  186. $_redata[$_k] = $_v;
  187. }
  188. }
  189. $data = $_redata;
  190. }
  191. $type = $this->getResponseType();
  192. if ('html' == $type) {
  193. parent::error($msg, $data, $code, $header, $url, $wait);
  194. }
  195. if (empty($data)) {
  196. $data = null;
  197. }
  198. $result = [
  199. 'code' => $code,
  200. 'msg' => $msg,
  201. 'data' => $data,
  202. ];
  203. $header['Access-Control-Allow-Origin'] = '*';
  204. $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang';
  205. $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
  206. $response = Response::create($result, $type)->header($header);
  207. throw new HttpResponseException($response);
  208. }
  209. /***
  210. * 传入参数转换
  211. *
  212. * @param string $params 原参数
  213. *
  214. * @return string 转换后参数
  215. */
  216. protected function ParameterConversion($params) {
  217. $_confusion = (new AppleParamConfusion())->getConfusion();
  218. return isset($_confusion[$params]) ? $_confusion[$params] : $params;
  219. /*
  220. $_redata = [];
  221. foreach ($params as $_k => $_v) {
  222. if (isset($_confusion[$_k])) {
  223. $_redata[$_confusion[$_k]] = $_v;
  224. } else {
  225. $_redata[$_k] = $_v;
  226. }
  227. }
  228. return $_redata;
  229. */
  230. }
  231. }