触客保优化

This commit is contained in:
wong
2026-01-04 15:58:47 +08:00
parent 64358df1c1
commit 08d2a811b7

View File

@@ -29,6 +29,12 @@ class MomentsController extends BaseController
$timingTime = $this->request->param('timingTime', date('Y-m-d H:i:s')); // 定时发布时间
$immediately = $this->request->param('immediately', false); // 是否立即发布
// 格式化时间字符串为统一格式
$timingTime = $this->normalizeTimingTime($timingTime);
if ($timingTime === false) {
return ResponseHelper::error('定时发布时间格式不正确');
}
// 参数验证
if (empty($text) && empty($picUrlList) && empty($videoUrl)) {
return ResponseHelper::error('朋友圈内容不能为空');
@@ -174,6 +180,12 @@ class MomentsController extends BaseController
$timingTime = $this->request->param('timingTime', date('Y-m-d H:i:s'));
$immediately = $this->request->param('immediately', false);
// 格式化时间字符串为统一格式
$timingTime = $this->normalizeTimingTime($timingTime);
if ($timingTime === false) {
return ResponseHelper::error('定时发布时间格式不正确');
}
// 读取待编辑记录
/** @var KfMoments|null $moments */
$moments = KfMoments::where(['id' => $id, 'companyId' => $companyId, 'userId' => $userId, 'isDel' => 0])->find();
@@ -427,4 +439,66 @@ class MomentsController extends BaseController
return ResponseHelper::error('删除失败:' . $e->getMessage());
}
}
/**
* 规范化时间字符串为 Y-m-d H:i:s 格式
* 支持多种时间格式:
* - "2026年1月5日15:43:00"
* - "2026-01-05 15:43:00"
* - "2026/01/05 15:43:00"
* - 时间戳
* @param string|int $timingTime 时间字符串或时间戳
* @return string|false 格式化后的时间字符串失败返回false
*/
private function normalizeTimingTime($timingTime)
{
if (empty($timingTime)) {
return date('Y-m-d H:i:s');
}
// 如果是时间戳
if (is_numeric($timingTime) && strlen($timingTime) == 10) {
return date('Y-m-d H:i:s', $timingTime);
}
// 如果是毫秒时间戳
if (is_numeric($timingTime) && strlen($timingTime) == 13) {
return date('Y-m-d H:i:s', intval($timingTime / 1000));
}
// 如果已经是标准格式,直接返回
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $timingTime)) {
return $timingTime;
}
// 处理中文日期格式2026年1月5日15:43:00 或 2026年01月05日15:43:00
if (preg_match('/^(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2}):(\d{1,2}):(\d{1,2})$/', $timingTime, $matches)) {
$year = $matches[1];
$month = str_pad($matches[2], 2, '0', STR_PAD_LEFT);
$day = str_pad($matches[3], 2, '0', STR_PAD_LEFT);
$hour = str_pad($matches[4], 2, '0', STR_PAD_LEFT);
$minute = str_pad($matches[5], 2, '0', STR_PAD_LEFT);
$second = str_pad($matches[6], 2, '0', STR_PAD_LEFT);
return "{$year}-{$month}-{$day} {$hour}:{$minute}:{$second}";
}
// 处理中文日期格式无秒2026年1月5日15:43
if (preg_match('/^(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2}):(\d{1,2})$/', $timingTime, $matches)) {
$year = $matches[1];
$month = str_pad($matches[2], 2, '0', STR_PAD_LEFT);
$day = str_pad($matches[3], 2, '0', STR_PAD_LEFT);
$hour = str_pad($matches[4], 2, '0', STR_PAD_LEFT);
$minute = str_pad($matches[5], 2, '0', STR_PAD_LEFT);
return "{$year}-{$month}-{$day} {$hour}:{$minute}:00";
}
// 尝试使用 strtotime 解析其他格式
$timestamp = strtotime($timingTime);
if ($timestamp !== false) {
return date('Y-m-d H:i:s', $timestamp);
}
// 如果所有方法都失败,返回 false
return false;
}
}