82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace app\adminapi\logic\popup;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\popup\PagePopup;
|
|
use app\common\model\popup\PagePopupLog;
|
|
|
|
class PopupLogic extends BaseLogic
|
|
{
|
|
/**
|
|
* 表单字段白名单
|
|
*/
|
|
private static array $fields = [
|
|
'name', 'page_path', 'popup_type', 'image', 'title', 'content',
|
|
'link_type', 'link_url', 'frequency', 'delay_seconds', 'auto_close_seconds',
|
|
'start_time', 'end_time', 'target_platform', 'sort', 'status',
|
|
];
|
|
|
|
public static function add(array $params): void
|
|
{
|
|
$data = self::filterFields($params);
|
|
PagePopup::create($data);
|
|
}
|
|
|
|
public static function edit(array $params): bool
|
|
{
|
|
try {
|
|
$data = self::filterFields($params);
|
|
$data['id'] = (int)$params['id'];
|
|
PagePopup::update($data);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function delete(array $params): void
|
|
{
|
|
$id = (int)$params['id'];
|
|
PagePopup::destroy($id);
|
|
// 同时清理日志
|
|
PagePopupLog::where('popup_id', $id)->delete();
|
|
}
|
|
|
|
public static function detail(array $params): array
|
|
{
|
|
return PagePopup::findOrEmpty((int)$params['id'])->toArray();
|
|
}
|
|
|
|
public static function updateStatus(array $params): bool
|
|
{
|
|
PagePopup::update([
|
|
'id' => (int)$params['id'],
|
|
'status' => (int)$params['status'],
|
|
]);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 字段白名单过滤 + 时间格式归一化
|
|
*/
|
|
private static function filterFields(array $params): array
|
|
{
|
|
$data = [];
|
|
foreach (self::$fields as $f) {
|
|
if (array_key_exists($f, $params)) {
|
|
$data[$f] = $params[$f];
|
|
}
|
|
}
|
|
// start_time / end_time 支持字符串日期,统一转时间戳
|
|
foreach (['start_time', 'end_time'] as $tf) {
|
|
if (isset($data[$tf]) && !is_numeric($data[$tf])) {
|
|
$ts = strtotime((string)$data[$tf]);
|
|
$data[$tf] = $ts ?: 0;
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
}
|