新建获客计划 - 设备选择

This commit is contained in:
柳清爽
2025-04-07 15:09:07 +08:00
parent 118c538e38
commit c76c6e65ea
4 changed files with 108 additions and 410 deletions

View File

@@ -1,247 +0,0 @@
<?php
namespace app\plan\controller;
use think\Controller;
use think\Request;
use app\plan\model\Tag as TagModel;
use think\facade\Log;
/**
* 标签控制器
*/
class Tag extends Controller
{
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
}
/**
* 获取标签列表
*
* @return \think\response\Json
*/
public function index()
{
$type = Request::param('type', '');
$status = Request::param('status', 1, 'intval');
// 查询标签列表
$tags = TagModel::getTagsByType($type, $status);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $tags
]);
}
/**
* 创建标签
*
* @return \think\response\Json
*/
public function save()
{
$data = Request::post();
// 数据验证
if (empty($data['name']) || empty($data['type'])) {
return json([
'code' => 400,
'msg' => '缺少必要参数'
]);
}
try {
// 创建或获取标签
$tagId = TagModel::getOrCreate($data['name'], $data['type']);
return json([
'code' => 200,
'msg' => '创建成功',
'data' => $tagId
]);
} catch (\Exception $e) {
Log::error('创建标签异常', [
'data' => $data,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return json([
'code' => 500,
'msg' => '创建失败:' . $e->getMessage()
]);
}
}
/**
* 批量创建标签
*
* @return \think\response\Json
*/
public function batchCreate()
{
$data = Request::post();
// 数据验证
if (empty($data['names']) || empty($data['type'])) {
return json([
'code' => 400,
'msg' => '缺少必要参数'
]);
}
// 检查名称数组
if (!is_array($data['names'])) {
return json([
'code' => 400,
'msg' => '标签名称必须是数组'
]);
}
try {
$result = [];
// 批量处理标签
foreach ($data['names'] as $name) {
$name = trim($name);
if (empty($name)) continue;
$tagId = TagModel::getOrCreate($name, $data['type']);
$result[] = [
'id' => $tagId,
'name' => $name,
'type' => $data['type']
];
}
return json([
'code' => 200,
'msg' => '创建成功',
'data' => $result
]);
} catch (\Exception $e) {
Log::error('批量创建标签异常', [
'data' => $data,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return json([
'code' => 500,
'msg' => '创建失败:' . $e->getMessage()
]);
}
}
/**
* 更新标签
*
* @param int $id
* @return \think\response\Json
*/
public function update($id)
{
$data = Request::put();
// 检查标签是否存在
$tag = TagModel::get($id);
if (!$tag) {
return json([
'code' => 404,
'msg' => '标签不存在'
]);
}
// 准备更新数据
$updateData = [];
// 只允许更新特定字段
$allowedFields = ['name', 'status'];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$updateData[$field] = $data[$field];
}
}
// 更新标签
$tag->save($updateData);
// 如果更新了标签名称,且该标签有使用次数,则增加计数
if (isset($updateData['name']) && $updateData['name'] != $tag->name && $tag->count > 0) {
$tag->updateCount(1);
}
return json([
'code' => 200,
'msg' => '更新成功'
]);
}
/**
* 删除标签
*
* @param int $id
* @return \think\response\Json
*/
public function delete($id)
{
// 检查标签是否存在
$tag = TagModel::get($id);
if (!$tag) {
return json([
'code' => 404,
'msg' => '标签不存在'
]);
}
// 更新状态为删除
$tag->save([
'status' => 0
]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
}
/**
* 获取标签名称
*
* @return \think\response\Json
*/
public function getNames()
{
$ids = Request::param('ids');
// 验证参数
if (empty($ids)) {
return json([
'code' => 400,
'msg' => '缺少标签ID参数'
]);
}
// 处理参数
if (is_string($ids)) {
$ids = explode(',', $ids);
}
// 获取标签名称
$names = TagModel::getTagNames($ids);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $names
]);
}
}

View File

@@ -1,125 +0,0 @@
<?php
namespace app\plan\model;
use think\Model;
/**
* 标签模型
*/
class Tag extends Model
{
// 设置表名
protected $name = 'tag';
protected $prefix = 'tk_';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义字段类型
protected $type = [
'id' => 'integer',
'count' => 'integer',
'status' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer'
];
/**
* 获取或创建标签
* @param string $name 标签名
* @param string $type 标签类型
* @param string $color 标签颜色
* @return int 标签ID
*/
public static function getOrCreate($name, $type = 'traffic', $color = '')
{
$tag = self::where([
['name', '=', $name],
['type', '=', $type]
])->find();
if ($tag) {
return $tag['id'];
} else {
$model = new self();
$model->save([
'name' => $name,
'type' => $type,
'color' => $color ?: self::getRandomColor(),
'count' => 0,
'status' => 1
]);
return $model->id;
}
}
/**
* 更新标签使用次数
* @param int $id 标签ID
* @param int $increment 增量
* @return bool 更新结果
*/
public static function updateCount($id, $increment = 1)
{
return self::where('id', $id)->inc('count', $increment)->update();
}
/**
* 获取标签列表
* @param string $type 标签类型
* @param array $where 额外条件
* @return array 标签列表
*/
public static function getTagsByType($type = 'traffic', $where = [])
{
$conditions = array_merge([
['type', '=', $type],
['status', '=', 1]
], $where);
return self::where($conditions)
->order('count DESC, id DESC')
->select();
}
/**
* 根据ID获取标签名称
* @param array $ids 标签ID数组
* @return array 标签名称数组
*/
public static function getTagNames($ids)
{
if (empty($ids)) {
return [];
}
$tagIds = is_array($ids) ? $ids : explode(',', $ids);
$tags = self::where('id', 'in', $tagIds)->column('name');
return $tags;
}
/**
* 获取随机颜色
* @return string 颜色代码
*/
private static function getRandomColor()
{
$colors = [
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5',
'#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4caf50',
'#8bc34a', '#cddc39', '#ffeb3b', '#ffc107', '#ff9800',
'#ff5722', '#795548', '#9e9e9e', '#607d8b'
];
return $colors[array_rand($colors)];
}
}

View File

@@ -19,11 +19,6 @@ Route::group('api/plan', function () {
Route::get('traffic/stats', 'plan/Traffic/sourceStats');
Route::post('traffic/import', 'plan/Traffic/importTraffic');
Route::post('traffic/external', 'plan/Traffic/handleExternalTraffic');
// 标签相关路由
Route::resource('tags', 'plan/Tag');
Route::post('tags/batch', 'plan/Tag/batchCreate');
Route::get('tags/names', 'plan/Tag/getNames');
});
// 返回空数组,避免路由注册冲突