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

3D试戴接口日志记录优化;试戴增加扣积分逻辑;生成视频异步消息通知代码初始化;

parent 4cd73cd9
......@@ -120,11 +120,18 @@ class AiTryon extends Common
}
$user = auth_user();
// 试戴一次扣5积分
$userService = new User();
$res = $userService->isTry($user->id);
$res = $userService->deductPoints($user->id, 5);
if ($res === 0) {
$this->error('积分不足 无法试戴');
}
$userService = new User();
$res = $userService->isTry($user->id);
if ($res === 0) {
$this->success('今日免费试戴次数已用完', 0);
$this->success('今日试戴次数已用完', 0);
} else {
// //开发时候先这样处理,AI图片生成次数有限
......@@ -226,6 +233,12 @@ class AiTryon extends Common
$user = auth_user();
// 试戴一次扣5积分
$userService = new User();
$res = $userService->deductPoints($user->id, 5);
if ($res === 0) {
$this->error('积分不足 无法试戴');
}
// \think\Queue::push('\addons\shopro\job\AiVideoGen@save', ['taskid' => 'cgt-20260506113558-x6wtq', 'user' => $user], 'shopro-high');
// $this->success('推送队列成功');
......@@ -233,9 +246,8 @@ class AiTryon extends Common
$userService = new User();
$res = $userService->isTry($user->id);
if ($res === 0) {
$this->success('今日免费试戴次数已用完', 0);
$this->success('今日试戴次数已用完', 0);
} else {
// 开发过程中临时代码
......
......@@ -273,8 +273,7 @@ class AiVideoGen extends BaseJob
// 获取用户信息
$user = User::find($user_id);
if (!$user) {
// Log::error('[AI视频通知]用户不存在: ' . $user_id);
$this->debug_log('[AI视频通知]用户不存在: ' . $user_id, 'gen_ai');
$this->debug_log('[AI视频通知]用户不存在: ' . $user_id);
return false;
}
......@@ -289,12 +288,10 @@ class AiVideoGen extends BaseJob
'user' => $user->toArray()
]));
// Log::info('[AI视频通知]通知发送成功: user_id=' . $user_id);
$this->debug_log('[AI视频通知]通知发送成功: user_id=' . $user_id, 'gen_ai');
$this->debug_log('[AI视频通知]通知发送成功: user_id=' . $user_id);
return true;
} catch (\Exception $e) {
// Log::error('[AI视频通知]发送失败: ' . $e->getMessage());
$this->debug_log('[AI视频通知]发送失败: user_id=' . $user_id . ' ' . $e->getMessage(), 'gen_ai');
$this->debug_log('[AI视频通知]发送失败: user_id=' . $user_id . ' ' . $e->getMessage());
return false;
}
}
......
<?php
namespace addons\shopro\notification\ai;
use addons\shopro\notification\Notification;
use addons\shopro\notification\traits\Notification as NotificationTrait;
/**
* AI视频生成成功通知
*/
class AiVideoSuccess extends Notification
{
use NotificationTrait;
// 队列延迟时间
public $delay = 0;
public $receiver_type = 'user'; // 接收人: user=用户
// 消息类型
public $notification_type = 'shop';
// 发送类型(对应后台配置的事件名)
public $event = 'ai_video_success';
// 额外数据
public $data = [];
// 模板配置
public $template = [
'MessageDefaultContent' => '您的AI试戴视频已生成完成,点击查看效果!',
'WechatOfficialAccount' => [
'temp_no' => '', // 需要在后台配置模板ID
'keywords' => ['视频状态', '生成时间'],
'fields' => [
[
"name" => "视频状态",
"field" => "status",
"template_field" => "character_string1",
],
[
"name" => "生成时间",
"field" => "create_time",
"template_field" => "time2",
]
],
],
'WechatMiniProgram' => [
'category_id' => 670,
'tid' => '', // 需要在后台配置模板ID
'kid' => [1,2],
'scene_desc' => 'AI视频生成成功通知用户',
'fields' => [
[
"name" => "视频状态",
"field" => "status",
"template_field" => "character_string1",
],
[
"name" => "生成时间",
"field" => "create_time",
"template_field" => "time2",
]
],
]
];
// 返回的字段列表
public $returnField = [
'name' => 'AI视频生成成功通知',
'channels' => ['Sms', 'Email', 'WechatOfficialAccount', 'WechatMiniProgram'],
'fields' => [
['name' => '消息名称', 'field' => 'template'],
['name' => '视频状态', 'field' => 'status'],
['name' => '生成时间', 'field' => 'create_time'],
['name' => '视频链接', 'field' => 'video_url'],
['name' => '用户昵称', 'field' => 'nickname'],
['name' => '用户手机', 'field' => 'mobile'],
]
];
/**
* 组合数据参数
*
* @param \think\Model $notifiable
* @return array
*/
protected function getData($notifiable)
{
$aiTryon = $this->data['ai_tryon'];
$user = $this->data['user'];
$data['template'] = $this->returnField['name'];
$data['status'] = '生成成功';
$data['create_time'] = date('Y-m-d H:i:s');
$data['video_url'] = $aiTryon['ai_url'] ?? '';
$data['nickname'] = $user['nickname'] ?? '';
$data['mobile'] = $user['mobile'] ?? '';
// 小程序跳转地址
$data['jump_url'] = "/pages/user/tryOnRecord";
return $data;
}
}
\ No newline at end of file
......@@ -49,7 +49,7 @@ class Ai
'allow_redirects' => true
];
// 记录入参日志
Log::info('用户ID:'. $userId . ' response_format'. $response_format . ' AI生成图片接口入参:'.json_encode($params));
custom_log('[AI生成图片接口]用户ID:'. $userId . ' response_format'. $response_format . ' AI生成图片接口入参:'.json_encode($params), 'gen_ai_img_api');
// 发送请求
$client = new Http();
......@@ -61,7 +61,7 @@ class Ai
$result = json_decode($content, true);
// 记录出参日志
Log::info('用户ID:'. $userId . ' response_format'. $response_format .' AI生成图片接口返回:'.json_encode($result));
custom_log('[AI生成图片接口]用户ID:'. $userId . ' response_format'. $response_format .' AI生成图片接口出参:'.json_encode($result), 'gen_ai_img_api');
if ($response_format == 'b64_json'){
if (!$result || !isset($result['data'][0]['b64_json'])) {
......@@ -151,7 +151,7 @@ class Ai
];
// 记录入参日志
Log::info('用户ID:'. $userId .' AI生成视频接口入参:'.json_encode($params));
custom_log('用户ID:'. $userId .' AI生成视频接口入参:'.json_encode($params), 'gen_ai_video_api');
// 发送请求
$client = new Http();
......@@ -163,7 +163,7 @@ class Ai
$result = json_decode($content, true);
// 记录出参日志
Log::info('用户ID:'. $userId .' AI生成视频接口返回:'.json_encode($result));
custom_log('用户ID:'. $userId .' AI生成视频接口返回:'.json_encode($result), 'gen_ai_video_api');
return [
'code' => 1 ,
......
......@@ -75,4 +75,23 @@ class User
return 1; // 可以试戴
}
}
/**
* @name 扣积分
* @param int $user_id 用户ID
* @param int $points 扣除积分数量
* @return int 0=失败(积分不足), 1=成功
*/
public function deductPoints($user_id, $points)
{
try {
// 使用 Wallet::change() 扣减积分,负数表示扣除
$result = \addons\shopro\service\Wallet::change($user_id, 'score', -$points, 'ai_tryon_video', [], 'AI试戴扣积分');
return $result ? 1 : 0;
} catch (\Exception $e) {
// 积分不足等异常情况
return 0;
}
}
}
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