123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace app\common\service;
- use app\common\model\Category;
- use app\common\traits\ServiceTrait;
- use Exception;
- use fast\Tree;
- class CategoryService
- {
- use ServiceTrait;
- /** @var Category */
- public static $Model = Category::class;
- public static function getTreeArray($type = 'goods', $pid = 0)
- {
- $tree = Tree::instance();
- $list = self::$Model::where('type', $type)->order('weigh asc,id asc')->select();
- $tree->init(collection($list)->toArray(), 'pid');
- return self::transformTree($tree->getTreeArray(0));
- }
- public static function getOptions($type = 'goods', $pid = 0)
- {
- $list = self::$Model::where('type', $type)->order('weigh asc,id asc')->column('name', 'id');
- return $list;
- }
- public static function transformTree($data)
- {
- return array_map(function ($item) {
- $transformedItem = [
- 'groupId' => (string)$item['id'],
- 'thumbnail' => $item['image'] ?: 'https://cdn-we-retail.ym.tencent.com/miniapp/category/category-default.png',
- 'name' => $item['name']
- ];
- if (isset($item['childlist']) && is_array($item['childlist']) && count($item['childlist']) > 0) {
- $transformedItem['children'] = self::transformTree($item['childlist']);
- }
- return $transformedItem;
- }, $data);
- }
- }
|