Commit e90e3354 authored by 刘小敏's avatar 刘小敏

优化试戴结果为双耳都戴;优化试戴日志记录到表格;

parent 130742cd
<?php
namespace addons\shopro\model;
use think\Model;
/**
* 数据库日志模型(每月一张表)
*/
class LogDb extends Model
{
/**
* 获取当前月份的表名
*/
public static function getTableName($year = null, $month = null)
{
$year = $year ?: date('Y');
$month = $month ?: date('m');
return 'fa_shopro_log_' . $year . str_pad($month, 2, '0', STR_PAD_LEFT);
}
/**
* 动态设置表名
*/
public function __construct($data = [])
{
// 使用 table 属性设置完整表名(包含前缀),避免 ThinkPHP 重复添加前缀
$this->table = self::getTableName();
parent::__construct($data);
}
/**
* 写入日志
* @param array $data
* @return bool
*/
public static function write($data)
{
// 确保表存在
self::ensureTableExists();
$model = new self();
$insertData = [
'user_id' => $data['user_id'] ?? 0,
'user_name' => $data['user_name'] ?? '',
'log_type' => $data['log_type'] ?? 'info',
'log_category' => $data['log_category'] ?? 'system',
'log_content' => $data['log_content'] ?? '',
'ip' => $data['ip'] ?? request()->ip(),
'created_at' => date('Y-m-d H:i:s'),
];
return $model->save($insertData);
}
/**
* 确保当前月份的日志表存在
*/
public static function ensureTableExists()
{
$tableName = self::getTableName();
$db = \think\Db::connect();
if (!$db->query("SHOW TABLES LIKE '{$tableName}'")) {
self::createTable($tableName);
}
}
/**
* 创建日志表
* @param string $tableName
*/
private static function createTable($tableName)
{
$sql = "CREATE TABLE IF NOT EXISTS `{$tableName}` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` INT(11) DEFAULT '0' COMMENT '用户ID',
`user_name` VARCHAR(100) DEFAULT '' COMMENT '用户名/昵称',
`log_type` VARCHAR(50) DEFAULT 'info' COMMENT '日志级别(info/error/debug/warning)',
`log_category` VARCHAR(100) DEFAULT 'system' COMMENT '日志分类(gen_ai/order/payment等)',
`log_content` LONGTEXT COMMENT '日志内容',
`ip` VARCHAR(50) DEFAULT '' COMMENT 'IP地址',
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_log_type` (`log_type`),
KEY `idx_log_category` (`log_category`),
KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='日志表-" . date('Y年m月') . "';";
\think\Db::execute($sql);
}
/**
* 查询日志
* @param array $where
* @param int $page
* @param int $limit
* @return array
*/
public static function queryLogs($where = [], $page = 1, $limit = 20)
{
self::ensureTableExists();
$query = (new self())->order('created_at', 'desc');
if (!empty($where['user_id'])) {
$query->where('user_id', $where['user_id']);
}
if (!empty($where['log_type'])) {
$query->where('log_type', $where['log_type']);
}
if (!empty($where['log_category'])) {
$query->where('log_category', $where['log_category']);
}
if (!empty($where['start_time'])) {
$query->where('created_at', '>=', $where['start_time']);
}
if (!empty($where['end_time'])) {
$query->where('created_at', '<=', $where['end_time']);
}
if (!empty($where['keyword'])) {
$query->where('log_content', 'LIKE', '%' . $where['keyword'] . '%');
}
return $query->paginate($limit, false, ['page' => $page]);
}
}
......@@ -149,7 +149,8 @@ class Ai
$allowCateHook = config('shopro.ai_hook') ?: '';
$pic_1 = '1:1 原样复刻图 1 人物容貌、姿势、场景与光影色调,严格匹配原图宽高比,不裁切、不拉伸、不改变比例,。人像轻度自然修肤,保留皮肤原生细节,五官比例不变,杜绝网红假脸与 AI 建模违和感。';
$ext = '仅外露耳朵佩戴饰品,隐耳不添加配饰。画质高清细腻,统一原图风格,呈现真实珠宝实拍效果。';
// $ext = '仅外露耳朵佩戴饰品,隐耳不添加配饰。画质高清细腻,统一原图风格,呈现真实珠宝实拍效果。';
$ext = '若原图仅单侧露耳,需调整人物角度,露出另一只耳朵并佩戴同款耳饰,保证双侧佩戴对称自然。画质高清细腻,整体光影色调与原图统一,呈现真实电商珠宝实拍质感。';
$prompt_clip = $pic_1.'将图 2 的耳骨夹精准佩戴在耳廓外侧合适位置,紧密贴合耳部轮廓、比例适配,佩戴稳固,无悬空、变形、拉伸、重复问题。'.$ext;
$prompt_mix = $pic_1.'将图 2 叠戴耳饰组合佩戴在完整耳部区域,仅限耳垂、耳廓、耳骨位置,严格避开脸颊、面部。饰品错落搭配,紧密贴合耳部轮廓,比例协调、佩戴稳固,无悬空、变形、拉伸、错位问题。'.$ext;
......
......@@ -24,5 +24,6 @@ return [
'app\command\StatsDaily',
'app\command\TrackFailedRetry',
'app\command\DataStatsCenter',
'app\command\TestLogDb',
];
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class TestLogDb extends Command
{
protected function configure()
{
$this->setName('shopro:test-log-db')
->setDescription('测试数据库日志系统');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('开始测试数据库日志系统...');
$output->writeln('');
// 测试1:写入测试日志
$output->writeln('测试1:写入测试日志');
custom_log('这是一条测试日志', 'test', 'info');
$output->writeln('✓ 日志写入成功');
$output->writeln('');
// 测试2:写入不同级别的日志
$output->writeln('测试2:写入不同级别的日志');
custom_log('这是一条错误日志', 'test', 'error');
custom_log('这是一条调试日志', 'test', 'debug');
custom_log('这是一条警告日志', 'test', 'warning');
$output->writeln('✓ 不同级别日志写入成功');
$output->writeln('');
// 测试3:查询日志
$output->writeln('测试3:查询日志');
$logs = \addons\shopro\model\LogDb::queryLogs(['log_category' => 'test'], 1, 10);
$output->writeln('查询到 ' . $logs->total() . ' 条日志');
foreach ($logs->items() as $log) {
$time = is_numeric($log['created_at']) ? date('Y-m-d H:i:s', $log['created_at']) : $log['created_at'];
$output->writeln('[' . $time . '] [' . $log['log_type'] . '] ' . mb_substr($log['log_content'], 0, 50) . '...');
}
$output->writeln('');
$output->writeln('✓ 日志查询成功');
$output->writeln('');
$output->writeln('数据库日志系统测试完成!');
}
}
......@@ -525,5 +525,60 @@ if (!function_exists('custom_log')) {
]);
\think\Log::record($content, $level);
// 同时记录到数据库日志表
custom_log_db($content, $name, $level);
}
}
if (!function_exists('custom_log_db')) {
/**
* 记录日志到数据库(每月一张表)
*
* @param string $content 日志内容
* @param string $name 日志分类
* @param string $level 日志级别 info/error/debug/warning
* @return void
*/
function custom_log_db($content, $name = 'custom', $level = 'info')
{
try {
// 获取当前用户信息
$user_id = 0;
$user_name = '';
// 尝试获取前端用户
if (function_exists('auth_user')) {
$user = auth_user(false);
if ($user) {
$user_id = $user->id;
$user_name = $user->nickname ?? $user->username ?? '';
}
}
// 如果没有前端用户,尝试获取管理员
if ($user_id == 0 && function_exists('auth_admin')) {
$admin = auth_admin();
if ($admin) {
$user_id = $admin->id;
$user_name = 'admin_' . ($admin->username ?? '');
}
}
// 写入数据库
if (class_exists(\addons\shopro\model\LogDb::class)) {
\addons\shopro\model\LogDb::write([
'user_id' => $user_id,
'user_name' => $user_name,
'log_type' => $level,
'log_category' => $name,
'log_content' => $content,
'ip' => request()->ip(),
]);
}
} catch (\Exception $e) {
// 数据库日志记录失败不影响主流程
\think\Log::error('数据库日志写入失败: ' . $e->getMessage());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment