WDTService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. class WDTService extends Service
  5. {
  6. private $appSecret = "";
  7. private $apiParas = [];
  8. private $config = [];
  9. public function __construct($wdt = 0)
  10. {
  11. $config = config("wdt")[$wdt] ?? [];
  12. if(empty($config["sid"])) throw new \Exception("旺店通sid不能为空");
  13. if(empty($config["appKey"])) throw new \Exception("旺店通appKey不能为空");
  14. if(empty($config["appSecret"])) throw new \Exception("旺店通appSecret不能为空");
  15. $this->config = $config;
  16. $this->appSecret = $config['appSecret'];
  17. $this->apiParas = [
  18. 'sid' => $config['sid'],
  19. 'appkey' => $config['appKey'],
  20. 'timestamp' => time(),
  21. ];
  22. }
  23. private function makeSign(&$apiParas, $appSecret){
  24. $sign = md5($this->packData($apiParas) . $appSecret);
  25. $apiParas['sign'] = $sign;
  26. }
  27. private function packData(&$req)
  28. {
  29. ksort($req);
  30. $arr = array();
  31. foreach($req as $key => $val)
  32. {
  33. if($key == 'sign') continue;
  34. if(count($arr))
  35. $arr[] = ';';
  36. $arr[] = sprintf("%02d", iconv_strlen($key, 'UTF-8'));
  37. $arr[] = '-';
  38. $arr[] = $key;
  39. $arr[] = ':';
  40. $arr[] = sprintf("%04d", iconv_strlen($val, 'UTF-8'));
  41. $arr[] = '-';
  42. $arr[] = $val;
  43. }
  44. return implode('', $arr);
  45. }
  46. private function putMultiApiParam($params)
  47. {
  48. return array_merge($this->apiParas,$params);
  49. }
  50. private function organizationParam($data){
  51. $params = $this->putMultiApiParam($data);
  52. $this->makeSign($params, $this->appSecret);
  53. $postData = http_build_query($params,'','&');
  54. return $postData;
  55. }
  56. public function getOrderListAuto($data){
  57. $timeStamp = time();
  58. $data['start_time'] = date("Y-m-d H:i:s", $timeStamp - 60 * 60);
  59. $data['end_time'] = date("Y-m-d H:i:s", $timeStamp);
  60. $postData = $this->organizationParam($data);
  61. list($status, $msg) = $this->post_wdt_helper($this->config['order_query'], $postData);
  62. dd($status, $msg);
  63. }
  64. //发送请求
  65. public function post_wdt_helper($url, $data, $timeout = 20){
  66. $header = array("Content-Type: application/x-www-form-urlencoded");
  67. Log::channel('apiLog')->info('旺店通POST', ["api" => $url , "param" => $data ,"header" => $header]);
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_URL, $url);
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($ch, CURLOPT_ENCODING, '');
  72. curl_setopt($ch, CURLOPT_POST, 1);
  73. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  74. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  76. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  77. if(! is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  78. $r = curl_exec($ch);
  79. if ($r === false) {
  80. // 获取错误号
  81. $errorNumber = curl_errno($ch);
  82. // 获取错误信息
  83. $errorMessage = curl_error($ch);
  84. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  85. Log::channel('apiLog')->info('旺店通POST结果', ["message" => $message]);
  86. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  87. }
  88. curl_close($ch);
  89. Log::channel('apiLog')->info('旺店通POST结果', ["message" => json_decode($r, true)]);
  90. return [true, json_decode($r, true)];
  91. }
  92. }