【操盘手端】自动点赞提交

This commit is contained in:
Ghost
2025-04-10 16:40:30 +08:00
parent c7062445ab
commit e3d29f0935
29 changed files with 2863 additions and 1684 deletions

View File

@@ -46,4 +46,13 @@ Route::group('v1/', function () {
Route::group('traffic/pool', function () {
Route::post('import', 'app\\cunkebao\\controller\\TrafficPool@importOrders'); // 导入订单标签
});
// 工作台相关
Route::group('workbench', function () {
Route::post('create', 'app\\cunkebao\\controller\\WorkbenchController@create'); // 创建工作台
Route::get('list', 'app\\cunkebao\\controller\\WorkbenchController@getList'); // 获取工作台列表
Route::post('update-status', 'app\\cunkebao\\controller\\WorkbenchController@updateStatus'); // 更新工作台状态
Route::delete('delete', 'app\\cunkebao\\controller\\WorkbenchController@delete'); // 删除工作台
Route::post('copy', 'app\\cunkebao\\controller\\WorkbenchController@copy'); // 拷贝工作台
});
})->middleware(['jwt']);

View File

@@ -0,0 +1,469 @@
<?php
namespace app\cunkebao\controller;
use app\cunkebao\model\Workbench;
use app\cunkebao\model\WorkbenchAutoLike;
use app\cunkebao\model\WorkbenchMomentsSync;
use app\cunkebao\model\WorkbenchGroupPush;
use app\cunkebao\model\WorkbenchGroupCreate;
use app\cunkebao\validate\Workbench as WorkbenchValidate;
use think\Controller;
use think\Db;
/**
* 工作台控制器
*/
class WorkbenchController extends Controller
{
/**
* 工作台类型定义
*/
const TYPE_AUTO_LIKE = 1; // 自动点赞
const TYPE_MOMENTS_SYNC = 2; // 朋友圈同步
const TYPE_GROUP_PUSH = 3; // 群消息推送
const TYPE_GROUP_CREATE = 4; // 自动建群
/**
* 创建工作台
* @return \think\response\Json
*/
public function create()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
// 获取登录用户信息
$userInfo = request()->userInfo;
// 获取请求参数
$param = $this->request->post();
// 验证数据
$validate = new WorkbenchValidate;
if (!$validate->scene('create')->check($param)) {
return json(['code' => 400, 'msg' => $validate->getError()]);
}
Db::startTrans();
try {
// 创建工作台基本信息
$workbench = new Workbench;
$workbench->name = $param['name'];
$workbench->type = $param['type'];
$workbench->status = 1;
$workbench->autoStart = !empty($param['autoStart']) ? 1 : 0;
$workbench->userId = $userInfo['id'];
$workbench->companyId = $userInfo['companyId'];
$workbench->createTime = time();
$workbench->updateTime = time();
$workbench->save();
// 根据类型创建对应的配置
switch ($param['type']) {
case self::TYPE_AUTO_LIKE: // 自动点赞
$config = new WorkbenchAutoLike;
$config->workbenchId = $workbench->id;
$config->interval = $param['interval'];
$config->maxLikes = $param['maxLikes'];
$config->startTime = $param['startTime'];
$config->endTime = $param['endTime'];
$config->contentTypes = json_encode($param['contentTypes']);
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->tagOperator = $param['tagOperator'];
$config->createTime = time();
$config->updateTime = time();
$config->save();
break;
case self::TYPE_MOMENTS_SYNC: // 朋友圈同步
$config = new WorkbenchMomentsSync;
$config->workbenchId = $workbench->id;
$config->syncInterval = $param['syncInterval'];
$config->syncCount = $param['syncCount'];
$config->syncType = $param['syncType'];
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->createTime = time();
$config->updateTime = time();
$config->save();
break;
case self::TYPE_GROUP_PUSH: // 群消息推送
$config = new WorkbenchGroupPush;
$config->workbenchId = $workbench->id;
$config->pushInterval = $param['pushInterval'];
$config->pushContent = json_encode($param['pushContent']);
$config->pushTime = json_encode($param['pushTime']);
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->save();
break;
case self::TYPE_GROUP_CREATE: // 自动建群
$config = new WorkbenchGroupCreate;
$config->workbenchId = $workbench->id;
$config->groupNamePrefix = $param['groupNamePrefix'];
$config->maxGroups = $param['maxGroups'];
$config->membersPerGroup = $param['membersPerGroup'];
$config->devices = json_encode($param['devices']);
$config->targetGroups = json_encode($param['targetGroups']);
$config->createTime = time();
$config->updateTime = time();
$config->save();
break;
}
Db::commit();
return json(['code' => 200, 'msg' => '创建成功', 'data' => ['id' => $workbench->id]]);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '创建失败:' . $e->getMessage()]);
}
}
/**
* 获取工作台列表
* @return \think\response\Json
*/
public function getList()
{
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$type = $this->request->param('type', '');
$keyword = $this->request->param('name', '');
$where = [
['userId', '=', $this->request->userInfo['id']],
['isDel', '=', 0]
];
// 添加类型筛选
if ($type !== '') {
$where[] = ['type', '=', $type];
}
// 添加名称模糊搜索
if ($keyword !== '') {
$where[] = ['name', 'like', '%' . $keyword . '%'];
}
// 定义关联关系
$with = [
'autoLike' => function($query) {
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups');
},
// 'momentsSync' => function($query) {
// $query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups');
// },
// 'groupPush' => function($query) {
// $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups');
// },
// 'groupCreate' => function($query) {
// $query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups');
// }
];
$list = Workbench::where($where)
->with($with)
->field('id,name,type,status,autoStart,createTime,updateTime')
->order('id', 'desc')
->page($page, $limit)
->select()
->each(function ($item) {
// 处理配置信息
switch ($item->type) {
case self::TYPE_AUTO_LIKE:
if (!empty($item->autoLike)) {
$item->config = $item->autoLike;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
$item->config->contentTypes = json_decode($item->config->contentTypes, true);
}
unset($item->autoLike,$item->auto_like);
break;
case self::TYPE_MOMENTS_SYNC:
if (!empty($item->momentsSync)) {
$item->config = $item->momentsSync;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
}
break;
case self::TYPE_GROUP_PUSH:
if (!empty($item->groupPush)) {
$item->config = $item->groupPush;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
$item->config->pushContent = json_decode($item->config->pushContent, true);
$item->config->pushTime = json_decode($item->config->pushTime, true);
}
break;
case self::TYPE_GROUP_CREATE:
if (!empty($item->groupCreate)) {
$item->config = $item->groupCreate;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->targetGroups = json_decode($item->config->targetGroups, true);
}
break;
}
unset( $item->momentsSync, $item->groupPush, $item->groupCreate);
return $item;
});
$total = Workbench::where($where)->count();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
}
/**
* 获取工作台详情
* @param int $id 工作台ID
* @return \think\response\Json
*/
public function detail($id)
{
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 定义关联关系
$with = [
'autoLike' => function($query) {
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups');
},
'momentsSync' => function($query) {
$query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups');
},
'groupPush' => function($query) {
$query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups');
},
'groupCreate' => function($query) {
$query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups');
}
];
$workbench = Workbench::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']],
['isDel', '=', 0]
])
->field('id,name,type,status,autoStart,createTime,updateTime')
->with($with)
->find();
if (empty($workbench)) {
return json(['code' => 404, 'msg' => '工作台不存在']);
}
// 处理配置信息
switch ($workbench->type) {
case self::TYPE_AUTO_LIKE:
if (!empty($workbench->autoLike)) {
$workbench->config = $workbench->autoLike;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
$workbench->config->contentTypes = explode(',', $workbench->config->contentTypes);
}
break;
case self::TYPE_MOMENTS_SYNC:
if (!empty($workbench->momentsSync)) {
$workbench->config = $workbench->momentsSync;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
}
break;
case self::TYPE_GROUP_PUSH:
if (!empty($workbench->groupPush)) {
$workbench->config = $workbench->groupPush;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
$workbench->config->pushContent = json_decode($workbench->config->pushContent, true);
$workbench->config->pushTime = json_decode($workbench->config->pushTime, true);
}
break;
case self::TYPE_GROUP_CREATE:
if (!empty($workbench->groupCreate)) {
$workbench->config = $workbench->groupCreate;
$workbench->config->devices = json_decode($workbench->config->devices, true);
$workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true);
}
break;
}
unset($workbench->autoLike, $workbench->momentsSync, $workbench->groupPush, $workbench->groupCreate);
return json(['code' => 200, 'msg' => '获取成功', 'data' => $workbench]);
}
/**
* 更新工作台状态
* @return \think\response\Json
*/
public function updateStatus()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$param = $this->request->post();
// 验证数据
$validate = new WorkbenchValidate;
if (!$validate->scene('update_status')->check($param)) {
return json(['code' => 400, 'msg' => $validate->getError()]);
}
$workbench = Workbench::where([
['id', '=', $param['id']],
['userId', '=', $this->request->userInfo['id']]
])->find();
if (empty($workbench)) {
return json(['code' => 404, 'msg' => '工作台不存在']);
}
$workbench->status = !$workbench['status'];
$workbench->save();
return json(['code' => 200, 'msg' => '更新成功']);
}
/**
* 删除工作台(软删除)
*/
public function delete($id)
{
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
$workbench = Workbench::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']],
['isDel', '=', 0]
])->find();
if (!$workbench) {
return json(['code' => 404, 'msg' => '工作台不存在']);
}
// 软删除
$workbench->isDel = 1;
$workbench->deleteTime = date('Y-m-d H:i:s');
$workbench->save();
return json(['code' => 200, 'msg' => '删除成功']);
}
/**
* 拷贝工作台
* @return \think\response\Json
*/
public function copy()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误']);
}
$id = $this->request->post('id');
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 验证权限并获取原数据
$workbench = Workbench::where([
['id', '=', $id],
['userId', '=', $this->request->userInfo['id']]
])->find();
if (empty($workbench)) {
return json(['code' => 404, 'msg' => '工作台不存在']);
}
Db::startTrans();
try {
// 创建新的工作台基本信息
$newWorkbench = new Workbench;
$newWorkbench->name = $workbench->name . ' copy';
$newWorkbench->type = $workbench->type;
$newWorkbench->status = 1; // 新拷贝的默认启用
$newWorkbench->autoStart = $workbench->autoStart;
$newWorkbench->userId = $this->request->userInfo['id'];
$newWorkbench->save();
// 根据类型拷贝对应的配置
switch ($workbench->type) {
case self::TYPE_AUTO_LIKE:
$config = WorkbenchAutoLike::where('workbenchId', $id)->find();
if ($config) {
$newConfig = new WorkbenchAutoLike;
$newConfig->workbenchId = $newWorkbench->id;
$newConfig->interval = $config->interval;
$newConfig->maxLikes = $config->maxLikes;
$newConfig->startTime = $config->startTime;
$newConfig->endTime = $config->endTime;
$newConfig->contentTypes = $config->contentTypes;
$newConfig->devices = $config->devices;
$newConfig->targetGroups = $config->targetGroups;
$newConfig->save();
}
break;
case self::TYPE_MOMENTS_SYNC:
$config = WorkbenchMomentsSync::where('workbenchId', $id)->find();
if ($config) {
$newConfig = new WorkbenchMomentsSync;
$newConfig->workbenchId = $newWorkbench->id;
$newConfig->syncInterval = $config->syncInterval;
$newConfig->syncCount = $config->syncCount;
$newConfig->syncType = $config->syncType;
$newConfig->devices = $config->devices;
$newConfig->targetGroups = $config->targetGroups;
$newConfig->save();
}
break;
case self::TYPE_GROUP_PUSH:
$config = WorkbenchGroupPush::where('workbenchId', $id)->find();
if ($config) {
$newConfig = new WorkbenchGroupPush;
$newConfig->workbenchId = $newWorkbench->id;
$newConfig->pushInterval = $config->pushInterval;
$newConfig->pushContent = $config->pushContent;
$newConfig->pushTime = $config->pushTime;
$newConfig->devices = $config->devices;
$newConfig->targetGroups = $config->targetGroups;
$newConfig->save();
}
break;
case self::TYPE_GROUP_CREATE:
$config = WorkbenchGroupCreate::where('workbenchId', $id)->find();
if ($config) {
$newConfig = new WorkbenchGroupCreate;
$newConfig->workbenchId = $newWorkbench->id;
$newConfig->groupNamePrefix = $config->groupNamePrefix;
$newConfig->maxGroups = $config->maxGroups;
$newConfig->membersPerGroup = $config->membersPerGroup;
$newConfig->devices = $config->devices;
$newConfig->targetGroups = $config->targetGroups;
$newConfig->save();
}
break;
}
Db::commit();
return json(['code' => 200, 'msg' => '拷贝成功', 'data' => ['id' => $newWorkbench->id]]);
} catch (\Exception $e) {
Db::rollback();
return json(['code' => 500, 'msg' => '拷贝失败:' . $e->getMessage()]);
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace app\cunkebao\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 工作台模型
*/
class Workbench extends Model
{
protected $pk = 'id';
protected $name = 'workbenches';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
protected $dateFormat = 'Y-m-d H:i:s';
// 创建时间获取器
public function getCreateTimeAttr($value)
{
return $value ? date('Y-m-d', is_numeric($value) ? $value : strtotime($value)) : '';
}
// 更新时间获取器
public function getUpdateTimeAttr($value)
{
return $value ? date('Y-m-d', is_numeric($value) ? $value : strtotime($value)) : '';
}
// 自动点赞配置关联
public function autoLike()
{
return $this->hasOne('WorkbenchAutoLike', 'workbenchId', 'id');
}
// 朋友圈同步配置关联
public function momentsSync()
{
return $this->hasOne('WorkbenchMomentsSync', 'workbenchId', 'id');
}
// 群消息推送配置关联
public function groupPush()
{
return $this->hasOne('WorkbenchGroupPush', 'workbenchId', 'id');
}
// 自动建群配置关联
public function groupCreate()
{
return $this->hasOne('WorkbenchGroupCreate', 'workbenchId', 'id');
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 自动点赞工作台模型
*/
class WorkbenchAutoLike extends Model
{
protected $pk = 'id';
protected $name = 'workbench_auto_like';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的工作台
public function workbench()
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class WorkbenchGroupCreate extends Model
{
protected $pk = 'id';
protected $name = 'workbench_group_create';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的工作台
public function workbench()
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class WorkbenchGroupPush extends Model
{
protected $pk = 'id';
protected $name = 'workbench_group_push';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的工作台
public function workbench()
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class WorkbenchMomentsSync extends Model
{
protected $pk = 'id';
protected $name = 'workbench_moments_sync';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 定义关联的工作台
public function workbench()
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace app\cunkebao\validate;
use think\Validate;
class Workbench extends Validate
{
// 工作台类型定义
const TYPE_AUTO_LIKE = 1; // 自动点赞
const TYPE_MOMENTS_SYNC = 2; // 朋友圈同步
const TYPE_GROUP_PUSH = 3; // 群消息推送
const TYPE_GROUP_CREATE = 4; // 自动建群
/**
* 验证规则
*/
protected $rule = [
'name' => 'require|max:100',
'type' => 'require|in:1,2,3,4',
//'autoStart' => 'require|boolean',
// 自动点赞特有参数
'interval' => 'requireIf:type,1|number|min:1',
'maxLikes' => 'requireIf:type,1|number|min:1',
'startTime' => 'requireIf:type,1|dateFormat:H:i',
'endTime' => 'requireIf:type,1|dateFormat:H:i',
'contentTypes' => 'requireIf:type,1|array|contentTypeEnum:text,image,video',
// 朋友圈同步特有参数
'syncInterval' => 'requireIf:type,2|number|min:1',
'syncCount' => 'requireIf:type,2|number|min:1',
'syncType' => 'requireIf:type,2|in:1,2,3,4',
// 群消息推送特有参数
'pushInterval' => 'requireIf:type,3|number|min:1',
'pushContent' => 'requireIf:type,3|array',
'pushTime' => 'requireIf:type,3|array',
// 自动建群特有参数
'groupNamePrefix' => 'requireIf:type,4|max:50',
'maxGroups' => 'requireIf:type,4|number|min:1',
'membersPerGroup' => 'requireIf:type,4|number|min:1',
// 通用参数
'devices' => 'require|array',
'targetGroups' => 'require|array'
];
/**
* 错误信息
*/
protected $message = [
'name.require' => '请输入任务名称',
'name.max' => '任务名称最多100个字符',
'type.require' => '请选择工作台类型',
'type.in' => '工作台类型错误',
'autoStart.require' => '请选择是否自动启动',
'autoStart.boolean' => '自动启动参数必须为布尔值',
// 自动点赞相关提示
'interval.requireIf' => '请设置点赞间隔',
'interval.number' => '点赞间隔必须为数字',
'interval.min' => '点赞间隔必须大于0',
'maxLikes.requireIf' => '请设置每日最大点赞数',
'maxLikes.number' => '每日最大点赞数必须为数字',
'maxLikes.min' => '每日最大点赞数必须大于0',
'startTime.requireIf' => '请设置开始时间',
'startTime.dateFormat' => '开始时间格式错误',
'endTime.requireIf' => '请设置结束时间',
'endTime.dateFormat' => '结束时间格式错误',
'contentTypes.requireIf' => '请选择点赞内容类型',
'contentTypes.array' => '点赞内容类型必须是数组',
'contentTypes.contentTypeEnum' => '点赞内容类型只能是text、image、video',
// 朋友圈同步相关提示
'syncInterval.requireIf' => '请设置同步间隔',
'syncInterval.number' => '同步间隔必须为数字',
'syncInterval.min' => '同步间隔必须大于0',
'syncCount.requireIf' => '请设置同步数量',
'syncCount.number' => '同步数量必须为数字',
'syncCount.min' => '同步数量必须大于0',
'syncType.requireIf' => '请选择同步类型',
'syncType.in' => '同步类型错误',
// 群消息推送相关提示
'pushInterval.requireIf' => '请设置推送间隔',
'pushInterval.number' => '推送间隔必须为数字',
'pushInterval.min' => '推送间隔必须大于0',
'pushContent.requireIf' => '请设置推送内容',
'pushContent.array' => '推送内容格式错误',
'pushTime.requireIf' => '请设置推送时间',
'pushTime.array' => '推送时间格式错误',
// 自动建群相关提示
'groupNamePrefix.requireIf' => '请设置群名称前缀',
'groupNamePrefix.max' => '群名称前缀最多50个字符',
'maxGroups.requireIf' => '请设置最大建群数量',
'maxGroups.number' => '最大建群数量必须为数字',
'maxGroups.min' => '最大建群数量必须大于0',
'membersPerGroup.requireIf' => '请设置每个群的人数',
'membersPerGroup.number' => '每个群的人数必须为数字',
'membersPerGroup.min' => '每个群的人数必须大于0',
// 通用提示
'devices.require' => '请选择设备',
'devices.array' => '设备格式错误',
'targetGroups.require' => '请选择目标用户组',
'targetGroups.array' => '目标用户组格式错误'
];
/**
* 验证场景
*/
protected $scene = [
'create' => ['name', 'type', 'autoStart', 'devices', 'targetGroups',
'interval', 'maxLikes', 'startTime', 'endTime', 'contentTypes',
'syncInterval', 'syncCount', 'syncType',
'pushInterval', 'pushContent', 'pushTime',
'groupNamePrefix', 'maxGroups', 'membersPerGroup'
],
'update_status' => ['id', 'status']
];
/**
* 自定义验证规则
*/
protected function contentTypeEnum($value, $rule, $data)
{
$allowTypes = explode(',', $rule);
foreach ($value as $type) {
if (!in_array($type, $allowTypes)) {
return false;
}
}
return true;
}
}