fixed
This commit is contained in:
98
superadmin/application/api/logic/BaseWechatLogic.php
Normal file
98
superadmin/application/api/logic/BaseWechatLogic.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2025/3/22} {14:06}
|
||||
*/
|
||||
|
||||
namespace app\api\logic;
|
||||
|
||||
|
||||
use think\Db;
|
||||
|
||||
class BaseWechatLogic
|
||||
{
|
||||
public $base_url = '';
|
||||
|
||||
function setBaseUrlByAiCustmerConfig($ai_config_id){
|
||||
//$ai_config_id = input('ai_config_id');
|
||||
$conf = Db::table('fa_aicustomerservice')
|
||||
->where('id',$ai_config_id)
|
||||
->find();
|
||||
if(empty($conf)){
|
||||
throw new \Exception('ai客服配置不存在');
|
||||
}
|
||||
if(empty($conf['server_url'])){
|
||||
throw new \Exception('ai客服配置的server_url不能为空');
|
||||
}
|
||||
$this->base_url = $conf['server_url'];
|
||||
return $conf['server_url'];
|
||||
}
|
||||
function setBaseUrl($base_url){
|
||||
$this->base_url = $base_url;
|
||||
}
|
||||
|
||||
function curl_post_json($url, array $postdata, $header=[]) {
|
||||
if(empty($this->base_url)){
|
||||
throw new \Exception('请先设置ai客服配置');
|
||||
}
|
||||
$tokenArr = (new TokenWechatLogic())->getToken();
|
||||
$postdata['appId'] = $tokenArr['appId'];
|
||||
$domain = $this->base_url;
|
||||
$timeout = 4;
|
||||
$connect_timeout = 1;
|
||||
$set_time_limit = 5;
|
||||
if($timeout + $connect_timeout < $set_time_limit) throw new \Exception('脚本超时值必须大于等于连接超时与请求处理超时之和');
|
||||
set_time_limit($set_time_limit);
|
||||
$header = array_merge($header, array(
|
||||
'X-GEWE-TOKEN:' .$tokenArr['token'],
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json'
|
||||
));
|
||||
|
||||
//初始化
|
||||
$curl = curl_init();
|
||||
//设置抓取的url
|
||||
curl_setopt($curl, CURLOPT_URL, $domain.$url);
|
||||
//设置头文件的信息作为数据流输出
|
||||
curl_setopt($curl, CURLOPT_HEADER, 0);
|
||||
//设置获取的信息以文件流的形式返回,而不是直接输出。
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
// 超时设置
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
|
||||
//发起连接前等待的时间,如果设置为0,则无限等待。
|
||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
|
||||
|
||||
// 超时设置,以毫秒为单位
|
||||
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
|
||||
|
||||
// 设置请求头
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||||
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
|
||||
|
||||
//设置post方式提交
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postdata,JSON_UNESCAPED_UNICODE));
|
||||
//执行命令
|
||||
$data = curl_exec($curl);
|
||||
|
||||
// 显示错误信息
|
||||
if (curl_error($curl)) {
|
||||
throw new \Exception('cURL error: ' . curl_error($curl));
|
||||
//返回错误码
|
||||
//return ['code'=>curl_errno($curl), 'msg'=>curl_error($curl)];
|
||||
} else {
|
||||
//关闭句柄
|
||||
curl_close($curl);
|
||||
// 返回的内容
|
||||
$res = json_decode($data, true);
|
||||
if($res['ret'] != 200){
|
||||
throw new \Exception($res['msg']);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user