Files
YwxAppThink/addon/paycenter/library/Schema.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

115 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace addon\paycenter\library;
use think\facade\Db;
/**
* 支付中心表结构自愈。
*
* 事实源:addon/paycenter/install.sql(插件自带,随插件分发)。
* 安装时由 AddonService::importsql() 导入;运行期(下单 / 回调 / 后台流水页)再兜底一次,
* 保证「插件已启用但表被误删 / 老库缺列」时不炸。
*
* 幂等:缺表建表、缺列补列都是 SHOW 探测,重复调用安全,进程内只探测一次。
*/
class Schema
{
/** 插件私有表(不含表前缀) */
public const TABLES = ['paycenter_trade'];
/** 老库补列:[表 => [列 => 列定义]] */
private const COLUMNS = [
'paycenter_trade' => [
'biz' => '`biz` varchar(30) NOT NULL DEFAULT \'\' COMMENT \'业务标识\'',
'biz_id' => '`biz_id` int NOT NULL DEFAULT 0 COMMENT \'业务侧订单主键\'',
'scene' => '`scene` varchar(20) NOT NULL DEFAULT \'\' COMMENT \'支付场景\'',
'attach' => '`attach` varchar(500) NOT NULL DEFAULT \'\' COMMENT \'业务附加数据\'',
'notify_at' => '`notify_at` int NOT NULL DEFAULT 0 COMMENT \'回调时间\'',
],
];
private static bool $done = false;
/** install.sql 解析缓存:[不含前缀表名 => CREATE 语句] */
private static array $ddl = [];
public static function ensure(): void
{
if (self::$done) {
return;
}
self::$done = true;
$prefix = self::prefix();
foreach (self::ddl() as $table => $statement) {
$full = $prefix . $table;
if (self::tableExists($full)) {
continue;
}
try {
Db::execute(str_replace('__PREFIX__', $prefix, $statement));
} catch (\Throwable $e) {
// 建表失败不阻断业务:后续写库会再暴露真实错误
trace('[paycenter] 建表失败 ' . $full . ':' . $e->getMessage(), 'error');
}
}
foreach (self::COLUMNS as $table => $columns) {
$full = $prefix . $table;
if (!self::tableExists($full)) {
continue;
}
foreach ($columns as $col => $def) {
try {
if (empty(Db::query("SHOW COLUMNS FROM `{$full}` LIKE '{$col}'"))) {
Db::execute("ALTER TABLE `{$full}` ADD COLUMN {$def}");
}
} catch (\Throwable) {
// 补列失败不阻断
}
}
}
}
public static function prefix(): string
{
$default = (string) config('database.default', 'mysql');
return (string) config("database.connections.{$default}.prefix", '');
}
private static function tableExists(string $full): bool
{
try {
return !empty(Db::query("SHOW TABLES LIKE '{$full}'"));
} catch (\Throwable) {
return false;
}
}
/**
* 解析插件自带 install.sql,取出建表语句。
* @return array<string,string>
*/
private static function ddl(): array
{
if (self::$ddl !== []) {
return self::$ddl;
}
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'install.sql';
if (!is_file($file)) {
return self::$ddl = [];
}
$sql = (string) file_get_contents($file);
if (preg_match_all('/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+`?__PREFIX__(\w+)`?\s*\(.*?\)\s*[^;]*;/is', $sql, $m)) {
foreach ($m[1] as $i => $table) {
self::$ddl[$table] = $m[0][$i];
}
}
return self::$ddl;
}
}