任务调度器提交

This commit is contained in:
wong
2026-01-17 15:17:05 +08:00
parent 002f03e037
commit 51a731ea68
3 changed files with 459 additions and 214 deletions

View File

@@ -127,6 +127,27 @@ class ReplyController extends BaseController
if ($title === '') {
return ResponseHelper::error('标题不能为空');
}
if ($content === '') {
return ResponseHelper::error('内容不能为空');
}
// 根据 msgType 处理 content3=图片43=视频49=链接 需要 JSON 编码
if (in_array($msgType, [3, 43, 49])) {
// 如果 content 已经是数组,直接编码;如果是字符串,先尝试解码再编码(确保格式正确)
if (is_array($content)) {
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
} elseif (is_string($content)) {
// 尝试解析,如果已经是 JSON 字符串,确保格式正确
$decoded = json_decode($content, true);
if ($decoded !== null) {
// 是有效的 JSON重新编码确保格式统一
$content = json_encode($decoded, JSON_UNESCAPED_UNICODE);
} else {
// 不是 JSON直接编码
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
}
}
}
try {
$now = time();
@@ -142,12 +163,20 @@ class ReplyController extends BaseController
'lastUpdateTime' => $now,
'userId' => $userId,
];
/** @var Reply $reply */
$reply = new Reply();
$reply->save($data);
return ResponseHelper::success($reply->toArray(), '创建成功');
// 返回时解析 content与 buildGroupData 保持一致)
$replyData = $reply->toArray();
if (in_array($msgType, [3, 43, 49]) && !empty($replyData['content'])) {
$decoded = json_decode($replyData['content'], true);
if ($decoded !== null) {
$replyData['content'] = $decoded;
}
}
return ResponseHelper::success($replyData, '创建成功');
} catch (\Exception $e) {
return ResponseHelper::error('创建失败:' . $e->getMessage());
}
@@ -232,9 +261,46 @@ class ReplyController extends BaseController
$sortIndex = $this->request->param('sortIndex', null);
if ($groupId !== null) $data['groupId'] = (int)$groupId;
if ($title !== null) $data['title'] = $title;
if ($title !== null) {
if ($title === '') {
return ResponseHelper::error('标题不能为空');
}
$data['title'] = $title;
}
if ($msgType !== null) $data['msgType'] = (int)$msgType;
if ($content !== null) $data['content'] = $content;
if ($content !== null) {
// 确定 msgType如果传了新的 msgType用新的否则用原有的
$currentMsgType = $msgType !== null ? (int)$msgType : null;
if ($currentMsgType === null) {
// 需要查询原有的 msgType
$reply = Reply::where(['id' => $id, 'isDel' => 0])->find();
if (empty($reply)) {
return ResponseHelper::error('快捷语不存在');
}
$currentMsgType = $reply->msgType;
}
// 根据 msgType 处理 content3=图片43=视频49=链接 需要 JSON 编码
if (in_array($currentMsgType, [3, 43, 49])) {
// 如果 content 已经是数组,直接编码;如果是字符串,先尝试解码再编码(确保格式正确)
if (is_array($content)) {
$data['content'] = json_encode($content, JSON_UNESCAPED_UNICODE);
} elseif (is_string($content)) {
// 尝试解析,如果已经是 JSON 字符串,确保格式正确
$decoded = json_decode($content, true);
if ($decoded !== null) {
// 是有效的 JSON重新编码确保格式统一
$data['content'] = json_encode($decoded, JSON_UNESCAPED_UNICODE);
} else {
// 不是 JSON直接编码
$data['content'] = json_encode($content, JSON_UNESCAPED_UNICODE);
}
}
} else {
// 文本类型,直接使用
$data['content'] = $content;
}
}
if ($sortIndex !== null) $data['sortIndex'] = (string)$sortIndex;
if (!empty($data)) {
$data['lastUpdateTime'] = time();
@@ -245,12 +311,23 @@ class ReplyController extends BaseController
}
try {
$reply = Reply::where(['id' => $id,'isDel' => 0])->find();
$reply = Reply::where(['id' => $id, 'isDel' => 0])->find();
if (empty($reply)) {
return ResponseHelper::error('快捷语不存在');
}
$reply->save($data);
return ResponseHelper::success($reply->toArray(), '更新成功');
// 返回时解析 content与 buildGroupData 保持一致)
$replyData = $reply->toArray();
$finalMsgType = isset($data['msgType']) ? $data['msgType'] : $reply->msgType;
if (in_array($finalMsgType, [3, 43, 49]) && !empty($replyData['content'])) {
$decoded = json_decode($replyData['content'], true);
if ($decoded !== null) {
$replyData['content'] = $decoded;
}
}
return ResponseHelper::success($replyData, '更新成功');
} catch (\Exception $e) {
return ResponseHelper::error('更新失败:' . $e->getMessage());
}
@@ -329,10 +406,24 @@ class ReplyController extends BaseController
// 获取该分组下的快捷回复
$replies = Reply::where($replyWhere)
->order('sortIndex asc, id desc
')
->order('sortIndex asc, id desc')
->select();
// 解析 replies 的 content 字段(根据 msgType 判断是否需要 JSON 解析)
$repliesArray = [];
foreach ($replies as $reply) {
$replyData = $reply->toArray();
// 根据 msgType 解析 content3=图片43=视频49=链接
if (in_array($replyData['msgType'], [3, 43, 49]) && !empty($replyData['content'])) {
$decoded = json_decode($replyData['content'], true);
// 如果解析成功,使用解析后的内容;否则保持原样
if ($decoded !== null) {
$replyData['content'] = $decoded;
}
}
$repliesArray[] = $replyData;
}
return [
'id' => $group->id,
'groupName' => $group->groupName,
@@ -342,7 +433,7 @@ class ReplyController extends BaseController
'replys' => $group->replys,
'companyId' => $group->companyId,
'userId' => $group->userId,
'replies' => $replies->toArray(),
'replies' => $repliesArray,
'children' => [] // 子分组
];
}