Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
loveisland
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘小敏
loveisland
Commits
406c2180
Commit
406c2180
authored
May 13, 2026
by
刘小敏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AI试戴接口优化 增加试戴商品分类限制逻辑
parent
a5926ebd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
153 additions
and
11 deletions
+153
-11
Goods.php
addons/shopro/controller/goods/Goods.php
+5
-0
AiTryon.php
addons/shopro/controller/user/AiTryon.php
+16
-4
GoodsService.php
addons/shopro/service/goods/GoodsService.php
+125
-5
config.php
application/config.php
+7
-2
No files found.
addons/shopro/controller/goods/Goods.php
View file @
406c2180
...
...
@@ -125,6 +125,11 @@ class Goods extends Common
$goods
[
'content'
]
=
$content
;
unset
(
$goods
[
'new_sku_prices'
]);
// 商品是否可试戴
$goodsService
=
new
GoodsService
();
$isAllowed
=
$goodsService
->
isCategoryAllowed
(
$goods
[
'category_ids'
]);
$goods
[
'is_allowed_tryon'
]
=
$isAllowed
?
1
:
0
;
$this
->
success
(
'获取成功'
,
$goods
);
}
...
...
addons/shopro/controller/user/AiTryon.php
View file @
406c2180
...
...
@@ -93,8 +93,8 @@ class AiTryon extends Common
// 查询AI试戴饰品列表
$service
=
new
GoodsService
();
$
service
->
category
(
4
,
true
);
$goods
=
$service
->
jewelryPaginate
();
$
allowed_cate_ids
=
config
(
'shopro.allow_tryon_cate'
);
$goods
=
$service
->
jewelryPaginate
(
$allowed_cate_ids
);
$this
->
success
(
'饰品列表'
,
$goods
);
}
...
...
@@ -112,6 +112,13 @@ class AiTryon extends Common
]);
$this
->
svalidate
(
$params
,
".aiadd"
);
// 校验商品分类是否在允许试戴分类中
$goodsService
=
new
GoodsService
();
$is_allowed
=
$goodsService
->
isCategoryAllowed
(
''
,
$params
[
'goods_id'
]);
if
(
$is_allowed
===
0
)
{
$this
->
error
(
'商品所属分类不允许试戴'
);
}
$user
=
auth_user
();
$userService
=
new
User
();
$res
=
$userService
->
isTry
(
$user
->
id
);
...
...
@@ -201,8 +208,6 @@ class AiTryon extends Common
*/
public
function
genVideo
()
{
if
(
!
$this
->
request
->
isPost
())
{
$this
->
error
(
'请用post方式请求'
);
}
...
...
@@ -212,6 +217,13 @@ class AiTryon extends Common
]);
$this
->
svalidate
(
$params
,
".aiadd"
);
// 校验商品分类是否在允许试戴分类中
$goodsService
=
new
GoodsService
();
$is_allowed
=
$goodsService
->
isCategoryAllowed
(
''
,
$params
[
'goods_id'
]);
if
(
$is_allowed
===
0
)
{
$this
->
error
(
'商品所属分类不允许试戴'
);
}
$user
=
auth_user
();
// \think\Queue::push('\addons\shopro\job\AiVideoGen@save', ['taskid' => 'cgt-20260506113558-x6wtq', 'user' => $user], 'shopro-high');
...
...
addons/shopro/service/goods/GoodsService.php
View file @
406c2180
...
...
@@ -6,6 +6,7 @@ use app\admin\model\shopro\goods\Goods;
use
app\admin\model\shopro\goods\SkuPrice
;
use
app\admin\model\shopro\app\ScoreSkuPrice
;
use
app\admin\model\shopro\Category
;
use
think\Cache
;
use
think\Db
;
use
addons\shopro\library\Tree
;
use
addons\shopro\service\SearchHistory
;
...
...
@@ -188,6 +189,77 @@ class GoodsService
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 方法
...
...
@@ -270,15 +342,20 @@ class GoodsService
* @param bool $is_cache 是否缓存
* @return \think\Paginator
*/
public
function
jewelryPaginate
(
$is_cache
=
false
)
public
function
jewelryPaginate
(
$
allowed_cate_ids
,
$
is_cache
=
false
)
{
$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'
)
->
cache
(
$this
->
getCacheKey
(
$is_cache
),
(
200
+
mt_rand
(
0
,
100
)))
->
paginate
(
request
()
->
param
(
'list_rows'
,
10
))
->
toArray
();
$goods
=
$this
->
up
()
->
categories
(
$allowed_cate_ids
)
->
paginate
(
$is_cache
);
return
$goods
;
}
...
...
@@ -461,4 +538,47 @@ class GoodsService
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
;
}
}
application/config.php
View file @
406c2180
...
...
@@ -185,9 +185,12 @@ return [
// +----------------------------------------------------------------------
'cache'
=>
[
// 驱动方式
'type'
=>
'File'
,
'type'
=>
'redis'
,
'host'
=>
'127.0.0.1'
,
'port'
=>
6379
,
'password'
=>
''
,
// 缓存保存目录
'path'
=>
CACHE_PATH
,
//
'path' => CACHE_PATH,
// 缓存前缀
'prefix'
=>
''
,
// 缓存有效期 0表示永久缓存
...
...
@@ -316,6 +319,8 @@ return [
'ai_api_key'
=>
Env
::
get
(
'ai.api_key'
,
''
),
// 接口域名
'api_host'
=>
Env
::
get
(
'app.api_host'
,
''
),
// 允许试戴的商品分类IDS
'allow_tryon_cate'
=>
Env
::
get
(
'app.allow_tryon_cate'
,
''
),
],
];
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment