master
wangxinglong 2022-05-27 18:36:21 +08:00
parent 982c375ea8
commit aae2aa342d
3 changed files with 49 additions and 2 deletions

View File

@ -677,5 +677,32 @@ if (!function_exists('replaceStoragePath'))
return preg_replace($pregRule, '<img src="' . request()->domain() . '/${1}" style="max-width:100%">', (string)$content );
}
}
if (!function_exists('list_to_tree')){
function list_to_tree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
}

View File

@ -27,8 +27,11 @@ class Spu extends Base
*/
public function category(): Json
{
$pid = input("pid/d",0);
$list = SpuRepository::getInstance()->category($pid)->toArray();
if(input("hasChildren/d",0)){
$list = SpuRepository::getInstance()->categoryAll(["id,pid,title,cover"]);
}else{
$list = SpuRepository::getInstance()->category(0,["id,pid,title,cover"])->toArray();
}
return $this->json(0, 'success', $list);
}

View File

@ -789,8 +789,25 @@ class SpuRepository extends Repository
->field($fields)
->order('sort', 'desc')
->order('id', 'asc')
->withAttr("cover",function ($value){
return resourceJoin($value,request()->domain());
})
->select();
}
public function categoryAll(array $fields = ['id', 'title'])
{
$list = Category::field($fields)
->order('sort', 'desc')
->order('id', 'asc')
->select()
->withAttr("cover",function ($value){
return resourceJoin($value,request()->domain());
})
->toArray();
return list_to_tree($list);
}
/**
* 设置商品分类