- 18 张公共表更名(addon/attachment/configure/links/spider_log/spider_stat/sms/notice/ad/task/prop/medal/help/card -> common_*,member_wallets->member_wallet,score_rule/score_log -> member_*,addon_config->common_addonconf),模型全部对齐新表名,Db 直引用清零 - Attachment 模型补 表名绑定,修复富文本上传查 wxapp_attachment 1146 隐患 - install.sql + 迁移 SQL:backend_admin/backend_role delete_at 默认 0,修复软删除(NULL != 0)误过滤导致后台菜单为空 - AppService::boot() 支持插件 info.php 声明 commands 自动注册插件命令(psr-4 自动加载,坏类名自动跳过) - 各插件(haonav/mqttbroker/wxchat/articles/blog/forum 等)字段与配置同步调整 - 框架版本 1.1.0 -> 1.1.1
152 lines
5.4 KiB
PHP
152 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ywxapp\model;
|
|
|
|
use ywxapp\model\BaseModel;
|
|
use think\model\concern\SoftDelete;
|
|
|
|
/**
|
|
* 站点公告
|
|
*/
|
|
class CommonNotice extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
|
|
// 类型:1系统维护 2活动公告 3版本更新 4其他
|
|
public const TYPE_MAINTAIN = 1;
|
|
public const TYPE_ACTIVITY = 2;
|
|
public const TYPE_UPDATE = 3;
|
|
public const TYPE_OTHER = 4;
|
|
|
|
// 字段类型转换
|
|
protected $type = [
|
|
'type' => 'integer',
|
|
'is_top' => 'integer',
|
|
'start_time' => 'integer',
|
|
'end_time' => 'integer',
|
|
'view_count' => 'integer',
|
|
'sort' => 'integer',
|
|
'status' => 'integer',
|
|
'create_at' => 'integer',
|
|
'update_at' => 'integer',
|
|
'delete_at' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* 模型配置(BaseModel::applyOptions 约定)
|
|
*/
|
|
protected function getOptions(): array
|
|
{
|
|
return [
|
|
'strict' => true,
|
|
'autoWriteTimestamp' => 'int',
|
|
'createTime' => 'create_at',
|
|
'updateTime' => 'update_at',
|
|
'readonly' => ['view_count']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 公告类型映射
|
|
*/
|
|
public static function typeList(): array
|
|
{
|
|
return [
|
|
self::TYPE_MAINTAIN => '系统维护',
|
|
self::TYPE_ACTIVITY => '活动公告',
|
|
self::TYPE_UPDATE => '版本更新',
|
|
self::TYPE_OTHER => '其他',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 取全站生效的置顶公告(用于前台公告条)。
|
|
* 过滤:status=1 + 在有效期内(start_time<=now<=end_time 或 0=长期) + is_top=1
|
|
*/
|
|
public static function getActiveTopNotices(): array
|
|
{
|
|
self::ensureSchema();
|
|
$now = time();
|
|
return (new self())
|
|
->where('status', 1)
|
|
->where('is_top', 1)
|
|
->where(function ($q) use ($now) {
|
|
$q->where('start_time', 0)->whereOr('start_time', '<=', $now);
|
|
})
|
|
->where(function ($q) use ($now) {
|
|
$q->where('end_time', 0)->whereOr('end_time', '>=', $now);
|
|
})
|
|
->order('sort', 'desc')
|
|
->order('id', 'desc')
|
|
->limit(5)
|
|
->select()
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 取前台公告列表(分页),按置顶优先 + 排序 + 时间倒序
|
|
*/
|
|
public static function getList(int $page = 1, int $limit = 10, int $type = 0): \think\Paginator
|
|
{
|
|
self::ensureSchema();
|
|
$now = time();
|
|
$query = (new self())
|
|
->where('status', 1)
|
|
->where(function ($q) use ($now) {
|
|
$q->where('start_time', 0)->whereOr('start_time', '<=', $now);
|
|
})
|
|
->where(function ($q) use ($now) {
|
|
$q->where('end_time', 0)->whereOr('end_time', '>=', $now);
|
|
});
|
|
if ($type > 0) {
|
|
$query->where('type', $type);
|
|
}
|
|
return $query->order('is_top', 'desc')
|
|
->order('sort', 'desc')
|
|
->order('id', 'desc')
|
|
->paginate(['page' => $page, 'list_rows' => $limit]);
|
|
}
|
|
|
|
/**
|
|
* 运行时结构自愈:老库缺新列时自动补齐(install.sql 为唯一事实源,此处兜底)
|
|
*/
|
|
public static function ensureSchema(): void
|
|
{
|
|
try {
|
|
$p = self::currentPrefix();
|
|
$prefix = $p;
|
|
$table = $prefix . 'common_notice';
|
|
// 缺表建表(install.sql 同款 DDL,运行时前缀兜底)
|
|
self::ensureTable($table, "CREATE TABLE IF NOT EXISTS `{$p}common_notice` (
|
|
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
|
`title` varchar(200) NOT NULL DEFAULT '' COMMENT '公告标题',
|
|
`content` text COMMENT '公告内容',
|
|
`author` varchar(50) NOT NULL DEFAULT '' COMMENT '发布人',
|
|
`type` tinyint(1) NOT NULL DEFAULT 1 COMMENT '类型 1系统维护 2活动公告 3版本更新 4其他',
|
|
`is_top` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否置顶 0否 1是',
|
|
`start_time` int(11) NOT NULL DEFAULT 0 COMMENT '展示开始时间 0=长期',
|
|
`end_time` int(11) NOT NULL DEFAULT 0 COMMENT '展示结束时间 0=长期',
|
|
`view_count` int(11) NOT NULL DEFAULT 0 COMMENT '浏览量',
|
|
`sort` int(11) NOT NULL DEFAULT 0 COMMENT '排序',
|
|
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态 0禁用 1启用',
|
|
`create_at` int(11) NOT NULL DEFAULT 0 COMMENT '创建时间',
|
|
`update_at` int(11) NOT NULL DEFAULT 0 COMMENT '更新时间',
|
|
`delete_at` int(11) NOT NULL DEFAULT 0 COMMENT '删除时间',
|
|
PRIMARY KEY (`id`),
|
|
KEY `idx_type` (`type`),
|
|
KEY `idx_is_top` (`is_top`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='站点公告'");
|
|
// 老库扩展列兜底(幂等)
|
|
self::ensureColumn($table, 'type', "tinyint(1) NOT NULL DEFAULT 1 COMMENT '公告类型 1系统维护 2活动公告 3版本更新 4其他'");
|
|
self::ensureColumn($table, 'is_top', "tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否置顶 0否 1是'");
|
|
self::ensureColumn($table, 'start_time', "int(11) NOT NULL DEFAULT 0 COMMENT '展示开始时间 0=长期'");
|
|
self::ensureColumn($table, 'end_time', "int(11) NOT NULL DEFAULT 0 COMMENT '展示结束时间 0=长期'");
|
|
self::ensureColumn($table, 'view_count', "int(11) NOT NULL DEFAULT 0 COMMENT '浏览量'");
|
|
} catch (\Throwable $e) {
|
|
// 忽略自愈失败,由业务暴露
|
|
}
|
|
}
|
|
}
|