DaHuangFengService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\CustomerFromThreePlatForm;
  5. use App\Model\Product;
  6. use App\Model\SalesOrder;
  7. use App\Model\SalesOrderInfo;
  8. use App\Model\SalesOrderProductInfo;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. class DaHuangFengService extends Service
  13. {
  14. public $appKey = 'b993bb5a17ce4d2db6cb038f02c9d646';
  15. public $appSecret = '054f5839ac624572916fc661f1412060';
  16. public $host = 'https://open.byering.com/';
  17. //聊天信息
  18. public $url_order_detail = 'saas/open/api/chat/getChatFlow';
  19. public function getOrderDetail($data){
  20. if(empty($data['customer_id'])) return [false, "客户ID不能为空"];
  21. $plat_customer = CustomerFromThreePlatForm::where('del_time',3)
  22. ->where('customer_id',$data['customer_id'])
  23. ->first();
  24. if(empty($plat_customer)) return [false, "第三方客户信息不存在,无法获取到聊天信息"];
  25. $plat_customer = $plat_customer->toArray();
  26. $cursor = $data['cursor'] ?? 0;
  27. $pageSize = $data['pageSize'] ?? 20;
  28. $timestamp = time();
  29. $param['appKey'] = $this->appKey;
  30. $param['timestamp'] = $timestamp;
  31. $data = [
  32. 'clueId' => $plat_customer['clueId'],
  33. 'userOpenId' => $plat_customer['fromUserId'],
  34. 'cursor' => $cursor,
  35. 'pageSize' => $pageSize,
  36. ];
  37. // $data = ["clueId" => "558a9a7e-a17c-4672-b67d-8788b6356f2e", "userOpenId" => "6f7e4ef2-c43b-4c79-9b39-ac97aa77f2ee", "cursor" => 0, "pageSize" => 20];
  38. $json_data = json_encode($data);
  39. $param['data'] = $json_data;
  40. $sign = $this->makeSign($param);
  41. $param['sign'] = $sign;
  42. list($status, $msg) = $this->post_big($this->host . $this->url_order_detail, $param);
  43. if(! $status) return [false, $msg];
  44. return [true, $msg];
  45. }
  46. public function curlForOrderDetail($data){
  47. }
  48. public function makeSign($params)
  49. {
  50. // 1. 去掉 sign 字段
  51. unset($params['sign']);
  52. // 2. 按字段名升序排序
  53. ksort($params);
  54. // 3. 拼接成 key=value&key=value...
  55. $paramsStr = '';
  56. foreach ($params as $key => $value) {
  57. $paramsStr .= ($paramsStr === '' ? '' : '&') . $key . '=' . $value;
  58. }
  59. // 4. Base64 编码
  60. $base64Str = base64_encode($paramsStr);
  61. // 5. appSecret:Base64Str 拼接
  62. $strToEncrypt = $this->appSecret . ':' . $base64Str;
  63. // 6. SHA1 加密
  64. return sha1($strToEncrypt);
  65. }
  66. public function getSign($timestamp, $data){
  67. $appKey = $this->appKey;
  68. $appSecret = $this->appSecret;
  69. $params = [
  70. 'appKey' => $appKey,
  71. 'timestamp'=> (string)$timestamp,
  72. 'data' => $data,
  73. ];
  74. ksort($params);
  75. $paramsStr = '';
  76. foreach ($params as $key => $value) {
  77. $paramsStr .= ($paramsStr === '' ? '' : '&') . $key . '=' . $value;
  78. }
  79. // echo "拼接结果: {$paramsStr}\n";
  80. $base64Str = base64_encode($paramsStr);
  81. $strToEncrypt = $appSecret . ':' . $base64Str;
  82. $sign = sha1($strToEncrypt);
  83. // echo "sign: {$sign}\n";
  84. return $sign;
  85. }
  86. //发送请求
  87. public function post_big($url, $data, $timeout = 20){
  88. $header = array("Content-type:application/json;charset='utf-8'");
  89. Log::channel('apiLog')->info('bigPOST', ["api" => $url , "param" => $data ,"header" => $header]);
  90. $ch = curl_init();
  91. curl_setopt($ch, CURLOPT_URL, $url);
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  93. curl_setopt($ch, CURLOPT_ENCODING, '');
  94. curl_setopt($ch, CURLOPT_POST, 1);
  95. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  98. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  99. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  100. $r = curl_exec($ch);
  101. if ($r === false) {
  102. // 获取错误号
  103. $errorNumber = curl_errno($ch);
  104. // 获取错误信息
  105. $errorMessage = curl_error($ch);
  106. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  107. Log::channel('apiLog')->info('bigPOST结果', ["message" => $message]);
  108. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  109. }
  110. curl_close($ch);
  111. Log::channel('apiLog')->info('bigPOST结果', ["message" => json_decode($r, true)]);
  112. return [true, json_decode($r, true)];
  113. }
  114. }