Files
duolamaojiazhen/application/api/logic/GoodsCategoryLogic.php
2026-01-07 10:31:16 +08:00

145 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | likeshop100%开源免费商用商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | 商业版本务必购买商业授权,以免引起法律纠纷
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshopTeam
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\server\UrlServer;
use think\Db;
use think\facade\Cache;
class GoodsCategoryLogic{
/**
* Notes:获取商品分类接口
* @param $client int 终端1-移动端2-pc端
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author: 2021/3/6 18:49
*/
public static function categoryThirdTree($client,$type){
$cache_key = 'goods_category_'.$client.'_'.$type;
$cache = Cache::get($cache_key);
if ($cache) {
return $cache;
}
// 构建查询条件
$where1 = ['is_show'=>1,'del'=>0,'level'=>1];
$where2 = ['is_show'=>1,'del'=>0,'level'=>2];
$where3 = ['is_show'=>1,'del'=>0,'level'=>3];
// 如果type参数存在尝试添加type条件如果表中存在该字段
// 注意如果数据库表中没有type字段需要移除这些条件或添加字段
// 暂时注释掉type条件避免报错
if ($type) {
$where1['type'] = $type;
$where2['type'] = $type;
$where3['type'] = $type;
}
$lists = Db::name('goods_category')
->where($where1)
->order('sort desc')
->column('id,name,pid,image,level','id');
$level2 = Db::name('goods_category')
->where($where2)
->column('id,name,pid,image,level','id');
$level3 = Db::name('goods_category')
->where($where3)
->field('id,name,pid,image,level')->select();
//挂载第二级
foreach ($level3 as $list3){
if(isset($level2[$list3['pid']])){
$list3['image'] = UrlServer::getFileUrl($list3['image']);
$list3['type'] = 1;
$level2[$list3['pid']]['sons'][] = $list3;
}
}
//挂载第一级、并移除没有下级的二级分类
foreach ($level2 as $key2 => $list2){
// if(!isset($list2['sons'])){
// unset($level2[$key2]);
// continue;
// }
if(isset($lists[$list2['pid']])){
$list2['type'] = 1;
$list2['image'] = UrlServer::getFileUrl($list2['image']);
$lists[$list2['pid']]['sons'][] = $list2;
}
}
//移除没有完整的三级分类
foreach ($lists as $key1 => $list1){
// if(!isset($list1['sons'])){
// unset($lists[$key1]);
// continue;
//
// }
$lists[$key1]['image'] = UrlServer::getFileUrl($list1['image']);
$lists[$key1]['type'] = 1;
}
//pc端不显示品牌
if(1 == $client){
$brandWhere = ['del'=>0,'is_show'=>1];
// 如果type参数存在尝试添加type条件如果表中存在该字段
// 暂时注释掉type条件避免报错
// if ($type) {
// $brandWhere['type'] = $type;
// }
$goods_brand = Db::name('goods_brand')
->where($brandWhere)
->field('id,name,image')
->order('sort desc,id desc')
->select();
if($goods_brand){
foreach ($goods_brand as &$brand_item){
$brand_item['type'] = 0;
$brand_item['image'] = UrlServer::getFileUrl($brand_item['image']);
}
$brand = [
'id' => 0,
'name' => '品牌推荐',
'type' => 0,
'sons' =>[
[
'id' => 0,
'name' => '热门品牌',
'type' => 0,
'sons' => $goods_brand,
]
],
];
array_unshift($lists,$brand);
}
}
Cache::set($cache_key, array_values($lists));
return array_values($lists);
}
}