Files
ulthon_admin/extend/base/admin/service/adminUpdateCodeData/v2.5.0.php

236 lines
8.5 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
/**
* @internal-framework
*
* 此文件为框架内置功能
*
* 用途框架版本更新代码v2.5.0- S3 / WebDAV 存储支持存量升级
* 维护者:框架维护者
*
* 变更:
* - system_config 新增 upload 组 12 条存储配置(与 adminInitData/SystemConfig.php 种子一致)
* s3_access_key / s3_secret_key / s3_region / s3_bucket / s3_endpoint /
* s3_path_style / s3_domain
* webdav_url / webdav_username / webdav_password / webdav_prefix / webdav_domain
* - system_config.upload_allow_type 追加合并 s3、webdav保留用户自定义值与原顺序
* - 清理 sysconfig 缓存tag=sysconfigTTL 3600s脚本直改 DB 不清则
* in: 校验 1 小时内拒绝新上传方式)
*
* 设计说明:
* - 幂等 check-then-insert新配置用 (group, name) 检查存在性
* - upload_allow_type 为追加合并而非整体覆盖:仅在缺失时 append已有自定义值不动
* - 表前缀取默认连接配置database.default 指向的连接),不写死 mysql 连接——
* 本项目默认连接是 main自定义前缀项目上 connections.mysql.prefix 会取错前缀
*
* 注意:此文件属于框架内核,业务开发者不应修改
*/
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;
class UpdateFunction
{
/**
* @var Input
*/
public $input;
/**
* @var Output
*/
public $output;
/**
* 待新增 sysconfig 配置upload 组S3 / WebDAV 存储).
* 字段顺序与 adminInitData/SystemConfig.php 一致name/group/value/remark/sort.
*/
public $newConfigs = [
[
'name' => 's3_access_key',
'group' => 'upload',
'value' => '填你的',
'remark' => 'S3公钥',
'sort' => 0,
],
[
'name' => 's3_secret_key',
'group' => 'upload',
'value' => '填你的',
'remark' => 'S3私钥',
'sort' => 0,
],
[
'name' => 's3_region',
'group' => 'upload',
'value' => '填你的',
'remark' => 'S3地域',
'sort' => 0,
],
[
'name' => 's3_bucket',
'group' => 'upload',
'value' => '填你的',
'remark' => 'S3空间名称',
'sort' => 0,
],
[
'name' => 's3_endpoint',
'group' => 'upload',
'value' => '',
'remark' => 'S3自定义端点(兼容MinIO等,留空用AWS官方)',
'sort' => 0,
],
[
'name' => 's3_path_style',
'group' => 'upload',
'value' => '0',
'remark' => 'S3路径风格(1开启,MinIO建议)',
'sort' => 0,
],
[
'name' => 's3_domain',
'group' => 'upload',
'value' => '填你的',
'remark' => 'S3访问域名',
'sort' => 0,
],
[
'name' => 'webdav_url',
'group' => 'upload',
'value' => '填你的',
'remark' => 'WebDAV服务地址',
'sort' => 0,
],
[
'name' => 'webdav_username',
'group' => 'upload',
'value' => '填你的',
'remark' => 'WebDAV账户',
'sort' => 0,
],
[
'name' => 'webdav_password',
'group' => 'upload',
'value' => '填你的',
'remark' => 'WebDAV密码',
'sort' => 0,
],
[
'name' => 'webdav_prefix',
'group' => 'upload',
'value' => '',
'remark' => 'WebDAV存储前缀',
'sort' => 0,
],
[
'name' => 'webdav_domain',
'group' => 'upload',
'value' => '填你的',
'remark' => 'WebDAV访问域名',
'sort' => 0,
],
];
/**
* upload_allow_type 待追加的上传方式(追加合并,不覆盖已有值).
*/
public $allowTypeAppend = ['s3', 'webdav'];
public function update()
{
$this->output->writeln('更新代码');
$this->output->info('v2.5.0S3 / WebDAV 存储支持存量升级(补齐 12 条 sysconfig + upload_allow_type 追加 s3、webdav');
// 表前缀从默认连接取database.default 指向的连接),不能写死 connections.mysql
$prefix = config('database.connections.' . config('database.default') . '.prefix', 'ul_');
$configTable = $prefix . 'system_config';
// 确认表存在(防御性检查)
$tablesExist = Db::query(
'SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?',
[$configTable]
);
if (empty($tablesExist)) {
$this->output->warning("{$configTable} 不存在,跳过数据补齐。请先执行 scheme:sync 或 migrate:run 初始化表结构。");
return;
}
// ==================== 新配置幂等补齐check-then-insert====================
$configAdded = 0;
$configSkipped = 0;
foreach ($this->newConfigs as $cfg) {
// 幂等:用 (group, name) 检查存在性
$exists = Db::name('system_config')
->where('group', $cfg['group'])
->where('name', $cfg['name'])
->find();
if ($exists) {
$this->output->writeln(" - 配置 {$cfg['group']}.{$cfg['name']} 已存在,跳过");
++$configSkipped;
continue;
}
Db::name('system_config')->insert($cfg);
$this->output->writeln(" - 新增配置 {$cfg['group']}.{$cfg['name']} 完成");
++$configAdded;
}
// ==================== upload_allow_type 追加合并(不覆盖用户自定义值)====================
$typesAppended = [];
$allowRow = Db::name('system_config')
->where('group', 'upload')
->where('name', 'upload_allow_type')
->find();
if (!$allowRow) {
// 不存在则插入完整默认值(与种子 adminInitData/SystemConfig.php 一致)
Db::name('system_config')->insert([
'name' => 'upload_allow_type',
'group' => 'upload',
'value' => 'local_public,alioss,qnoss,txcos,s3,webdav',
'remark' => '可用的上传文件方式',
'sort' => 0,
]);
$this->output->writeln(' - 新增配置 upload.upload_allow_type 完成(默认全集)');
++$configAdded;
} else {
// 追加合并:过滤空串项后逐个 append保持原顺序与用户自定义值不动
$types = array_values(array_filter(explode(',', (string) $allowRow['value']), function ($item) {
return $item !== '';
}));
foreach ($this->allowTypeAppend as $type) {
if (!in_array($type, $types, true)) {
$types[] = $type;
$typesAppended[] = $type;
}
}
if (!empty($typesAppended)) {
Db::name('system_config')
->where('id', $allowRow['id'])
->update(['value' => implode(',', $types)]);
$this->output->writeln(' - 配置 upload.upload_allow_type 追加 ' . implode(',', $typesAppended) . ' 完成:' . implode(',', $types));
} else {
$this->output->writeln(' - 配置 upload.upload_allow_type 已含 ' . implode(',', $this->allowTypeAppend) . ',跳过');
}
}
// ==================== 清理 sysconfig 缓存 ====================
// 脚本直改 DB 绕过了 sysconfig() 的缓存失效逻辑tag=sysconfigTTL 3600s
// 不清理则表单 in: 校验在缓存过期前会拒绝新上传方式
Cache::tag('sysconfig')->clear();
$this->output->writeln(' - 已清理 sysconfig 缓存tag=sysconfig');
$summary = "本次新增 {$configAdded} 条配置";
if (!empty($typesAppended)) {
$summary .= 'upload_allow_type 追加 ' . implode(',', $typesAppended);
}
$summary .= ",跳过 {$configSkipped} 条已存在";
$this->output->info($summary);
$this->output->writeln('更新代码完成');
}
}