Files
cunkebao_v3/Server/application/cunkebao/validate/Workbench.php

413 lines
20 KiB
PHP
Raw Normal View History

2025-04-10 16:40:30 +08:00
<?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; // 自动建群
const TYPE_TRAFFIC_DISTRIBUTION = 5; // 流量分发
2025-09-11 15:16:04 +08:00
const TYPE_IMPORT_CONTACT = 6; // 流量分发
2025-04-10 16:40:30 +08:00
/**
* 验证规则
*/
protected $rule = [
'name' => 'require|max:100',
2025-09-11 15:16:04 +08:00
'type' => 'require|in:1,2,3,4,5,6',
2025-04-10 16:40:30 +08:00
//'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',
2025-05-09 09:22:31 +08:00
//'targetGroups' => 'requireIf:type,1|array',
2025-04-10 16:40:30 +08:00
// 朋友圈同步特有参数
//'syncInterval' => 'requireIf:type,2|number|min:1',
2025-04-10 16:40:30 +08:00
'syncCount' => 'requireIf:type,2|number|min:1',
'syncType' => 'requireIf:type,2|in:1,2,3,4',
2025-04-11 16:15:48 +08:00
'startTime' => 'requireIf:type,2|dateFormat:H:i',
'endTime' => 'requireIf:type,2|dateFormat:H:i',
2025-08-28 16:35:10 +08:00
'accountGroups' => 'requireIf:type,2|in:1,2',
'contentGroups' => 'requireIf:type,2|array',
2025-04-10 16:40:30 +08:00
// 群消息推送特有参数
2025-08-28 16:35:10 +08:00
'pushType' => 'requireIf:type,3|in:0,1', // 推送方式 0定时 1立即
2025-11-07 15:25:50 +08:00
'targetType' => 'requireIf:type,3|in:1,2', // 推送目标类型1=群推送2=好友推送
2025-11-13 16:10:47 +08:00
'groupPushSubType' => 'checkGroupPushSubType|in:1,2', // 群推送子类型1=群群发2=群公告仅当targetType=1时有效
'maxPerDay' => 'requireIf:type,3|number|min:1',
'pushOrder' => 'requireIf:type,3|in:1,2', // 1最早 2最新
'isLoop' => 'requireIf:type,3|in:0,1',
'status' => 'requireIf:type,3|in:0,1',
2025-11-07 15:25:50 +08:00
'wechatGroups' => 'checkGroupPushTarget|array|min:1', // 当targetType=1时必填
'wechatFriends' => 'checkFriendPushTarget|array', // 当targetType=2时可选可以为空
2025-11-13 16:10:47 +08:00
'ownerWechatId' => 'checkFriendPushService', // 当targetType=2且未选择好友/流量池时必填
2026-01-06 10:58:29 +08:00
'contentGroups' => 'checkContentGroups|array', // 群推送时必填,但群公告时可以为空
2025-11-13 16:10:47 +08:00
// 群公告特有参数
'announcementContent' => 'checkAnnouncementContent|max:5000', // 群公告内容当groupPushSubType=2时必填
'enableAiRewrite' => 'checkEnableAiRewrite|in:0,1', // 是否启用AI智能话术改写
'aiRewritePrompt' => 'checkAiRewritePrompt|max:500', // AI改写提示词当enableAiRewrite=1时必填
2025-04-10 16:40:30 +08:00
// 自动建群特有参数
'groupNameTemplate' => 'requireIf:type,4|max:50',
'maxGroupsPerDay' => 'requireIf:type,4|number|min:1',
2025-08-22 10:23:05 +08:00
'groupSizeMin' => 'requireIf:type,4|number|min:1|max:50',
'groupSizeMax' => 'requireIf:type,4|number|min:1|max:50',
// 流量分发特有参数
'distributeType' => 'requireIf:type,5|in:1,2',
'maxPerDay' => 'requireIf:type,5|number|min:1',
'timeType' => 'requireIf:type,5|in:1,2',
2025-08-28 16:35:10 +08:00
'accountGroups' => 'requireIf:type,5|array|min:1',
// 通用参数
2025-09-19 17:50:53 +08:00
'deviceGroups' => 'requireIf:type,1,2,5|array',
2025-11-13 16:10:47 +08:00
'trafficPools' => 'checkFriendPushPools',
2025-04-10 16:40:30 +08:00
];
/**
* 错误信息
*/
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' => '请设置同步间隔',
2025-04-10 16:40:30 +08:00
'syncInterval.number' => '同步间隔必须为数字',
'syncInterval.min' => '同步间隔必须大于0',*/
2025-04-10 16:40:30 +08:00
'syncCount.requireIf' => '请设置同步数量',
'syncCount.number' => '同步数量必须为数字',
'syncCount.min' => '同步数量必须大于0',
'syncType.requireIf' => '请选择同步类型',
'syncType.in' => '同步类型错误',
2025-04-11 16:15:48 +08:00
'startTime.requireIf' => '请设置发布开始时间',
'startTime.dateFormat' => '发布开始时间格式错误',
'endTime.requireIf' => '请设置发布结束时间',
'endTime.dateFormat' => '发布结束时间格式错误',
2025-08-28 16:35:10 +08:00
'accountGroups.requireIf' => '请选择账号类型',
'accountGroups.in' => '账号类型错误',
2026-01-06 10:58:29 +08:00
'contentGroups.checkContentGroups' => '群群发时必须选择内容库',
2025-08-28 16:35:10 +08:00
'contentGroups.array' => '内容库格式错误',
2025-04-10 16:40:30 +08:00
// 群消息推送相关提示
'pushType.requireIf' => '请选择推送方式',
'startTime.requireIf' => '请设置推送开始时间',
'startTime.dateFormat' => '推送开始时间格式错误',
'endTime.requireIf' => '请设置推送结束时间',
'endTime.dateFormat' => '推送结束时间格式错误',
'maxPerDay.requireIf' => '请设置每日最大推送数',
'maxPerDay.number' => '每日最大推送数必须为数字',
'maxPerDay.min' => '每日最大推送数必须大于0',
'pushOrder.requireIf' => '请选择推送顺序',
'pushOrder.in' => '推送顺序错误',
'isLoop.requireIf' => '请选择是否循环推送',
'isLoop.in' => '循环推送参数错误',
2025-11-07 15:25:50 +08:00
'targetType.requireIf' => '请选择推送目标类型',
'targetType.in' => '推送目标类型错误,只能选择群推送或好友推送',
2025-08-28 16:35:10 +08:00
'wechatGroups.requireIf' => '请选择推送群组',
2025-11-07 15:25:50 +08:00
'wechatGroups.checkGroupPushTarget' => '群推送时必须选择推送群组',
2025-08-28 16:35:10 +08:00
'wechatGroups.array' => '推送群组格式错误',
'wechatGroups.min' => '至少选择一个推送群组',
2025-11-13 16:10:47 +08:00
'groupPushSubType.checkGroupPushSubType' => '群推送子类型错误',
'groupPushSubType.in' => '群推送子类型只能是群群发或群公告',
'announcementContent.checkAnnouncementContent' => '群公告必须输入公告内容',
'announcementContent.max' => '公告内容最多5000个字符',
'enableAiRewrite.checkEnableAiRewrite' => 'AI智能话术改写参数错误',
'enableAiRewrite.in' => 'AI智能话术改写参数只能是0或1',
'aiRewritePrompt.checkAiRewritePrompt' => '启用AI智能话术改写时必须输入改写提示词',
'aiRewritePrompt.max' => '改写提示词最多500个字符',
2025-11-07 15:25:50 +08:00
'wechatFriends.requireIf' => '请选择推送好友',
'wechatFriends.checkFriendPushTarget' => '好友推送时必须选择推送好友',
'wechatFriends.array' => '推送好友格式错误',
'deviceGroups.requireIf' => '请选择设备',
'deviceGroups.array' => '设备格式错误',
2025-11-13 16:10:47 +08:00
'ownerWechatId.checkFriendPushService' => '好友推送需选择客服或提供好友/流量池',
2025-04-10 16:40:30 +08:00
// 自动建群相关提示
'groupNameTemplate.requireIf' => '请设置群名称前缀',
'groupNameTemplate.max' => '群名称前缀最多50个字符',
'maxGroupsPerDay.requireIf' => '请设置最大建群数量',
'maxGroupsPerDay.number' => '最大建群数量必须为数字',
'maxGroupsPerDay.min' => '最大建群数量必须大于0',
'groupSizeMin.requireIf' => '请设置每个群的人数',
'groupSizeMin.number' => '每个群的人数必须为数字',
'groupSizeMin.min' => '每个群的人数必须大于0',
2025-08-22 10:23:05 +08:00
'groupSizeMin.max' => '每个群的人数最大50人',
'groupSizeMax.requireIf' => '请设置每个群的人数',
'groupSizeMax.number' => '每个群的人数必须为数字',
'groupSizeMax.min' => '每个群的人数必须大于0',
2025-08-22 10:23:05 +08:00
'groupSizeMax.max' => '每个群的人数最大50人',
// 流量分发相关提示
'distributeType.requireIf' => '请选择流量分发类型',
'distributeType.in' => '流量分发类型错误',
'maxPerDay.requireIf' => '请设置每日最大流量',
'maxPerDay.number' => '每日最大流量必须为数字',
'maxPerDay.min' => '每日最大流量必须大于0',
'timeType.requireIf' => '请选择时间类型',
2025-04-10 16:40:30 +08:00
// 通用提示
2025-09-19 17:50:53 +08:00
'deviceGroups.require' => '请选择设备',
'deviceGroups.array' => '设备格式错误',
2025-04-10 16:40:30 +08:00
'targetGroups.require' => '请选择目标用户组',
'targetGroups.array' => '目标用户组格式错误',
2025-08-28 16:35:10 +08:00
'accountGroups.requireIf' => '流量分发时必须选择分发账号',
'accountGroups.array' => '分发账号格式错误',
'accountGroups.min' => '至少选择一个分发账号',
2025-11-13 16:10:47 +08:00
'trafficPools.checkFriendPushPools' => '好友推送时请选择好友或流量池',
2025-04-10 16:40:30 +08:00
];
/**
* 验证场景
*/
protected $scene = [
2025-09-19 17:50:53 +08:00
'create' => ['name', 'type', 'autoStart', 'deviceGroups', 'targetGroups',
2025-04-10 16:40:30 +08:00
'interval', 'maxLikes', 'startTime', 'endTime', 'contentTypes',
2025-11-07 15:25:50 +08:00
'syncCount', 'syncType', 'accountGroups',
2025-11-13 16:10:47 +08:00
'pushType', 'targetType', 'groupPushSubType', 'startTime', 'endTime', 'maxPerDay', 'pushOrder', 'isLoop', 'status', 'wechatGroups', 'wechatFriends', 'trafficPools', 'ownerWechatId', 'contentGroups',
'announcementContent', 'enableAiRewrite', 'aiRewritePrompt',
2025-08-28 16:35:10 +08:00
'groupNameTemplate', 'maxGroupsPerDay', 'groupSizeMin', 'groupSizeMax',
2025-11-07 15:25:50 +08:00
'distributeType', 'timeType', 'accountGroups',
2025-04-10 16:40:30 +08:00
],
'update_status' => ['id', 'status'],
2025-11-07 15:25:50 +08:00
'update' => ['name', 'type', 'autoStart', 'deviceGroups', 'targetGroups',
'interval', 'maxLikes', 'startTime', 'endTime', 'contentTypes',
2025-11-07 15:25:50 +08:00
'syncCount', 'syncType', 'accountGroups',
2025-11-13 16:10:47 +08:00
'pushType', 'targetType', 'groupPushSubType', 'startTime', 'endTime', 'maxPerDay', 'pushOrder', 'isLoop', 'status', 'wechatGroups', 'wechatFriends', 'trafficPools', 'ownerWechatId', 'contentGroups',
'announcementContent', 'enableAiRewrite', 'aiRewritePrompt',
2025-08-28 16:35:10 +08:00
'groupNameTemplate', 'maxGroupsPerDay', 'groupSizeMin', 'groupSizeMax',
2025-11-07 15:25:50 +08:00
'distributeType', 'timeType', 'accountGroups',
]
2025-04-10 16:40:30 +08:00
];
/**
* 自定义验证规则
*/
protected function contentTypeEnum($value, $rule, $data)
{
$allowTypes = explode(',', $rule);
foreach ($value as $type) {
if (!in_array($type, $allowTypes)) {
return false;
}
}
return true;
}
2025-11-07 15:25:50 +08:00
/**
* 验证群推送目标当targetType=1wechatGroups必填
*/
protected function checkGroupPushTarget($value, $rule, $data)
{
// 如果是群消息推送类型
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
// 如果targetType=1群推送则wechatGroups必填
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
if ($targetType == 1) {
// 检查值是否存在且有效
if (!isset($value) || $value === null || $value === '') {
return false;
}
if (!is_array($value) || count($value) < 1) {
return false;
}
}
}
return true;
}
/**
* 验证好友推送目标当targetType=2wechatFriends可选可以为空
*/
protected function checkFriendPushTarget($value, $rule, $data)
{
// 如果是群消息推送类型
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
// 如果targetType=2好友推送wechatFriends可以为空数组
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
if ($targetType == 2) {
// 如果提供了值,则必须是数组
if (isset($value) && $value !== null && $value !== '') {
if (!is_array($value)) {
return false;
}
}
}
}
return true;
}
/**
* 验证好友推送时设备必填当targetType=2deviceGroups必填
*/
2025-11-13 16:10:47 +08:00
protected function checkFriendPushService($value, $rule, $data)
{
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
if ($targetType == 2) {
if ($value !== null && $value !== '' && !is_array($value)) {
return false;
}
$hasFriends = isset($data['wechatFriends']) && is_array($data['wechatFriends']) && count($data['wechatFriends']) > 0;
$hasPools = isset($data['trafficPools']) && is_array($data['trafficPools']) && count($data['trafficPools']) > 0;
$hasServices = is_array($value) && count(array_filter($value, function ($item) {
if (is_array($item)) {
return !empty($item['ownerWechatId'] ?? $item['wechatId'] ?? $item['id']);
}
return $item !== null && $item !== '';
})) > 0;
if (!$hasFriends && !$hasPools && !$hasServices) {
return false;
}
}
}
return true;
}
/**
* 验证好友推送时是否选择好友或流量池(至少其一)
*/
protected function checkFriendPushPools($value, $rule, $data)
2025-11-07 15:25:50 +08:00
{
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
if ($targetType == 2) {
2025-11-13 16:10:47 +08:00
$hasFriends = isset($data['wechatFriends']) && !empty($data['wechatFriends']);
$hasPools = isset($value) && $value !== null && $value !== '' && is_array($value) && count($value) > 0;
if (!$hasFriends && !$hasPools) {
return false;
}
if (isset($value) && $value !== null && $value !== '') {
if (!is_array($value)) {
return false;
}
}
}
}
return true;
}
/**
* 验证群推送子类型当targetType=1groupPushSubType必填且只能是1或2
*/
protected function checkGroupPushSubType($value, $rule, $data)
{
// 如果是群消息推送类型
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
// 如果targetType=1群推送则groupPushSubType必填
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
if ($targetType == 1) {
2025-11-07 15:25:50 +08:00
// 检查值是否存在且有效
2025-11-13 16:10:47 +08:00
if (!isset($value) || !in_array(intval($value), [1, 2])) {
2025-11-07 15:25:50 +08:00
return false;
}
2025-11-13 16:10:47 +08:00
}
}
return true;
}
/**
* 验证群公告内容当groupPushSubType=2announcementContent必填
*/
protected function checkAnnouncementContent($value, $rule, $data)
{
// 如果是群消息推送类型
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
// 如果targetType=1且groupPushSubType=2群公告则announcementContent必填
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
$groupPushSubType = isset($data['groupPushSubType']) ? intval($data['groupPushSubType']) : 1; // 默认1
if ($targetType == 1 && $groupPushSubType == 2) {
// 检查值是否存在且有效
if (!isset($value) || $value === null || trim($value) === '') {
return false;
}
}
}
return true;
}
/**
* 验证AI智能话术改写当enableAiRewrite=1aiRewritePrompt必填
*/
protected function checkEnableAiRewrite($value, $rule, $data)
{
// 如果是群消息推送类型且是群公告
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
$groupPushSubType = isset($data['groupPushSubType']) ? intval($data['groupPushSubType']) : 1; // 默认1
if ($targetType == 1 && $groupPushSubType == 2) {
// 检查值是否存在且有效
if (!isset($value) || !in_array(intval($value), [0, 1])) {
return false;
}
}
}
return true;
}
/**
* 验证AI改写提示词当enableAiRewrite=1aiRewritePrompt必填
*/
protected function checkAiRewritePrompt($value, $rule, $data)
{
// 如果是群消息推送类型且是群公告
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
$groupPushSubType = isset($data['groupPushSubType']) ? intval($data['groupPushSubType']) : 1; // 默认1
$enableAiRewrite = isset($data['enableAiRewrite']) ? intval($data['enableAiRewrite']) : 0; // 默认0
if ($targetType == 1 && $groupPushSubType == 2 && $enableAiRewrite == 1) {
// 如果启用AI改写提示词必填
if (!isset($value) || $value === null || trim($value) === '') {
2025-11-07 15:25:50 +08:00
return false;
}
}
}
return true;
}
2026-01-06 10:58:29 +08:00
/**
* 验证内容库(群推送时必填,但群公告时可以为空)
*/
protected function checkContentGroups($value, $rule, $data)
{
// 如果是群消息推送类型
if (isset($data['type']) && $data['type'] == self::TYPE_GROUP_PUSH) {
$targetType = isset($data['targetType']) ? intval($data['targetType']) : 1; // 默认1
$groupPushSubType = isset($data['groupPushSubType']) ? intval($data['groupPushSubType']) : 1; // 默认1
// 群公告groupPushSubType=2内容库可以为空不需要验证
if ($targetType == 1 && $groupPushSubType == 2) {
// 群公告时允许为空,不进行验证
return true;
}
// 其他情况(群群发、好友推送),内容库必填
if (!isset($value) || $value === null || $value === '') {
return false;
}
if (!is_array($value) || count($value) < 1) {
return false;
}
}
return true;
}
2025-04-10 16:40:30 +08:00
}