入群欢迎语功能提交

This commit is contained in:
wong
2026-01-09 17:05:17 +08:00
parent 2b128195bf
commit 66d217d5f1
33 changed files with 3827 additions and 37 deletions

View File

@@ -12,6 +12,7 @@ use app\cunkebao\model\WorkbenchImportContact;
use app\cunkebao\model\WorkbenchMomentsSync;
use app\cunkebao\model\WorkbenchGroupPush;
use app\cunkebao\model\WorkbenchGroupCreate;
use app\cunkebao\model\WorkbenchGroupWelcome;
use app\cunkebao\validate\Workbench as WorkbenchValidate;
use think\Controller;
use think\Db;
@@ -33,6 +34,7 @@ class WorkbenchController extends Controller
const TYPE_GROUP_CREATE = 4; // 自动建群
const TYPE_TRAFFIC_DISTRIBUTION = 5; // 流量分发
const TYPE_IMPORT_CONTACT = 6; // 联系人导入
const TYPE_GROUP_WELCOME = 7; // 入群欢迎语
/**
* 创建工作台
@@ -49,7 +51,6 @@ class WorkbenchController extends Controller
// 获取请求参数
$param = $this->request->post();
// 根据业务默认值补全参数
if (
@@ -201,6 +202,30 @@ class WorkbenchController extends Controller
$config->createTime = time();
$config->save();
break;
case self::TYPE_GROUP_WELCOME: // 入群欢迎语
$config = new WorkbenchGroupWelcome;
$config->workbenchId = $workbench->id;
$config->devices = json_encode($param['deviceGroups'] ?? [], JSON_UNESCAPED_UNICODE);
$config->groups = json_encode($param['wechatGroups'] ?? [], JSON_UNESCAPED_UNICODE);
$config->startTime = $param['startTime'] ?? '';
$config->endTime = $param['endTime'] ?? '';
$config->interval = isset($param['interval']) ? intval($param['interval']) : 0;
// messages 作为 JSON 存储(如果表中有 messages 字段)
if (isset($param['messages']) && is_array($param['messages'])) {
// 按 order 排序
usort($param['messages'], function($a, $b) {
$orderA = isset($a['order']) ? intval($a['order']) : 0;
$orderB = isset($b['order']) ? intval($b['order']) : 0;
return $orderA <=> $orderB;
});
$config->messages = json_encode($param['messages'], JSON_UNESCAPED_UNICODE);
} else {
$config->messages = json_encode([], JSON_UNESCAPED_UNICODE);
}
$config->createTime = time();
$config->updateTime = time();
$config->save();
break;
}
Db::commit();
@@ -456,6 +481,23 @@ class WorkbenchController extends Controller
}
unset($item->importContact, $item->import_contact);
break;
case self::TYPE_GROUP_WELCOME:
if (!empty($item->groupWelcome)) {
$item->config = $item->groupWelcome;
$item->config->deviceGroups = json_decode($item->config->devices, true);
$item->config->wechatGroups = json_decode($item->config->groups, true);
// 解析 messages JSON 字段
if (!empty($item->config->messages)) {
$item->config->messages = json_decode($item->config->messages, true);
if (!is_array($item->config->messages)) {
$item->config->messages = [];
}
} else {
$item->config->messages = [];
}
}
unset($item->groupWelcome, $item->group_welcome);
break;
}
// 添加创建人名称
$item['creatorName'] = $item->user ? $item->user->username : '';
@@ -510,6 +552,9 @@ class WorkbenchController extends Controller
'importContact' => function ($query) {
$query->field('workbenchId,devices,pools,num,remarkType,remark,clearContact,startTime,endTime');
},
'groupWelcome' => function ($query) {
$query->field('workbenchId,devices,groups,startTime,endTime,interval,messages');
},
];
$where = [
@@ -773,6 +818,23 @@ class WorkbenchController extends Controller
}
unset($workbench->importContact, $workbench->import_contact);
break;
case self::TYPE_GROUP_WELCOME:
if (!empty($workbench->groupWelcome)) {
$workbench->config = $workbench->groupWelcome;
$workbench->config->deviceGroups = json_decode($workbench->config->devices, true);
$workbench->config->wechatGroups = json_decode($workbench->config->groups, true);
// 解析 messages JSON 字段
if (!empty($workbench->config->messages)) {
$workbench->config->messages = json_decode($workbench->config->messages, true);
if (!is_array($workbench->config->messages)) {
$workbench->config->messages = [];
}
} else {
$workbench->config->messages = [];
}
}
unset($workbench->groupWelcome, $workbench->group_welcome);
break;
}
unset(
$workbench->autoLike,
@@ -873,13 +935,14 @@ class WorkbenchController extends Controller
}
// 获取群当targetType=1时
if (!empty($workbench->config->wechatGroups) && isset($workbench->config->targetType) && $workbench->config->targetType == 1) {
$groupList = Db::name('wechat_group')->alias('wg')
->join('wechat_account wa', 'wa.wechatId = wg.ownerWechatId')
->where('wg.id', 'in', $workbench->config->wechatGroups)
->order('wg.id', 'desc')
->field('wg.id,wg.name as groupName,wg.ownerWechatId,wa.nickName,wa.avatar,wa.alias,wg.avatar as groupAvatar')
if (!empty($workbench->config->wechatGroups) && $workbench->type != self::TYPE_GROUP_CREATE) {
$groupList = Db::table('s2_wechat_chatroom')->alias('wc')
->whereIn('wc.id', $workbench->config->wechatGroups)
->where('wc.isDeleted', 0)
->order('wc.id', 'desc')
->field('wc.id,wc.nickname as groupName,wc.wechatAccountWechatId as ownerWechatId,wc.wechatAccountNickname as nickName,wc.wechatAccountAvatar as avatar,wc.wechatAccountAlias as alias,wc.chatroomAvatar as groupAvatar')
->select();
$workbench->config->wechatGroupsOptions = $groupList;
} else {
@@ -887,7 +950,7 @@ class WorkbenchController extends Controller
}
// 获取好友当targetType=2时
if (!empty($workbench->config->wechatFriends) && isset($workbench->config->targetType) && $workbench->config->targetType == 2) {
if (!empty($workbench->config->wechatFriends)) {
$friendList = Db::table('s2_wechat_friend')->alias('wf')
->join(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId', 'left')
->where('wf.id', 'in', $workbench->config->wechatFriends)
@@ -900,7 +963,7 @@ class WorkbenchController extends Controller
}
// 获取流量池当targetType=2时
if (!empty($workbench->config->trafficPools) && isset($workbench->config->targetType) && $workbench->config->targetType == 2) {
if (!empty($workbench->config->trafficPools)) {
$poolList = [];
$companyId = $this->request->userInfo['companyId'];
@@ -1065,9 +1128,7 @@ class WorkbenchController extends Controller
}
$workbench->config->wechatGroupsOptions = $wechatGroupsOptions;
} else {
$workbench->config->wechatGroupsOptions = [];
}
}
// 获取管理员选项(自动建群)
if ($workbench->type == self::TYPE_GROUP_CREATE && !empty($workbench->config->admins)) {
@@ -1258,6 +1319,30 @@ class WorkbenchController extends Controller
$config->save();
}
break;
case self::TYPE_GROUP_WELCOME: // 入群欢迎语
$config = WorkbenchGroupWelcome::where('workbenchId', $param['id'])->find();
if ($config) {
$config->devices = json_encode($param['deviceGroups'] ?? [], JSON_UNESCAPED_UNICODE);
$config->groups = json_encode($param['wechatGroups'] ?? [], JSON_UNESCAPED_UNICODE);
$config->startTime = $param['startTime'] ?? '';
$config->endTime = $param['endTime'] ?? '';
$config->interval = isset($param['interval']) ? intval($param['interval']) : 0;
// messages 作为 JSON 存储
if (isset($param['messages']) && is_array($param['messages'])) {
// 按 order 排序
usort($param['messages'], function($a, $b) {
$orderA = isset($a['order']) ? intval($a['order']) : 0;
$orderB = isset($b['order']) ? intval($b['order']) : 0;
return $orderA <=> $orderB;
});
$config->messages = json_encode($param['messages'], JSON_UNESCAPED_UNICODE);
} else {
$config->messages = json_encode([], JSON_UNESCAPED_UNICODE);
}
$config->updateTime = time();
$config->save();
}
break;
}
Db::commit();
@@ -1476,6 +1561,22 @@ class WorkbenchController extends Controller
$newConfig->save();
}
break;
case self::TYPE_GROUP_WELCOME: // 入群欢迎语
$config = WorkbenchGroupWelcome::where('workbenchId', $id)->find();
if ($config) {
$newConfig = new WorkbenchGroupWelcome;
$newConfig->workbenchId = $newWorkbench->id;
$newConfig->devices = $config->devices;
$newConfig->groups = $config->groups;
$newConfig->startTime = $config->startTime;
$newConfig->endTime = $config->endTime;
$newConfig->interval = $config->interval;
$newConfig->messages = $config->messages ?? json_encode([], JSON_UNESCAPED_UNICODE);
$newConfig->createTime = time();
$newConfig->updateTime = time();
$newConfig->save();
}
break;
}
Db::commit();

View File

@@ -67,6 +67,11 @@ class Workbench extends Model
return $this->hasOne('WorkbenchImportContact', 'workbenchId', 'id');
}
// 入群欢迎语配置关联
public function groupWelcome()
{
return $this->hasOne('WorkbenchGroupWelcome', 'workbenchId', 'id');
}
/**
* 用户关联

View File

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

View File

@@ -0,0 +1,51 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 入群欢迎语发送记录模型
*/
class WorkbenchGroupWelcomeItem extends Model
{
protected $table = 'ck_workbench_group_welcome_item';
protected $pk = 'id';
protected $name = 'workbench_group_welcome_item';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 状态常量
const STATUS_PENDING = 0; // 待发送
const STATUS_SENDING = 1; // 发送中
const STATUS_SUCCESS = 2; // 发送成功
const STATUS_FAILED = 3; // 发送失败
/**
* 定义关联的工作台
*/
public function workbench()
{
return $this->belongsTo('Workbench', 'workbenchId', 'id');
}
/**
* 获取状态文本
* @param int $status 状态值
* @return string
*/
public static function getStatusText($status)
{
$statusMap = [
self::STATUS_PENDING => '待发送',
self::STATUS_SENDING => '发送中',
self::STATUS_SUCCESS => '发送成功',
self::STATUS_FAILED => '发送失败',
];
return $statusMap[$status] ?? '未知';
}
}

View File

@@ -13,13 +13,14 @@ class Workbench extends Validate
const TYPE_GROUP_CREATE = 4; // 自动建群
const TYPE_TRAFFIC_DISTRIBUTION = 5; // 流量分发
const TYPE_IMPORT_CONTACT = 6; // 流量分发
const TYPE_GROUP_WELCOME = 7; // 入群欢迎语
/**
* 验证规则
*/
protected $rule = [
'name' => 'require|max:100',
'type' => 'require|in:1,2,3,4,5,6',
'type' => 'require|in:1,2,3,4,5,6,7',
//'autoStart' => 'require|boolean',
// 自动点赞特有参数
'interval' => 'requireIf:type,1|number|min:1',
@@ -62,8 +63,14 @@ class Workbench extends Validate
'maxPerDay' => 'requireIf:type,5|number|min:1',
'timeType' => 'requireIf:type,5|in:1,2',
'accountGroups' => 'requireIf:type,5|array|min:1',
// 入群欢迎语特有参数
'wechatGroups' => 'requireIf:type,7|array|min:1', // 入群欢迎语必须选择群组
'interval' => 'requireIf:type,7|number|min:1', // 间隔时间
'startTime' => 'requireIf:type,7|dateFormat:H:i', // 开始时间
'endTime' => 'requireIf:type,7|dateFormat:H:i', // 结束时间
'messages' => 'requireIf:type,7|array|min:1', // 欢迎消息列表
// 通用参数
'deviceGroups' => 'requireIf:type,1,2,5|array',
'deviceGroups' => 'requireIf:type,1,2,5,7|array',
'trafficPools' => 'checkFriendPushPools',
];
@@ -185,6 +192,7 @@ class Workbench extends Validate
'announcementContent', 'enableAiRewrite', 'aiRewritePrompt',
'groupNameTemplate', 'maxGroupsPerDay', 'groupSizeMin', 'groupSizeMax',
'distributeType', 'timeType', 'accountGroups',
'messages',
],
'update_status' => ['id', 'status'],
'update' => ['name', 'type', 'autoStart', 'deviceGroups', 'targetGroups',
@@ -194,6 +202,7 @@ class Workbench extends Validate
'announcementContent', 'enableAiRewrite', 'aiRewritePrompt',
'groupNameTemplate', 'maxGroupsPerDay', 'groupSizeMin', 'groupSizeMax',
'distributeType', 'timeType', 'accountGroups',
'messages',
]
];