zhaojs
2023-07-06 1a8d87ee2e142203e16c90f050c7b05e1315a8cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
 
namespace app\api\logic;
 
use app\common\logic\Basic as BasicLogic;
use app\api\model\SuperClass as ModelSuperClass;
use fast\Tree;
use think\Cache;
 
/**
 * 超级分类管理(好单库)
 */
class SuperClass extends BasicLogic
{
 
    protected $model = null;
 
    public function __construct()
    {
        parent::__construct();
 
        $this->model = new ModelSuperClass();
    }
 
    /**
     * 获取分类列表
     *
     * @return void
     */
    public function getList()
    {
        $list = $this->model->field('id,cid,main_name,son_name,imgurl,parent_id,next_name')->where(['status' => 1])->select();
        $res = $this->getTree($list);
        return $res;
    }
 
    private function getTree($array)
    {
        //遍历数组,按照id作为键名重新组建新的数组
        $new_array = [];
        foreach ($array as $v) {
            $v = $v->toArray();
            $new_array[$v['id']] = $v;
        }
        $return_tree = [];
        foreach ($new_array as $kk => $vv) {
            if (isset($new_array[$vv['parent_id']])) {
                if(!isset($new_array[$vv['parent_id']]['sons'])) $new_array[$vv['parent_id']]['sons'] = [];
                $new_array[$vv['parent_id']]['sons'][] = &$new_array[$kk];
            } else {
                $return_tree[] = &$new_array[$kk];
            }
        }
        return $return_tree;
    }
}