123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace app\common\service;
- use think\Log;
- /**
- * 订单推送基础服务类
- */
- class ZopBaseService
- {
- // const ZOP_URL = 'https://demo.mall.10010.com:8104/zop';
- const ZOP_URL = 'https://cd.10010.com/zop';
- const ZOP_KEY = [
- // 'appCode' => '36F4FB0764884970939A8962AAAED993',
- // 'HMACMD5' => 'RIgp8DurFh1hIUOmqHTCcuK7G7GuoUQc0SP1WhasUh5ZwNHFGBPpc7ZvinlSzNAqeqMOc1Nf+FrVh2hMJEszeQ==',
- // 'AES' => 'M5E1i/s1u4BWsocqG7zC3Q==',
- 'appCode' => '1A2715D230724895AFB318FE8919244A',
- 'HMACMD5' => 'wi/iBAuOWhfn63nEw8/yi2g1kypvdc1X7qt901z1G+uZN2JzdW0SwsxBwYDGYBDW+etr2i4V/rLpXp5yQb9BBg==',
- 'AES' => 'uH6eH0iGoFsgsfOHOF/kUw=='
- ];
- /**
- * 正确返回
- *
- * @param array $data
- * @param string $message
- * @param integer $code
- * @return void
- */
- public static function success($data = [], $message = '', $code = 0)
- {
- $result = compact('code', 'message', 'data');
- return $result;
- }
- /**
- * 错误返回
- *
- * @param string $message
- * @param integer $code
- * @param array $data
- * @return void
- */
- public static function error($message = '', $code = 1, $data = [])
- {
- $result = compact('code', 'message', 'data');
- return $result;
- }
- /**
- * uuid 唯一序列码
- * @param string $prefix
- *
- * @return string
- */
- public static function uuid($prefix = '')
- {
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr($chars, 0, 8) . '-';
- $uuid .= substr($chars, 8, 4) . '-';
- $uuid .= substr($chars, 12, 4) . '-';
- $uuid .= substr($chars, 16, 4) . '-';
- $uuid .= substr($chars, 20, 12);
- return $prefix . $uuid;
- }
- /**
- * 获取当前时间毫秒
- *
- * @return float
- */
- public static function msectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- /**
- * 获取时间戳
- *
- * @return string
- */
- public static function getTimestamp()
- {
- date_default_timezone_set('PRC');
- $time = self::msectime();
- return date('Y-m-d H:i:s.') . substr($time, -3);
- }
- /**
- * 生成签名
- *
- * @param string $uuid
- * @param string $timestamp
- * @return string
- */
- public static function genSign($uuid, $timestamp)
- {
- $appCode = self::ZOP_KEY['appCode'] ?? '';
- $hmacMd5 = self::ZOP_KEY['HMACMD5'] ?? '';
- $dataStr = 'appCode' . $appCode . 'timestamp' . $timestamp . 'uuid' . $uuid . $hmacMd5;
- return self::sign($hmacMd5, $dataStr);
- }
- /**
- * 签名
- * @param key 密钥 平台侧提供的HmacMD5
- * @param dataStr appCode+appCode值+timestamp+timestamp值*+uuid+uuid值+HmacMD5密钥值
- *
- * @return string
- */
- public static function sign($key, $dataStr)
- {
- return base64_encode(hash_hmac("md5", $dataStr, base64_decode($key), true));
- }
- /**
- * AES加密
- *
- * @param string $params
- * @param string $key
- *
- * @return string
- */
- public static function encrypt($params, $key)
- {
- if (is_array($params)) {
- $params = json_encode($params);
- }
- $data = openssl_encrypt($params, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
- return base64_encode($data);
- }
- /**
- * 发送请求
- *
- * @param string $url
- * @param array $postData
- * @param boolean $isPost
- *
- * @return string
- */
- public static function sendRequest($url, $postData, $isPost = true)
- {
- $optData = json_encode($postData);
- $header = array();
- $header[] = 'Accept:application/json';
- $header[] = 'Content-Type:application/json;charset=utf-8';
- $curl = curl_init(); //初始化
- curl_setopt($curl, CURLOPT_URL, $url); //设置url
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $optData);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- if ($isPost) {
- curl_setopt($curl, CURLOPT_POST, 1);
- }
- // 判断地址是不是https
- if (stripos($url, 'https://') !== FALSE) {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
- }
- $result = curl_exec($curl);
- if($result === false){
- common_log('发起请求失败, 失败地址为: ' . $url . ', 失败原因为: ' . curl_error($curl) . ', 失败参数为: ' . json_encode($postData, JSON_UNESCAPED_UNICODE));
- return false;
- }
- curl_close($curl);
- return $result;
- }
-
- /**
- * 发送请求
- *
- * @param string $urlPath
- * @param array $params
- * @return string
- */
- public static function send($urlPath = '', $params = [], $order = null)
- {
- // 数据获取
- $baseUrl = self::ZOP_URL;
- $aes = self::ZOP_KEY['AES'] ?? '';
- $appCode = self::ZOP_KEY['appCode'] ?? '';
-
- // 准备参数
- $url = $baseUrl . $urlPath;
- // uuid
- $uuid = self::uuid();
- // 生成时间戳
- $timestamp = self::getTimestamp();
- $head = [
- 'sign' => self::genSign($uuid, $timestamp),
- 'timestamp' => $timestamp,
- 'uuid' => $uuid
- ];
- $reqObjBase = [
- 'head' => $head,
- 'body' => $params
- ];
- $reqObj = self::encrypt($reqObjBase, $aes);
- $reqParam = [
- 'appCode' => $appCode,
- 'reqObj' => $reqObj
- ];
- $result = self::sendRequest($url, $reqParam);
- if ($urlPath != '/link/king/resource/upload/v1') {
- common_log('请求地址: ' . $urlPath);
- common_log('请求参数: '. json_encode($params, JSON_UNESCAPED_UNICODE));
- common_log('请求结果: '. $result);
- }
- // 判断result是否有报错
- if (empty($result)) {
- return $result;
- }
- $resArr = json_decode($result, true);
- if ($urlPath == '/link/king/identity/cust/v2') {
- if ($resArr['aCode'] != '0000' || $resArr['bCode']!= '0000') {
- $order->original_import_params = json_encode($reqObjBase, JSON_UNESCAPED_UNICODE);
- $order->import_params = json_encode($reqParam);
- $order->export_params = $result;
- $order->save();
- }
- } else {
- if ($resArr['rspCode'] != '0000') {
- $order->original_import_params = json_encode($reqObjBase, JSON_UNESCAPED_UNICODE);
- $order->import_params = json_encode($reqParam);
- $order->export_params = $result;
- $order->save();
- }
- }
- return $result;
- }
- }
|