chore: 优化上传分类规则并新增分享功能
- 新增上传分类归一化处理,统一白名单校验规则 - 新增COS存储记录同步逻辑 - 新增分享功能支持,配置多页面分享参数 - 新增订单案例分享按钮 - 新增订单自动校验和回收命令 - 新增测试脚本和图片同步脚本 - 优化后台上传分类配置,统一分类映射
这个提交包含在:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\command;
|
||||
|
||||
use think\Config;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
use think\Loader;
|
||||
|
||||
/**
|
||||
* 孤儿附件扫描:检查 fa_attachment 中的文件是否被任何业务表引用
|
||||
*
|
||||
* 用法:
|
||||
* php think attachment:scan 仅扫描,输出孤儿清单
|
||||
* php think attachment:scan --delete 扫描并删除孤儿(记录 + 本地文件 + COS 对象)
|
||||
*/
|
||||
class AttachmentScan extends Command
|
||||
{
|
||||
/**
|
||||
* 命令配置
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('attachment:scan')
|
||||
->addOption('delete', null, Option::VALUE_NONE, '删除孤儿附件(记录+文件+COS对象)')
|
||||
->setDescription('扫描无业务引用的孤儿附件');
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令执行
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$config = Config::get('database');
|
||||
$database = $config['database'];
|
||||
$prefix = $config['prefix'];
|
||||
|
||||
// 1. 收集业务表所有文本类列(排除附件表自身)
|
||||
$columns = Db::query(
|
||||
"SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS "
|
||||
. "WHERE TABLE_SCHEMA = ? AND DATA_TYPE IN ('varchar','char','text','tinytext','mediumtext','longtext') "
|
||||
. "AND TABLE_NAME NOT IN (?, CONCAT(?, 'attachment'))",
|
||||
[$database, $database . '.' . $prefix . 'attachment', $prefix]
|
||||
);
|
||||
if (!$columns) {
|
||||
$output->writeln('<error>未找到业务表列</error>');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 遍历附件记录,逐列检查 url 引用(每列一次查询,OR 组合全部 url)
|
||||
$attachments = Db::name('attachment')->field('id,url,storage,filename,uploadtime')->select();
|
||||
$urls = array_column($attachments, 'url');
|
||||
$referenced = []; // 有引用的 url 集合
|
||||
foreach ($columns as $col) {
|
||||
$table = $col['TABLE_NAME'];
|
||||
$field = $col['COLUMN_NAME'];
|
||||
$wheres = [];
|
||||
$bind = [];
|
||||
foreach ($urls as $i => $url) {
|
||||
$wheres[] = "`{$field}` LIKE :u{$i}";
|
||||
$bind["u{$i}"] = '%' . addcslashes($url, '%_') . '%';
|
||||
}
|
||||
$sql = "SELECT `{$field}` AS v FROM `{$table}` WHERE " . implode(' OR ', $wheres) . " LIMIT 500";
|
||||
try {
|
||||
$rows = Db::query($sql, $bind);
|
||||
} catch (\Exception $e) {
|
||||
continue; // 个别列类型不兼容时跳过
|
||||
}
|
||||
foreach ($rows as $row) {
|
||||
$v = (string)$row['v'];
|
||||
foreach ($urls as $url) {
|
||||
if ($url !== '' && strpos($v, $url) !== false) {
|
||||
$referenced[$url] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 输出孤儿清单
|
||||
$orphans = array_values(array_filter($attachments, function ($a) use ($referenced) {
|
||||
return empty($referenced[$a['url']]);
|
||||
}));
|
||||
if (!$orphans) {
|
||||
$output->writeln('<info>扫描完成:所有附件均有业务引用,无孤儿文件</info>');
|
||||
return;
|
||||
}
|
||||
$output->writeln(sprintf('扫描完成:共 %d 条附件,孤儿 %d 条:', count($attachments), count($orphans)));
|
||||
foreach ($orphans as $o) {
|
||||
$output->writeln(sprintf(' [%d] %s (%s) %s', $o['id'], $o['url'], $o['storage'], date('Y-m-d', $o['uploadtime'])));
|
||||
}
|
||||
|
||||
// 4. 可选删除:手动清理本地文件 / COS 对象 / 记录
|
||||
if ($input->getOption('delete')) {
|
||||
Loader::addNamespace('Qcloud\Cos', ROOT_PATH . 'addons/cos/library/Qcloud/Cos/');
|
||||
$cosConfig = get_addon_config('cos');
|
||||
$client = null;
|
||||
if ($cosConfig && !empty($cosConfig['cdnurl'])) {
|
||||
$client = new \Qcloud\Cos\Client([
|
||||
'region' => $cosConfig['region'],
|
||||
'schema' => 'https',
|
||||
'credentials' => ['secretId' => $cosConfig['secretId'], 'secretKey' => $cosConfig['secretKey']],
|
||||
]);
|
||||
}
|
||||
foreach ($orphans as $o) {
|
||||
$id = $o['id'];
|
||||
// 同 URL 成对记录一起删
|
||||
$pairs = Db::name('attachment')->where('url', $o['url'])->select();
|
||||
foreach ($pairs as $pair) {
|
||||
if ($pair['storage'] === 'local') {
|
||||
$file = ROOT_PATH . 'public' . $pair['url'];
|
||||
if (is_file($file)) {
|
||||
@unlink($file);
|
||||
}
|
||||
} elseif ($pair['storage'] === 'cos' && $client) {
|
||||
try {
|
||||
$client->deleteObject(['Bucket' => $cosConfig['bucket'], 'Key' => ltrim($pair['url'], '/')]);
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<comment>COS删除失败: ' . $pair['url'] . ' ' . $e->getMessage() . '</comment>');
|
||||
}
|
||||
}
|
||||
Db::name('attachment')->where('id', $pair['id'])->delete();
|
||||
}
|
||||
$output->writeln('<info>已删除: ' . $o['url'] . '</info>');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\command;
|
||||
|
||||
use app\common\library\OrderStatus;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 自动核销完工超过48小时未确认的工单守护任务
|
||||
* 运行方式:php think order:autoverify
|
||||
*/
|
||||
class OrderAutoverify extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('order:autoverify')
|
||||
->setDescription('自动核销完工超过48小时未确认的工单');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$threshold = time() - (48 * 3600);
|
||||
$orders = Db::name('order')
|
||||
->where('status', OrderStatus::PENDING_VERIFY)
|
||||
->where('finish_time', '>', 0)
|
||||
->where('finish_time', '<=', $threshold)
|
||||
->select();
|
||||
|
||||
$count = count($orders);
|
||||
$output->writeln("[OrderAutoverify] 扫描到 {$count} 笔超48小时待核销工单");
|
||||
|
||||
$success = 0;
|
||||
foreach ($orders as $order) {
|
||||
$orderId = (int)$order['id'];
|
||||
$masterId = (int)$order['master_id'];
|
||||
$payAmount = (float)$order['pay_amount'];
|
||||
$rate = 10.00;
|
||||
$commission = round($payAmount * $rate / 100, 2);
|
||||
$settleAmount = round($payAmount - $commission, 2);
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 1. 更新订单为已完成
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'status' => OrderStatus::COMPLETED,
|
||||
'verify_time' => time(),
|
||||
'settle_status' => 1,
|
||||
'settle_amount' => $settleAmount,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
// 2. 插入订单日志
|
||||
Db::name('order_log')->insert([
|
||||
'order_id' => $orderId,
|
||||
'from_status'=> OrderStatus::PENDING_VERIFY,
|
||||
'to_status' => OrderStatus::COMPLETED,
|
||||
'remark' => '完工满48小时客户未确认,系统自动验收结算',
|
||||
'operator' => 'system',
|
||||
'op_id' => 0,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
// 3. 写入结算表
|
||||
Db::name('settle')->insert([
|
||||
'order_id' => $orderId,
|
||||
'master_id' => $masterId,
|
||||
'order_amount' => $payAmount,
|
||||
'commission_rate' => $rate,
|
||||
'commission' => $commission,
|
||||
'settle_amount' => $settleAmount,
|
||||
'status' => 'settled',
|
||||
'settle_time' => time(),
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
// 4. 师傅钱包入账(行级悲观锁)
|
||||
if ($masterId > 0) {
|
||||
$master = Db::name('master')->where('id', $masterId)->lock(true)->find();
|
||||
$newMoney = round((float)$master['money'] + $settleAmount, 2);
|
||||
Db::name('master')->where('id', $masterId)->update([
|
||||
'money' => $newMoney,
|
||||
'order_count' => (int)$master['order_count'] + 1,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
Db::name('wallet_log')->insert([
|
||||
'user_type' => 'master',
|
||||
'user_id' => $masterId,
|
||||
'type' => 'settle',
|
||||
'amount' => $settleAmount,
|
||||
'balance' => $newMoney,
|
||||
'order_id' => $orderId,
|
||||
'remark' => '订单超时自动核销结算入账',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
// 通知师傅
|
||||
Db::name('message')->insert([
|
||||
'user_type' => 'master',
|
||||
'user_id' => (int)$master['user_id'],
|
||||
'title' => '订单自动核销结算入账',
|
||||
'content' => "订单 {$order['order_no']} 完工满48小时已自动验收,收入 ¥{$settleAmount}",
|
||||
'type' => 'order',
|
||||
'target_id' => $orderId,
|
||||
'is_read' => 0,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$success++;
|
||||
$output->writeln(" [OK] 订单 #{$orderId} ({$order['order_no']}) 自动核销完成,结算 ¥{$settleAmount}");
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$output->writeln(" [FAIL] 订单 #{$orderId} 自动核销异常: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln("[OrderAutoverify] 执行结束,成功自动核销 {$success} 笔");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\command;
|
||||
|
||||
use app\common\library\OrderStatus;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 师傅接单超过2小时未首联打卡超时回收守护任务
|
||||
* 运行方式:php think order:recycle
|
||||
*/
|
||||
class OrderRecycle extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('order:recycle')
|
||||
->setDescription('回收接单超过2小时未首联打卡的超时工单');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
// 阈值:当前时间倒推 2 小时
|
||||
$threshold = time() - (2 * 3600);
|
||||
|
||||
// 状态为已接单(3),且未首联打卡(contact_time 为 0 或 NULL),且接单时间超 2 小时
|
||||
$orders = Db::name('order')
|
||||
->where('status', OrderStatus::ACCEPTED)
|
||||
->where(function ($query) {
|
||||
$query->whereNull('contact_time')->whereOr('contact_time', 0);
|
||||
})
|
||||
->where('accept_time', '>', 0)
|
||||
->where('accept_time', '<=', $threshold)
|
||||
->select();
|
||||
|
||||
$count = count($orders);
|
||||
$output->writeln("[OrderRecycle] 扫描到 {$count} 笔接单超2小时未首联工单");
|
||||
|
||||
$recycled = 0;
|
||||
foreach ($orders as $order) {
|
||||
$orderId = (int)$order['id'];
|
||||
$masterId = (int)$order['master_id'];
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 1. 退回待接单状态(公海接单大厅)
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'status' => OrderStatus::PENDING_ACCEPT,
|
||||
'master_id' => 0,
|
||||
'accept_time' => 0,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
// 2. 插入订单状态变迁日志
|
||||
Db::name('order_log')->insert([
|
||||
'order_id' => $orderId,
|
||||
'from_status'=> OrderStatus::ACCEPTED,
|
||||
'to_status' => OrderStatus::PENDING_ACCEPT,
|
||||
'remark' => '师傅接单后超2小时未联系客户,系统自动退回公海大厅',
|
||||
'operator' => 'system',
|
||||
'op_id' => 0,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
// 3. 对原接单师傅扣减信用分(扣 2 分)与推送警告
|
||||
if ($masterId > 0) {
|
||||
$master = Db::name('master')->where('id', $masterId)->lock(true)->find();
|
||||
if ($master) {
|
||||
$newScore = max(0, (float)$master['credit_score'] - 2.0);
|
||||
Db::name('master')->where('id', $masterId)->update([
|
||||
'credit_score' => $newScore,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
// 记录信誉流水表(如表存在)
|
||||
if (Db::query("SHOW TABLES LIKE '%master_credit_log%'")) {
|
||||
Db::name('master_credit_log')->insert([
|
||||
'master_id' => $masterId,
|
||||
'type' => 'contact_timeout',
|
||||
'score' => -2.0,
|
||||
'balance' => $newScore,
|
||||
'order_id' => $orderId,
|
||||
'remark' => '接单后超2小时未首联,违规扣除信誉分',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
// 给师傅发送通知
|
||||
Db::name('message')->insert([
|
||||
'user_type' => 'master',
|
||||
'user_id' => (int)$master['user_id'],
|
||||
'title' => '接单超时退单警告',
|
||||
'content' => "订单 {$order['order_no']} 因超过2小时未联系客户,已被系统回收退回公海,扣除信誉分 2 分",
|
||||
'type' => 'order',
|
||||
'target_id' => $orderId,
|
||||
'is_read' => 0,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$recycled++;
|
||||
$output->writeln(" [OK] 订单 #{$orderId} ({$order['order_no']}) 超时退单成功,已重置回公海大厅");
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$output->writeln(" [FAIL] 订单 #{$orderId} 超时退单失败: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln("[OrderRecycle] 执行结束,成功回收 {$recycled} 笔");
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户