123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\CustomerFromThreePlatForm;
- use App\Model\Product;
- use App\Model\SalesOrder;
- use App\Model\SalesOrderInfo;
- use App\Model\SalesOrderProductInfo;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class DaHuangFengService extends Service
- {
- public $appKey = 'b993bb5a17ce4d2db6cb038f02c9d646';
- public $appSecret = '054f5839ac624572916fc661f1412060';
- public $host = 'https://open.byering.com/';
- //聊天信息
- public $url_order_detail = 'saas/open/api/chat/getChatFlow';
- public function getOrderDetail($data){
- if(empty($data['customer_id'])) return [false, "客户ID不能为空"];
- $plat_customer = CustomerFromThreePlatForm::where('del_time',3)
- ->where('customer_id',$data['customer_id'])
- ->first();
- if(empty($plat_customer)) return [false, "第三方客户信息不存在,无法获取到聊天信息"];
- $plat_customer = $plat_customer->toArray();
- $cursor = $data['cursor'] ?? 0;
- $pageSize = $data['pageSize'] ?? 20;
- $timestamp = time();
- $param['appKey'] = $this->appKey;
- $param['timestamp'] = $timestamp;
- $data = [
- 'clueId' => $plat_customer['clueId'],
- 'userOpenId' => $plat_customer['fromUserId'],
- 'cursor' => $cursor,
- 'pageSize' => $pageSize,
- ];
- // $data = ["clueId" => "558a9a7e-a17c-4672-b67d-8788b6356f2e", "userOpenId" => "6f7e4ef2-c43b-4c79-9b39-ac97aa77f2ee", "cursor" => 0, "pageSize" => 20];
- $json_data = json_encode($data);
- $param['data'] = $json_data;
- $sign = $this->makeSign($param);
- $param['sign'] = $sign;
- list($status, $msg) = $this->post_big($this->host . $this->url_order_detail, $param);
- if(! $status) return [false, $msg];
- return [true, $msg];
- }
- public function curlForOrderDetail($data){
- }
- public function makeSign($params)
- {
- // 1. 去掉 sign 字段
- unset($params['sign']);
- // 2. 按字段名升序排序
- ksort($params);
- // 3. 拼接成 key=value&key=value...
- $paramsStr = '';
- foreach ($params as $key => $value) {
- $paramsStr .= ($paramsStr === '' ? '' : '&') . $key . '=' . $value;
- }
- // 4. Base64 编码
- $base64Str = base64_encode($paramsStr);
- // 5. appSecret:Base64Str 拼接
- $strToEncrypt = $this->appSecret . ':' . $base64Str;
- // 6. SHA1 加密
- return sha1($strToEncrypt);
- }
- public function getSign($timestamp, $data){
- $appKey = $this->appKey;
- $appSecret = $this->appSecret;
- $params = [
- 'appKey' => $appKey,
- 'timestamp'=> (string)$timestamp,
- 'data' => $data,
- ];
- ksort($params);
- $paramsStr = '';
- foreach ($params as $key => $value) {
- $paramsStr .= ($paramsStr === '' ? '' : '&') . $key . '=' . $value;
- }
- // echo "拼接结果: {$paramsStr}\n";
- $base64Str = base64_encode($paramsStr);
- $strToEncrypt = $appSecret . ':' . $base64Str;
- $sign = sha1($strToEncrypt);
- // echo "sign: {$sign}\n";
- return $sign;
- }
- //发送请求
- public function post_big($url, $data, $timeout = 20){
- $header = array("Content-type:application/json;charset='utf-8'");
- Log::channel('apiLog')->info('bigPOST', ["api" => $url , "param" => $data ,"header" => $header]);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_ENCODING, '');
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- $r = curl_exec($ch);
- if ($r === false) {
- // 获取错误号
- $errorNumber = curl_errno($ch);
- // 获取错误信息
- $errorMessage = curl_error($ch);
- $message = "cURL Error #{$errorNumber}: {$errorMessage}";
- Log::channel('apiLog')->info('bigPOST结果', ["message" => $message]);
- return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
- }
- curl_close($ch);
- Log::channel('apiLog')->info('bigPOST结果', ["message" => json_decode($r, true)]);
- return [true, json_decode($r, true)];
- }
- }
|