Commit 406c2180 authored by 刘小敏's avatar 刘小敏

AI试戴接口优化 增加试戴商品分类限制逻辑

parent a5926ebd
...@@ -125,6 +125,11 @@ class Goods extends Common ...@@ -125,6 +125,11 @@ class Goods extends Common
$goods['content'] = $content; $goods['content'] = $content;
unset($goods['new_sku_prices']); unset($goods['new_sku_prices']);
// 商品是否可试戴
$goodsService = new GoodsService();
$isAllowed = $goodsService->isCategoryAllowed($goods['category_ids']);
$goods['is_allowed_tryon'] = $isAllowed ? 1 : 0;
$this->success('获取成功', $goods); $this->success('获取成功', $goods);
} }
......
...@@ -93,8 +93,8 @@ class AiTryon extends Common ...@@ -93,8 +93,8 @@ class AiTryon extends Common
// 查询AI试戴饰品列表 // 查询AI试戴饰品列表
$service = new GoodsService(); $service = new GoodsService();
$service->category(4, true); $allowed_cate_ids = config('shopro.allow_tryon_cate');
$goods = $service->jewelryPaginate(); $goods = $service->jewelryPaginate($allowed_cate_ids);
$this->success('饰品列表', $goods); $this->success('饰品列表', $goods);
} }
...@@ -112,6 +112,13 @@ class AiTryon extends Common ...@@ -112,6 +112,13 @@ class AiTryon extends Common
]); ]);
$this->svalidate($params, ".aiadd"); $this->svalidate($params, ".aiadd");
// 校验商品分类是否在允许试戴分类中
$goodsService = new GoodsService();
$is_allowed = $goodsService->isCategoryAllowed('', $params['goods_id']);
if ($is_allowed === 0) {
$this->error('商品所属分类不允许试戴');
}
$user = auth_user(); $user = auth_user();
$userService = new User(); $userService = new User();
$res = $userService->isTry($user->id); $res = $userService->isTry($user->id);
...@@ -201,8 +208,6 @@ class AiTryon extends Common ...@@ -201,8 +208,6 @@ class AiTryon extends Common
*/ */
public function genVideo() public function genVideo()
{ {
if (!$this->request->isPost()) { if (!$this->request->isPost()) {
$this->error('请用post方式请求'); $this->error('请用post方式请求');
} }
...@@ -212,6 +217,13 @@ class AiTryon extends Common ...@@ -212,6 +217,13 @@ class AiTryon extends Common
]); ]);
$this->svalidate($params, ".aiadd"); $this->svalidate($params, ".aiadd");
// 校验商品分类是否在允许试戴分类中
$goodsService = new GoodsService();
$is_allowed = $goodsService->isCategoryAllowed('', $params['goods_id']);
if ($is_allowed === 0) {
$this->error('商品所属分类不允许试戴');
}
$user = auth_user(); $user = auth_user();
// \think\Queue::push('\addons\shopro\job\AiVideoGen@save', ['taskid' => 'cgt-20260506113558-x6wtq', 'user' => $user], 'shopro-high'); // \think\Queue::push('\addons\shopro\job\AiVideoGen@save', ['taskid' => 'cgt-20260506113558-x6wtq', 'user' => $user], 'shopro-high');
......
...@@ -6,6 +6,7 @@ use app\admin\model\shopro\goods\Goods; ...@@ -6,6 +6,7 @@ use app\admin\model\shopro\goods\Goods;
use app\admin\model\shopro\goods\SkuPrice; use app\admin\model\shopro\goods\SkuPrice;
use app\admin\model\shopro\app\ScoreSkuPrice; use app\admin\model\shopro\app\ScoreSkuPrice;
use app\admin\model\shopro\Category; use app\admin\model\shopro\Category;
use think\Cache;
use think\Db; use think\Db;
use addons\shopro\library\Tree; use addons\shopro\library\Tree;
use addons\shopro\service\SearchHistory; use addons\shopro\service\SearchHistory;
...@@ -188,6 +189,77 @@ class GoodsService ...@@ -188,6 +189,77 @@ class GoodsService
return $this; return $this;
} }
/**
* 根据多个分类ID查询商品
*
* @param array|string $categoryIds 分类ID数组或逗号分隔的字符串
* @param bool $is_category_deep 是否查询子分类
* @return self
*/
public function categories($categoryIds, $is_category_deep = true)
{
// 快速路径:空参数直接返回
if (empty($categoryIds)) {
return $this;
}
// 统一处理输入:字符串转数组,去除空格,转换为整数
if (is_string($categoryIds)) {
$categoryIds = array_filter(array_map('intval', array_map('trim', explode(',', $categoryIds))));
} else {
$categoryIds = array_filter(array_map('intval', $categoryIds));
}
// 去重
$categoryIds = array_unique($categoryIds);
if (empty($categoryIds)) {
return $this;
}
$this->md5s[] = 'category_ids:' . implode(',', $categoryIds);
// 如果需要查询子分类(使用缓存)
if ($is_category_deep) {
$allCategoryIds = [];
$tree = null;
foreach ($categoryIds as $categoryId) {
// 构建缓存key
$cacheKey = "category_child_ids_{$categoryId}";
// 尝试从缓存获取
$childIds = Cache::get($cacheKey);
if ($childIds === false) {
// 缓存不存在,查询并缓存
if (!$tree) {
$tree = new Tree(function () {
return (new Category)->normal();
});
}
$childIds = $tree->getChildIds($categoryId);
// 缓存24小时
Cache::set($cacheKey, $childIds, 86400);
}
$allCategoryIds = array_merge($allCategoryIds, $childIds);
}
$categoryIds = array_unique($allCategoryIds);
}
// 优化:构建单个 WHERE 条件
$conditions = [];
foreach ($categoryIds as $categoryId) {
$conditions[] = "FIND_IN_SET($categoryId, category_ids)";
}
$this->query->whereRaw(implode(' OR ', $conditions));
return $this;
}
/** /**
* 排序,方法参数同 thinkphp order 方法 * 排序,方法参数同 thinkphp order 方法
...@@ -270,15 +342,20 @@ class GoodsService ...@@ -270,15 +342,20 @@ class GoodsService
* @param bool $is_cache 是否缓存 * @param bool $is_cache 是否缓存
* @return \think\Paginator * @return \think\Paginator
*/ */
public function jewelryPaginate($is_cache = false) public function jewelryPaginate($allowed_cate_ids, $is_cache = false)
{ {
$this->md5s[] = 'paginate'; $this->md5s[] = 'paginate';
// 确保分类ID不为空
if (empty($allowed_cate_ids)) {
// 返回空的分页结果
$this->query->where('id', 0);
}
// 默认排序 // 默认排序
$goods = $this->query->where('status', 'up')->field('id,title,subtitle,image') $goods = $this->up()
->cache($this->getCacheKey($is_cache), (200 + mt_rand(0, 100))) ->categories($allowed_cate_ids)
->paginate(request()->param('list_rows', 10)) ->paginate($is_cache);
->toArray();
return $goods; return $goods;
} }
...@@ -461,4 +538,47 @@ class GoodsService ...@@ -461,4 +538,47 @@ class GoodsService
return $this; return $this;
} }
/**
* 判断商品分类是否在允许的分类ID数组中
*
* @param int $goodId 商品ID
* @param int $categoryIds 商品分类ID
* return int
*/
public function isCategoryAllowed($categoryIds, $goodId=0)
{
if (empty($categoryIds) && empty($goodId)) {
return 0;
}
if (empty($categoryId) && !empty($goodId)) {
$goods = $this->whereIds($goodId)->find();
$categoryIds = $goods['category_ids'] ?? 0;
}
$count = 0;
if (!empty($categoryIds)){
// 获取配置的允许分类ID数组
$allowedCates = config('app.allow_tryon_cate');
if (empty($allowedCates)) {
return 0;
}
// 将配置字符串转换为数组
$allowedArray = explode(',', $allowedCates);
$categoryIdsArray = explode(',', $categoryIds);
// 去除空格并转换为整数
$categoryIdsArray = array_map(function ($item) {
return (int)trim($item);
}, $categoryIdsArray);
// 判断当前分类ID是否在允许列表中
$count = count(array_intersect($allowedArray, $categoryIdsArray));
}
return $count > 0 ? 1 : 0;
}
} }
...@@ -185,9 +185,12 @@ return [ ...@@ -185,9 +185,12 @@ return [
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
'cache' => [ 'cache' => [
// 驱动方式 // 驱动方式
'type' => 'File', 'type' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
// 缓存保存目录 // 缓存保存目录
'path' => CACHE_PATH, // 'path' => CACHE_PATH,
// 缓存前缀 // 缓存前缀
'prefix' => '', 'prefix' => '',
// 缓存有效期 0表示永久缓存 // 缓存有效期 0表示永久缓存
...@@ -316,6 +319,8 @@ return [ ...@@ -316,6 +319,8 @@ return [
'ai_api_key' => Env::get('ai.api_key', ''), 'ai_api_key' => Env::get('ai.api_key', ''),
// 接口域名 // 接口域名
'api_host' => Env::get('app.api_host', ''), 'api_host' => Env::get('app.api_host', ''),
// 允许试戴的商品分类IDS
'allow_tryon_cate' => Env::get('app.allow_tryon_cate', ''),
], ],
]; ];
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