ZopBaseService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\common\service;
  3. use think\Log;
  4. /**
  5. * 订单推送基础服务类
  6. */
  7. class ZopBaseService
  8. {
  9. // const ZOP_URL = 'https://demo.mall.10010.com:8104/zop';
  10. const ZOP_URL = 'https://cd.10010.com/zop';
  11. const ZOP_KEY = [
  12. // 'appCode' => '36F4FB0764884970939A8962AAAED993',
  13. // 'HMACMD5' => 'RIgp8DurFh1hIUOmqHTCcuK7G7GuoUQc0SP1WhasUh5ZwNHFGBPpc7ZvinlSzNAqeqMOc1Nf+FrVh2hMJEszeQ==',
  14. // 'AES' => 'M5E1i/s1u4BWsocqG7zC3Q==',
  15. 'appCode' => '1A2715D230724895AFB318FE8919244A',
  16. 'HMACMD5' => 'wi/iBAuOWhfn63nEw8/yi2g1kypvdc1X7qt901z1G+uZN2JzdW0SwsxBwYDGYBDW+etr2i4V/rLpXp5yQb9BBg==',
  17. 'AES' => 'uH6eH0iGoFsgsfOHOF/kUw=='
  18. ];
  19. /**
  20. * 正确返回
  21. *
  22. * @param array $data
  23. * @param string $message
  24. * @param integer $code
  25. * @return void
  26. */
  27. public static function success($data = [], $message = '', $code = 0)
  28. {
  29. $result = compact('code', 'message', 'data');
  30. return $result;
  31. }
  32. /**
  33. * 错误返回
  34. *
  35. * @param string $message
  36. * @param integer $code
  37. * @param array $data
  38. * @return void
  39. */
  40. public static function error($message = '', $code = 1, $data = [])
  41. {
  42. $result = compact('code', 'message', 'data');
  43. return $result;
  44. }
  45. /**
  46. * uuid 唯一序列码
  47. * @param string $prefix
  48. *
  49. * @return string
  50. */
  51. public static function uuid($prefix = '')
  52. {
  53. $chars = md5(uniqid(mt_rand(), true));
  54. $uuid = substr($chars, 0, 8) . '-';
  55. $uuid .= substr($chars, 8, 4) . '-';
  56. $uuid .= substr($chars, 12, 4) . '-';
  57. $uuid .= substr($chars, 16, 4) . '-';
  58. $uuid .= substr($chars, 20, 12);
  59. return $prefix . $uuid;
  60. }
  61. /**
  62. * 获取当前时间毫秒
  63. *
  64. * @return float
  65. */
  66. public static function msectime()
  67. {
  68. list($msec, $sec) = explode(' ', microtime());
  69. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  70. return $msectime;
  71. }
  72. /**
  73. * 获取时间戳
  74. *
  75. * @return string
  76. */
  77. public static function getTimestamp()
  78. {
  79. date_default_timezone_set('PRC');
  80. $time = self::msectime();
  81. return date('Y-m-d H:i:s.') . substr($time, -3);
  82. }
  83. /**
  84. * 生成签名
  85. *
  86. * @param string $uuid
  87. * @param string $timestamp
  88. * @return string
  89. */
  90. public static function genSign($uuid, $timestamp)
  91. {
  92. $appCode = self::ZOP_KEY['appCode'] ?? '';
  93. $hmacMd5 = self::ZOP_KEY['HMACMD5'] ?? '';
  94. $dataStr = 'appCode' . $appCode . 'timestamp' . $timestamp . 'uuid' . $uuid . $hmacMd5;
  95. return self::sign($hmacMd5, $dataStr);
  96. }
  97. /**
  98. * 签名
  99. * @param key 密钥 平台侧提供的HmacMD5
  100. * @param dataStr appCode+appCode值+timestamp+timestamp值*+uuid+uuid值+HmacMD5密钥值
  101. *
  102. * @return string
  103. */
  104. public static function sign($key, $dataStr)
  105. {
  106. return base64_encode(hash_hmac("md5", $dataStr, base64_decode($key), true));
  107. }
  108. /**
  109. * AES加密
  110. *
  111. * @param string $params
  112. * @param string $key
  113. *
  114. * @return string
  115. */
  116. public static function encrypt($params, $key)
  117. {
  118. if (is_array($params)) {
  119. $params = json_encode($params);
  120. }
  121. $data = openssl_encrypt($params, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
  122. return base64_encode($data);
  123. }
  124. /**
  125. * 发送请求
  126. *
  127. * @param string $url
  128. * @param array $postData
  129. * @param boolean $isPost
  130. *
  131. * @return string
  132. */
  133. public static function sendRequest($url, $postData, $isPost = true)
  134. {
  135. $optData = json_encode($postData);
  136. $header = array();
  137. $header[] = 'Accept:application/json';
  138. $header[] = 'Content-Type:application/json;charset=utf-8';
  139. $curl = curl_init(); //初始化
  140. curl_setopt($curl, CURLOPT_URL, $url); //设置url
  141. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  142. curl_setopt($curl, CURLOPT_POSTFIELDS, $optData);
  143. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  144. if ($isPost) {
  145. curl_setopt($curl, CURLOPT_POST, 1);
  146. }
  147. // 判断地址是不是https
  148. if (stripos($url, 'https://') !== FALSE) {
  149. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  150. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  151. }
  152. $result = curl_exec($curl);
  153. if($result === false){
  154. common_log('发起请求失败, 失败地址为: ' . $url . ', 失败原因为: ' . curl_error($curl) . ', 失败参数为: ' . json_encode($postData, JSON_UNESCAPED_UNICODE));
  155. return false;
  156. }
  157. curl_close($curl);
  158. return $result;
  159. }
  160. /**
  161. * 发送请求
  162. *
  163. * @param string $urlPath
  164. * @param array $params
  165. * @return string
  166. */
  167. public static function send($urlPath = '', $params = [], $order = null)
  168. {
  169. // 数据获取
  170. $baseUrl = self::ZOP_URL;
  171. $aes = self::ZOP_KEY['AES'] ?? '';
  172. $appCode = self::ZOP_KEY['appCode'] ?? '';
  173. // 准备参数
  174. $url = $baseUrl . $urlPath;
  175. // uuid
  176. $uuid = self::uuid();
  177. // 生成时间戳
  178. $timestamp = self::getTimestamp();
  179. $head = [
  180. 'sign' => self::genSign($uuid, $timestamp),
  181. 'timestamp' => $timestamp,
  182. 'uuid' => $uuid
  183. ];
  184. $reqObjBase = [
  185. 'head' => $head,
  186. 'body' => $params
  187. ];
  188. $reqObj = self::encrypt($reqObjBase, $aes);
  189. $reqParam = [
  190. 'appCode' => $appCode,
  191. 'reqObj' => $reqObj
  192. ];
  193. $result = self::sendRequest($url, $reqParam);
  194. if ($urlPath != '/link/king/resource/upload/v1') {
  195. common_log('请求地址: ' . $urlPath);
  196. common_log('请求参数: '. json_encode($params, JSON_UNESCAPED_UNICODE));
  197. common_log('请求结果: '. $result);
  198. }
  199. // 判断result是否有报错
  200. if (empty($result)) {
  201. return $result;
  202. }
  203. $resArr = json_decode($result, true);
  204. if ($urlPath == '/link/king/identity/cust/v2') {
  205. if ($resArr['aCode'] != '0000' || $resArr['bCode']!= '0000') {
  206. $order->original_import_params = json_encode($reqObjBase, JSON_UNESCAPED_UNICODE);
  207. $order->import_params = json_encode($reqParam);
  208. $order->export_params = $result;
  209. $order->save();
  210. }
  211. } else {
  212. if ($resArr['rspCode'] != '0000') {
  213. $order->original_import_params = json_encode($reqObjBase, JSON_UNESCAPED_UNICODE);
  214. $order->import_params = json_encode($reqParam);
  215. $order->export_params = $result;
  216. $order->save();
  217. }
  218. }
  219. return $result;
  220. }
  221. }