- 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
104 lines
3.0 KiB
PHP
104 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ywxapp\model;
|
|
|
|
use ywxapp\model\BaseModel;
|
|
|
|
use think\facade\Db;
|
|
use think\model\concern\SoftDelete;
|
|
|
|
/**
|
|
* 勋章中心
|
|
*/
|
|
class CommonMedal extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
protected function getOptions(): array
|
|
{
|
|
return [
|
|
'strict' => false,
|
|
'autoWriteTimestamp' => 'int',
|
|
'createTime' => 'create_at',
|
|
'updateTime' => 'update_at',
|
|
'deleteTime' => 'delete_at',
|
|
'defaultSoftDelete' => 0,
|
|
'append' => ['target'],
|
|
'hidden' => ['create_at', 'update_at', 'delete_at'],
|
|
'readonly' => ['id'],
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* 控制器初始化(模型实例方法,非静态)
|
|
*/
|
|
protected function initialize()
|
|
{
|
|
parent::initialize();
|
|
self::ensureSchema();
|
|
}
|
|
|
|
/**
|
|
* 授予勋章(幂等:同一用户同一勋章仅记录一次)
|
|
* @return array [bool $ok, string $msg]
|
|
*/
|
|
public static function grant(int $uid, int $medalId): array
|
|
{
|
|
self::ensureSchema(); // 确保 user_medal 关联表已就绪(静态入口也可能在未实例化模型时被调用)
|
|
if ($uid <= 0) {
|
|
return [false, '用户未登录'];
|
|
}
|
|
$medal = self::where('id', $medalId)->where('status', 1)->find();
|
|
if (!$medal) {
|
|
return [false, '勋章不存在或未启用'];
|
|
}
|
|
|
|
$exists = Db::name('member_medal')
|
|
->where('uid', $uid)
|
|
->where('medal_id', $medalId)
|
|
->find();
|
|
if ($exists) {
|
|
return [true, '已拥有该勋章'];
|
|
}
|
|
|
|
Db::name('member_medal')->insert([
|
|
'uid' => $uid,
|
|
'medal_id' => $medalId,
|
|
'create_at' => time(),
|
|
]);
|
|
return [true, '恭喜获得勋章:' . $medal->title];
|
|
}
|
|
|
|
/**
|
|
* 取某用户拥有的勋章列表(含勋章信息)
|
|
*/
|
|
public static function getUserMedals(int $uid): array
|
|
{
|
|
self::ensureSchema(); // 确保 user_medal 关联表已就绪(静态入口也可能在未实例化模型时被调用)
|
|
if ($uid <= 0) {
|
|
return [];
|
|
}
|
|
return Db::name('member_medal')
|
|
->alias('um')
|
|
->join('common_medal m', 'm.id = um.medal_id')
|
|
->where('um.uid', $uid)
|
|
->where('m.delete_at', 0)
|
|
->field('m.id,m.title,m.image,m.description,um.create_at')
|
|
->order('um.create_at', 'desc')
|
|
->select()
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 表自愈:确保勋章相关表(medal / member_medal)已就绪。
|
|
*/
|
|
public static function ensureSchema(): void
|
|
{
|
|
$prefix = BaseModel::currentPrefix();
|
|
BaseModel::ensureTableFromInstall($prefix, 'common_medal');
|
|
BaseModel::ensureTableFromInstall($prefix, 'member_medal');
|
|
}
|
|
}
|