feat: 添加获取系统前台各模块url接口
This commit is contained in:
@@ -7,10 +7,11 @@ if (!function_exists('array_to_tree')) {
|
|||||||
* @param array $data 数据
|
* @param array $data 数据
|
||||||
* @param int $pid 父级ID
|
* @param int $pid 父级ID
|
||||||
* @param string $with 转换依据字段
|
* @param string $with 转换依据字段
|
||||||
* @param int $level 层级
|
* @param int|bool $level 层级
|
||||||
|
* @param bool $keep_pid 是否保留pid
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function array_to_tree(array $data, int $pid, string $with = 'pid', int|false $level = 1)
|
function array_to_tree(array $data, int $pid, string $with = 'pid', int|bool $level = 1, bool $keep_pid = true)
|
||||||
{
|
{
|
||||||
$ret = [];
|
$ret = [];
|
||||||
foreach ($data as $item) {
|
foreach ($data as $item) {
|
||||||
@@ -20,6 +21,9 @@ if (!function_exists('array_to_tree')) {
|
|||||||
$item['level'] = $level;
|
$item['level'] = $level;
|
||||||
$lv = $level + 1;
|
$lv = $level + 1;
|
||||||
}
|
}
|
||||||
|
if ($keep_pid === false) {
|
||||||
|
unset($item[$with]);
|
||||||
|
}
|
||||||
$children = array_to_tree($data, $item['id'], $with, $lv);
|
$children = array_to_tree($data, $item['id'], $with, $lv);
|
||||||
if ($children) {
|
if ($children) {
|
||||||
$item['children'] = $children;
|
$item['children'] = $children;
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\controller\v1;
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\ArticleCategoryModel;
|
||||||
|
use app\admin\model\v1\ArticleModel;
|
||||||
|
use app\admin\model\v1\ProductCategoryModel;
|
||||||
|
use app\admin\model\v1\ProductModel;
|
||||||
use think\facade\Db;
|
use think\facade\Db;
|
||||||
|
|
||||||
class System
|
class System
|
||||||
@@ -80,4 +84,138 @@ class System
|
|||||||
|
|
||||||
return success('获取成功', $info);
|
return success('获取成功', $info);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// 组装系统内页面URL
|
||||||
|
public function urls()
|
||||||
|
{
|
||||||
|
$lang_id = request()->lang_id;
|
||||||
|
|
||||||
|
if (request()->has('link_to')) {
|
||||||
|
$param = request()->get([
|
||||||
|
'link_to',
|
||||||
|
'id'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$urls = [];
|
||||||
|
switch ($param['link_to']) {
|
||||||
|
case 'article':
|
||||||
|
$articles = $this->getArticleByCategory($lang_id, $param['id']);
|
||||||
|
$urls = array_map(function($item) {
|
||||||
|
$item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
|
||||||
|
return $item;
|
||||||
|
}, $articles);
|
||||||
|
break;
|
||||||
|
case 'product':
|
||||||
|
$products = $this->getProductByCategory($lang_id, $param['id']);
|
||||||
|
$urls = array_map(function($item) {
|
||||||
|
$item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
|
||||||
|
return $item;
|
||||||
|
}, $products);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return error('请确认link_to参数');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return success('获取成功', $urls);
|
||||||
|
} else {
|
||||||
|
// 获取文章分类
|
||||||
|
$article_category = $this->getArticleCategory($lang_id);
|
||||||
|
// 获取产品分类
|
||||||
|
$product_category = $this->getProductCategory($lang_id);
|
||||||
|
|
||||||
|
$urls = [
|
||||||
|
[
|
||||||
|
'name' => '自定义',
|
||||||
|
'link_to' => 'custom',
|
||||||
|
'data' => []
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => '文章分类',
|
||||||
|
'link_to' => 'article_category',
|
||||||
|
'data' => array_to_tree(array_map(function($item) {
|
||||||
|
$item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
|
||||||
|
return $item;
|
||||||
|
}, $article_category), 0, 'pid', false, false)
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => '文章管理',
|
||||||
|
'link_to' => 'article',
|
||||||
|
'data' => array_to_tree($article_category, 0, 'pid', false, false)
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => '产品分类',
|
||||||
|
'link_to' => 'product_category',
|
||||||
|
'data' => array_to_tree(array_map(function($item) {
|
||||||
|
$item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
|
||||||
|
return $item;
|
||||||
|
}, $product_category), 0, 'pid', false, false)
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => '产品',
|
||||||
|
'link_to' => 'product',
|
||||||
|
'data' => array_to_tree($product_category, 0, 'pid', false, false)
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
return success('获取成功', $urls);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 获取文章分类数据
|
||||||
|
private function getArticleCategory($lang_id)
|
||||||
|
{
|
||||||
|
$data = ArticleCategoryModel::field([
|
||||||
|
'id',
|
||||||
|
'pid',
|
||||||
|
'name'
|
||||||
|
])
|
||||||
|
->language($lang_id)
|
||||||
|
->isShow(true)
|
||||||
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||||
|
->select();
|
||||||
|
|
||||||
|
return $data->toArray();
|
||||||
|
}
|
||||||
|
// 获取产品分类数据
|
||||||
|
private function getProductCategory($lang_id)
|
||||||
|
{
|
||||||
|
$data = ProductCategoryModel::field([
|
||||||
|
'id',
|
||||||
|
'pid',
|
||||||
|
'name'
|
||||||
|
])
|
||||||
|
->language($lang_id)
|
||||||
|
->isShow(true)
|
||||||
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||||
|
->select();
|
||||||
|
|
||||||
|
return $data->toArray();
|
||||||
|
}
|
||||||
|
// 根据文章分类获取文章
|
||||||
|
private function getArticleByCategory($lang_id, $category_id)
|
||||||
|
{
|
||||||
|
$data = ArticleModel::field([
|
||||||
|
'id',
|
||||||
|
'title' => 'name'
|
||||||
|
])
|
||||||
|
->language($lang_id)
|
||||||
|
->category($category_id)
|
||||||
|
->enabled()
|
||||||
|
->select();
|
||||||
|
|
||||||
|
return $data->toArray();
|
||||||
|
}
|
||||||
|
// 根据产品分类获取产品
|
||||||
|
private function getProductByCategory($lang_id, $category_id)
|
||||||
|
{
|
||||||
|
$data = ProductModel::field([
|
||||||
|
'id',
|
||||||
|
'name'
|
||||||
|
])
|
||||||
|
->language($lang_id)
|
||||||
|
->category($category_id)
|
||||||
|
->isShow(true)
|
||||||
|
->select();
|
||||||
|
|
||||||
|
return $data->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,12 @@ class ArticleCategoryModel extends ArticleCategoryBaseModel
|
|||||||
$query->where('language_id', '=', $lang_id);
|
$query->where('language_id', '=', $lang_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据是否显示查询
|
||||||
|
public function scopeIsShow($query, bool $is_show)
|
||||||
|
{
|
||||||
|
$query->where('is_show', '=', (int)$is_show);
|
||||||
|
}
|
||||||
|
|
||||||
// 搜索分类名
|
// 搜索分类名
|
||||||
public function searchNameAttr($query, $value, $data)
|
public function searchNameAttr($query, $value, $data)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -61,4 +61,10 @@ class ArticleModel extends ArticleBaseModel
|
|||||||
}
|
}
|
||||||
$query->where('category_id', '=', $value);
|
$query->where('category_id', '=', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 已启用的查询
|
||||||
|
public function scopeEnabled($query)
|
||||||
|
{
|
||||||
|
$query->where('enabled', '=', 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ class ProductCategoryModel extends ProductCategoryBaseModel
|
|||||||
$query->where('language_id', '=', $value);
|
$query->where('language_id', '=', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据是否显示查询
|
||||||
|
public function scopeIsShow($query, bool $value)
|
||||||
|
{
|
||||||
|
$query->where('is_show', '=', (int)$value);
|
||||||
|
}
|
||||||
|
|
||||||
// 搜索分类名称
|
// 搜索分类名称
|
||||||
public function searchNameNullableAttr($query, $value, $data)
|
public function searchNameNullableAttr($query, $value, $data)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ class ProductModel extends ProductBaseModel
|
|||||||
}
|
}
|
||||||
$query->where('category_id', '=', $value);
|
$query->where('category_id', '=', $value);
|
||||||
}
|
}
|
||||||
|
public function scopeCategory($query, $value)
|
||||||
|
{
|
||||||
|
$query->where('category_id', '=', $value);
|
||||||
|
}
|
||||||
|
|
||||||
// 上架状态查询
|
// 上架状态查询
|
||||||
public function scopeIsShowNullable($query, bool|null $value)
|
public function scopeIsShowNullable($query, bool|null $value)
|
||||||
@@ -76,4 +80,8 @@ class ProductModel extends ProductBaseModel
|
|||||||
}
|
}
|
||||||
$query->where('is_show', '=', (int)$value);
|
$query->where('is_show', '=', (int)$value);
|
||||||
}
|
}
|
||||||
|
public function scopeIsShow($query, bool $value)
|
||||||
|
{
|
||||||
|
$query->where('is_show', '=', (int)$value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,13 @@ use think\facade\Route;
|
|||||||
// v1版本路由定义
|
// v1版本路由定义
|
||||||
Route::group('v1', function () {
|
Route::group('v1', function () {
|
||||||
// 获取系统信息
|
// 获取系统信息
|
||||||
Route::get('system/info', 'System/info');
|
Route::group('system', function() {
|
||||||
|
// 获取系统信息
|
||||||
|
Route::get('info', 'System/info');
|
||||||
|
|
||||||
|
// 组装系统内页面URL
|
||||||
|
Route::get('urls', 'System/urls');
|
||||||
|
});
|
||||||
|
|
||||||
// 用户模块
|
// 用户模块
|
||||||
Route::group('user', function () {
|
Route::group('user', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user