流量分发列表 + 添加 + 修改状态功能提交

This commit is contained in:
wong
2025-06-16 15:42:36 +08:00
parent 9e89e3db9c
commit 51beeee212
14 changed files with 1423 additions and 645 deletions

View File

@@ -0,0 +1,135 @@
<?php
namespace app\cunkebao\controller\plan;
use library\ResponseHelper;
use think\Db;
use app\cunkebao\controller\BaseController;
/**
* 获取计划任务列表控制器
*/
class GetCreateAddFriendPlanV1Controller extends BaseController
{
/**
* 生成唯一API密钥
*
* @return string
*/
private function generateApiKey()
{
// 生成6组随机字符串每组5个字符
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$apiKey = '';
for ($i = 0; $i < 6; $i++) {
$segment = '';
for ($j = 0; $j < 5; $j++) {
$segment .= $chars[mt_rand(0, strlen($chars) - 1)];
}
$apiKey .= ($i > 0 ? '-' : '') . $segment;
}
// 检查是否已存在
$exists = Db::name('customer_acquisition_task')
->where('apiKey', $apiKey)
->find();
if ($exists) {
// 如果已存在,递归重新生成
return $this->generateApiKey();
}
return $apiKey;
}
/**
* 拷贝计划任务
*
* @return \think\response\Json
*/
public function copy()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$plan = Db::name('customer_acquisition_task')->where('id', $planId)->find();
if (!$plan) {
return ResponseHelper::error('计划不存在', 404);
}
unset($plan['id']);
$plan['name'] = $plan['name'] . ' (拷贝)';
$plan['createTime'] = time();
$plan['updateTime'] = time();
$plan['apiKey'] = $this->generateApiKey(); // 生成新的API密钥
$newPlanId = Db::name('customer_acquisition_task')->insertGetId($plan);
if (!$newPlanId) {
return ResponseHelper::error('拷贝计划失败', 500);
}
return ResponseHelper::success(['planId' => $newPlanId], '拷贝计划任务成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
/**
* 删除计划任务
*
* @return \think\response\Json
*/
public function delete()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$result = Db::name('customer_acquisition_task')->where('id', $planId)->update(['deleteTime' => time()]);
if (!$result) {
return ResponseHelper::error('删除计划失败', 500);
}
return ResponseHelper::success([], '删除计划任务成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
/**
* 修改计划任务状态
*
* @return \think\response\Json
*/
public function updateStatus()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
$status = isset($params['status']) ? intval($params['status']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$result = Db::name('customer_acquisition_task')->where('id', $planId)->update(['status' => $status, 'updateTime' => time()]);
if (!$result) {
return ResponseHelper::error('修改计划状态失败', 500);
}
return ResponseHelper::success([], '修改计划任务状态成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
}

View File

@@ -15,19 +15,48 @@ class GetPlanSceneListV1Controller extends BaseController
/**
* 获取开启的场景列表
*
* @param array $params 查询参数
* @return array
*/
protected function getSceneList(): array
protected function getSceneList(array $params = []): array
{
$list = PlansSceneModel::where(['status' => PlansSceneModel::STATUS_ACTIVE])->order('sort desc')->select()->toArray();
$userInfo = $this->getUserInfo();
foreach($list as &$val){
$val['scenarioTags'] = json_decode($val['scenarioTags'],true);
$val['count'] = 0;
$val['growth'] = "0%";
try {
// 构建查询条件
$where = ['status' => PlansSceneModel::STATUS_ACTIVE];
// 搜索条件
if (!empty($params['keyword'])) {
$where[] = ['name', 'like', '%' . $params['keyword'] . '%'];
}
// 标签筛选
if (!empty($params['tag'])) {
$where[] = ['scenarioTags', 'like', '%' . $params['tag'] . '%'];
}
// 查询数据
$query = PlansSceneModel::where($where);
// 获取总数
$total = $query->count();
// 获取分页数据
$list = $query->order('sort DESC')->select()->toArray();
// 处理数据
foreach($list as &$val) {
$val['scenarioTags'] = json_decode($val['scenarioTags'], true);
$val['count'] = $this->getPlanCount($val['id']);
$val['growth'] = $this->calculateGrowth($val['id']);
}
unset($val);
return $list;
} catch (\Exception $e) {
throw new \Exception('获取场景列表失败:' . $e->getMessage());
}
unset($val);
return $list;
}
/**
@@ -37,31 +66,90 @@ class GetPlanSceneListV1Controller extends BaseController
*/
public function index()
{
return ResponseHelper::success(
$this->getSceneList()
);
try {
$params = $this->request->param();
$result = $this->getSceneList($params);
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), 500);
}
}
/**
* 获取场景详情
*
* @return \think\response\Json
*/
public function detail()
{
$id = $this->request->param('id','');
if(empty($id)){
ResponseHelper::error('参数缺失');
}
try {
$id = $this->request->param('id', '');
if(empty($id)) {
return ResponseHelper::error('参数缺失');
}
$data = PlansSceneModel::where(['status' => PlansSceneModel::STATUS_ACTIVE,'id' => $id])->find();
if(empty($data)){
ResponseHelper::error('场景不存在');
}
$data = PlansSceneModel::where([
'status' => PlansSceneModel::STATUS_ACTIVE,
'id' => $id
])->find();
if(empty($data)) {
return ResponseHelper::error('场景不存在');
}
$data['scenarioTags'] = json_decode($data['scenarioTags'],true);
return ResponseHelper::success($data);
$data['scenarioTags'] = json_decode($data['scenarioTags'], true);
$data['count'] = $this->getPlanCount($id);
$data['growth'] = $this->calculateGrowth($id);
return ResponseHelper::success($data);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), 500);
}
}
/**
* 获取计划数量
*
* @param int $sceneId 场景ID
* @return int
*/
private function getPlanCount(int $sceneId): int
{
return Db::name('customer_acquisition_task')
->where('sceneId', $sceneId)
->where('status', 1)
->count();
}
/**
* 计算增长率
*
* @param int $sceneId 场景ID
* @return string
*/
private function calculateGrowth(int $sceneId): string
{
// 获取本月和上月的计划数量
$currentMonth = Db::name('customer_acquisition_task')
->where('sceneId', $sceneId)
->where('status', 1)
->whereTime('createTime', '>=', strtotime(date('Y-m-01')))
->count();
$lastMonth = Db::name('customer_acquisition_task')
->where('sceneId', $sceneId)
->where('status', 1)
->whereTime('createTime', 'between', [
strtotime(date('Y-m-01', strtotime('-1 month'))),
strtotime(date('Y-m-01')) - 1
])
->count();
if ($lastMonth == 0) {
return $currentMonth > 0 ? '100%' : '0%';
}
$growth = round(($currentMonth - $lastMonth) / $lastMonth * 100, 2);
return $growth . '%';
}
}

View File

@@ -0,0 +1,155 @@
<?php
namespace app\cunkebao\controller\plan;
use library\ResponseHelper;
use think\Db;
use app\cunkebao\controller\BaseController;
/**
* 获取计划任务列表控制器
*/
class PlanSceneV1Controller extends BaseController
{
/**
* 获取计划任务列表
*
* @return \think\response\Json
*/
public function index()
{
try {
$params = $this->request->param();
$page = isset($params['page']) ? intval($params['page']) : 1;
$limit = isset($params['limit']) ? intval($params['limit']) : 10;
$sceneId = $this->request->param('sceneId','');
$where = [
'deleteTime' => 0,
'companyId' => $this->getUserInfo('companyId'),
];
if($this->getUserInfo('isAdmin')){
$where['userId'] = $this->getUserInfo('id');
}
if(!empty($sceneId)){
$where['sceneId'] = $sceneId;
}
$total = Db::name('customer_acquisition_task')->where($where)->count();
$list = Db::name('customer_acquisition_task')
->where($where)
->order('createTime', 'desc')
->page($page, $limit)
->select();
foreach($list as &$val){
$val['createTime'] = date('Y-m-d H:i:s', $val['createTime']);
$val['updateTime'] = date('Y-m-d H:i:s', $val['updateTime']);
$val['sceneConf'] = json_decode($val['sceneConf'],true);
$val['reqConf'] = json_decode($val['reqConf'],true);
$val['msgConf'] = json_decode($val['msgConf'],true);
$val['tagConf'] = json_decode($val['tagConf'],true);
}
unset($val);
return ResponseHelper::success([
'total' => $total,
'list' => $list
], '获取计划任务列表成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
/**
* 拷贝计划任务
*
* @return \think\response\Json
*/
public function copy()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$plan = Db::name('customer_acquisition_task')->where('id', $planId)->find();
if (!$plan) {
return ResponseHelper::error('计划不存在', 404);
}
unset($plan['id']);
$plan['name'] = $plan['name'] . ' (拷贝)';
$plan['createTime'] = time();
$plan['updateTime'] = time();
$newPlanId = Db::name('customer_acquisition_task')->insertGetId($plan);
if (!$newPlanId) {
return ResponseHelper::error('拷贝计划失败', 500);
}
return ResponseHelper::success(['planId' => $newPlanId], '拷贝计划任务成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
/**
* 删除计划任务
*
* @return \think\response\Json
*/
public function delete()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$result = Db::name('customer_acquisition_task')->where('id', $planId)->update(['deleteTime' => time()]);
if (!$result) {
return ResponseHelper::error('删除计划失败', 500);
}
return ResponseHelper::success([], '删除计划任务成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
/**
* 修改计划任务状态
*
* @return \think\response\Json
*/
public function updateStatus()
{
try {
$params = $this->request->param();
$planId = isset($params['planId']) ? intval($params['planId']) : 0;
$status = isset($params['status']) ? intval($params['status']) : 0;
if ($planId <= 0) {
return ResponseHelper::error('计划ID不能为空', 400);
}
$result = Db::name('customer_acquisition_task')->where('id', $planId)->update(['status' => $status, 'updateTime' => time()]);
if (!$result) {
return ResponseHelper::error('修改计划状态失败', 500);
}
return ResponseHelper::success([], '修改计划任务状态成功');
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}
}
}

View File

@@ -18,6 +18,37 @@ class PostCreateAddFriendPlanV1Controller extends Controller
}
/**
* 生成唯一API密钥
*
* @return string
*/
private function generateApiKey()
{
// 生成5组随机字符串每组5个字符
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$apiKey = '';
for ($i = 0; $i < 5; $i++) {
$segment = '';
for ($j = 0; $j < 5; $j++) {
$segment .= $chars[mt_rand(0, strlen($chars) - 1)];
}
$apiKey .= ($i > 0 ? '-' : '') . $segment;
}
// 检查是否已存在
$exists = Db::name('customer_acquisition_task')
->where('apiKey', $apiKey)
->find();
if ($exists) {
// 如果已存在,递归重新生成
return $this->generateApiKey();
}
return $apiKey;
}
/**
* 添加计划任务
@@ -27,49 +58,91 @@ class PostCreateAddFriendPlanV1Controller extends Controller
public function index()
{
try {
$params = $this->request->only(['name', 'sceneId', 'status', 'reqConf', 'msgConf', 'tagConf']);
dd(
json_decode($params['reqConf']),
json_decode($params['tagConf']),
json_decode($params['msgConf'])
);
$params = $this->request->param();
// 验证必填字段
if (empty($data['name'])) {
if (empty($params['planName'])) {
return ResponseHelper::error('计划名称不能为空', 400);
}
if (empty($data['sceneId'])) {
if (empty($params['scenario'])) {
return ResponseHelper::error('场景ID不能为空', 400);
}
// 验证数据格式
if (!$this->validateJson($data['reqConf'])) {
return ResponseHelper::error('好友申请设置格式不正确', 400);
if (empty($params['device'])) {
return ResponseHelper::error('请选择设备', 400);
}
if (!$this->validateJson($data['msgConf'])) {
return ResponseHelper::error('消息设置格式不正确', 400);
}
if (!$this->validateJson($data['tagConf'])) {
return ResponseHelper::error('标签设置格式不正确', 400);
}
// 插入数据库 customer_acquisition_task
$result = Db::name('friend_plan')->insert($data);
if ($result) {
return ResponseHelper::success([], '添加计划任务成功');
} else {
return ResponseHelper::error('添加计划任务失败', 500);
// 归类参数
$msgConf = isset($params['messagePlans']) ? $params['messagePlans'] : [];
$tagConf = [
'scenarioTags' => $params['scenarioTags'] ?? [],
'customTags' => $params['customTags'] ?? [],
];
$reqConf = [
'device' => $params['device'] ?? [],
'remarkType' => $params['remarkType'] ?? '',
'greeting' => $params['greeting'] ?? '',
'addFriendInterval' => $params['addFriendInterval'] ?? '',
'startTime' => $params['startTime'] ?? '',
'endTime' => $params['endTime'] ?? '',
];
// 其余参数归为sceneConf
$sceneConf = $params;
unset(
$sceneConf['planName'],
$sceneConf['scenario'],
$sceneConf['messagePlans'],
$sceneConf['scenarioTags'],
$sceneConf['customTags'],
$sceneConf['device'],
$sceneConf['remarkType'],
$sceneConf['greeting'],
$sceneConf['addFriendInterval'],
$sceneConf['startTime'],
$sceneConf['endTime']
);
// 构建数据
$data = [
'name' => $params['planName'],
'sceneId' => $params['scenario'],
'sceneConf' => json_encode($sceneConf, JSON_UNESCAPED_UNICODE),
'reqConf' => json_encode($reqConf, JSON_UNESCAPED_UNICODE),
'msgConf' => json_encode($msgConf, JSON_UNESCAPED_UNICODE),
'tagConf' => json_encode($tagConf, JSON_UNESCAPED_UNICODE),
'userId' => $params['userInfo']['id'] ?? 0,
'companyId' => $params['userInfo']['companyId'] ?? 0,
'status' => 1,
'apiKey' => $this->generateApiKey(), // 生成API密钥
'createTime'=> time(),
'updateTime'=> time(),
];
// 开启事务
Db::startTrans();
try {
// 插入数据
$planId = Db::name('customer_acquisition_task')->insertGetId($data);
if (!$planId) {
throw new \Exception('添加计划失败');
}
// 提交事务
Db::commit();
return ResponseHelper::success(['planId' => $planId], '添加计划任务成功');
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
throw $e;
}
} catch (\Exception $e) {
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
}