调整商品分类管理

This commit is contained in:
lewis 2025-07-14 12:16:49 +08:00
parent 01d63e6114
commit ea0b6f1f65
4 changed files with 88 additions and 70 deletions

View File

@ -7,6 +7,7 @@ use app\admin\controller\BaseAdminController;
use app\admin\lists\ProductCategoryLists;
use app\admin\logic\ProductCategoryLogic;
use app\admin\validate\ProductCategoryValidate;
use support\Request;
/**
@ -91,5 +92,11 @@ class ProductCategoryController extends BaseAdminController
return $this->data($result);
}
public function all(Request $request)
{
$params = $request->get();
$result = ProductCategoryLogic::all($params);
return $this->data($result);
}
}
}

View File

@ -1,65 +1,67 @@
<?php
namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\ProductCategory;
use app\common\lists\ListsSearchInterface;
/**
* ProductCategory列表
* Class ProductCategoryLists
* @package app\admin\lists
*/
class ProductCategoryLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function setSearch(): array
{
return [
'=' => ['pid', 'name'],
];
}
/**
* @notes 获取列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function lists(): array
{
return ProductCategory::where($this->searchWhere)
->field(['id', 'pid', 'name', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取数量
* @return int
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function count(): int
{
return ProductCategory::where($this->searchWhere)->count();
}
}
<?php
namespace app\admin\lists;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\ProductCategory;
use app\common\lists\ListsSearchInterface;
/**
* ProductCategory列表
* Class ProductCategoryLists
* @package app\admin\lists
*/
class ProductCategoryLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function setSearch(): array
{
return [
'=' => ['pid', 'name'],
];
}
/**
* @notes 获取列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function lists(): array
{
$lists = ProductCategory::where($this->searchWhere)
->field(['id', 'pid', 'name', 'create_time'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
return linear_to_tree($lists, 'children');
}
/**
* @notes 获取数量
* @return int
* @author likeadmin
* @date 2025/07/11 11:41
*/
public function count(): int
{
return ProductCategory::where($this->searchWhere)->count();
}
}

View File

@ -93,4 +93,12 @@ class ProductCategoryLogic extends BaseLogic
{
return ProductCategory::findOrEmpty($params['id'])->toArray();
}
}
public static function all($params)
{
$query = ProductCategory::field(['id', 'pid', 'name', 'create_time']);
$list = $query->order(['id' => 'desc'])->select()->toArray();
return linear_to_tree($list, 'children');
}
}

View File

@ -94,13 +94,14 @@ function create_password(string $plaintext, string $salt) : string
* @param int $parent_id 此值请勿给参数
* @return array
*/
function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0, $level = 0)
{
$tree = [];
foreach ($data as $row) {
$row['level'] = $level;
if ($row[$parent_id_name] == $parent_id) {
$temp = $row;
$child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
$child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name], $level + 1);
if ($child) {
$temp[$sub_key_name] = $child;
}
@ -206,4 +207,4 @@ function get_file_domain($content)
$preg = '/(<img .*?src=")[^https|^http](.*?)(".*?>)/is';
$fileUrl = FileService::getFileUrl();
return preg_replace($preg, "\${1}$fileUrl\${2}\${3}", $content);
}
}