api接口提交

This commit is contained in:
wong
2025-06-19 15:14:41 +08:00
parent fbaf253302
commit beb676e0ce
11 changed files with 708 additions and 286 deletions

View File

@@ -11,6 +11,86 @@ use think\Db;
*/
class GetAddFriendPlanDetailV1Controller extends Controller
{
/**
* 生成签名
*
* @param array $params 参数数组
* @param string $apiKey API密钥
* @return string
*/
private function generateSignature($params, $apiKey)
{
// 1. 移除sign和apiKey
unset($params['sign'], $params['apiKey']);
// 2. 移除空值
$params = array_filter($params, function($value) {
return !is_null($value) && $value !== '';
});
// 3. 参数按键名升序排序
ksort($params);
// 4. 直接拼接参数值
$stringToSign = implode('', array_values($params));
// 5. 第一次MD5加密
$firstMd5 = md5($stringToSign);
// 6. 拼接apiKey并第二次MD5加密
return md5($firstMd5 . $apiKey);
}
/**
* 生成测试URL
*
* @param string $apiKey API密钥
* @return array
*/
public function testUrl($apiKey)
{
try {
if (empty($apiKey)) {
return [];
}
// 构建测试参数
$testParams = [
'name' => '测试客户',
'phone' => '18888888888',
'apiKey' => $apiKey,
'timestamp' => time()
];
// 生成签名
$sign = $this->generateSignature($testParams, $apiKey);
$testParams['sign'] = $sign;
// 构建签名过程说明
$signParams = $testParams;
unset($signParams['sign'], $signParams['apiKey']);
ksort($signParams);
$signStr = implode('', array_values($signParams));
// 构建完整URL参数不对中文进行编码
$urlParams = [];
foreach ($testParams as $key => $value) {
$urlParams[] = $key . '=' . $value;
}
$fullUrl = implode('&', $urlParams);
return [
'apiKey' => $apiKey,
'originalString' => $signStr,
'sign' => $sign,
'fullUrl' => $fullUrl
];
} catch (\Exception $e) {
return [];
}
}
/**
* 获取计划详情
*
@@ -37,11 +117,14 @@ class GetAddFriendPlanDetailV1Controller extends Controller
// 解析JSON字段
$sceneConf = json_decode($plan['sceneConf'], true) ?: [];
$reqConf = json_decode($plan['reqConf'], true) ?: [];
$msgConf= json_decode($plan['msgConf'], true) ?: [];
$msgConf = json_decode($plan['msgConf'], true) ?: [];
$tagConf = json_decode($plan['tagConf'], true) ?: [];
// 合并数据
$newData['messagePlans'] = $msgConf;
$newData = array_merge($newData,$sceneConf,$reqConf,$tagConf,$plan);
$newData = array_merge($newData, $sceneConf, $reqConf, $tagConf, $plan);
// 移除不需要的字段
unset(
$newData['sceneConf'],
$newData['reqConf'],
@@ -50,10 +133,11 @@ class GetAddFriendPlanDetailV1Controller extends Controller
$newData['userInfo'],
$newData['createTime'],
$newData['updateTime'],
$newData['deleteTime'],
$newData['deleteTime']
);
// 生成测试URL
$newData['textUrl'] = $this->testUrl($newData['apiKey']);
return ResponseHelper::success($newData, '获取计划详情成功');

View File

@@ -0,0 +1,125 @@
<?php
namespace app\cunkebao\controller\plan;
use library\ResponseHelper;
use think\Controller;
use think\Db;
/**
* 对外API接口控制器
*/
class PostExternalApiV1Controller extends Controller
{
/**
* 验证签名
*
* @param array $params 请求参数
* @param string $apiKey API密钥
* @param string $sign 签名
* @return bool
*/
private function validateSign($params, $apiKey, $sign)
{
// 1. 从参数中移除sign和apiKey
unset($params['sign'], $params['apiKey']);
// 2. 移除空值
$params = array_filter($params, function($value) {
return !is_null($value) && $value !== '';
});
// 3. 参数按键名升序排序
ksort($params);
// 4. 直接拼接参数值
$stringToSign = implode('', array_values($params));
// 5. 第一次MD5加密
$firstMd5 = md5($stringToSign);
// 6. 拼接apiKey并第二次MD5加密
$expectedSign = md5($firstMd5 . $apiKey);
// 7. 比对签名
return $expectedSign === $sign;
}
/**
* 对外API接口入口
*
* @return \think\response\Json
*/
public function index()
{
try {
$params = $this->request->param();
// 验证必填参数
if (empty($params['apiKey'])) {
return ResponseHelper::error('apiKey不能为空', 400);
}
if (empty($params['sign'])) {
return ResponseHelper::error('sign不能为空', 400);
}
if (empty($params['timestamp'])) {
return ResponseHelper::error('timestamp不能为空', 400);
}
// 验证时间戳允许5分钟误差
if (abs(time() - intval($params['timestamp'])) > 300) {
return ResponseHelper::error('请求已过期', 400);
}
// 查询API密钥是否存在
$plan = Db::name('customer_acquisition_task')
->where('apiKey', $params['apiKey'])
->where('status', 1)
->find();
if (!$plan) {
return ResponseHelper::error('无效的apiKey', 401);
}
// 验证签名
if (!$this->validateSign($params,$params['apiKey'], $params['sign'])) {
return ResponseHelper::error('签名验证失败', 401);
}
$identifier = !empty($params['wechatId']) ? $params['wechatId'] : $params['phone'];
$trafficPool = Db::name('traffic_pool')->where('identifier', $identifier)->find();
if (!$trafficPool) {
Db::name('traffic_pool')->insert([
'identifier' => $identifier,
'mobile' => $params['phone']
]);
}
$taskCustomer = Db::name('task_customer')->where('task_id', $plan['id'])->where('phone', $identifier)->find();
if (!$taskCustomer) {
Db::name('task_customer')->insert([
'task_id' => $plan['id'],
'phone' => $identifier
]);
return json([
'code' => 200,
'message' => '新增成功',
'data' => $identifier
]);
}else{
return json([
'code' => 200,
'message' => '已存在',
'data' => $identifier
]);
}
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
}