更新模块市场功能

This commit is contained in:
李志强 2026-01-30 16:19:53 +08:00
parent 5e997747c5
commit 581738da6e
7 changed files with 280 additions and 0 deletions

View File

@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\response\Json;
use app\model\SystemModuleCenter;
use app\model\SystemModuleCategory;
use app\model\AdminUser;
class ModuleCenterController extends BaseController
{
/**
* 获取模块中心分类
* @return Json
*/
public function getModuleCategory()
{
$cates = SystemModuleCategory::where('delete_time', null)->field('id, title, status')->select()->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $cates,
'total' => count($cates)
]
]);
}
/**
* 获取模块中心列表
* @param int $cid 分类ID0表示全部
* @return Json
*/
public function getModuleList(int $cid = 0)
{
$query = SystemModuleCenter::where('delete_time', null)
->field('id, title, desc, thumb, cid, downloads, author, status, version, create_time');
// 如果cid不为0则按分类筛选
if ($cid > 0) {
$query->where('cid', $cid);
}
$list = $query->select()->toArray();
// 遍历列表将cid替换为分类标题
foreach ($list as &$item) {
if ($item['cid']) {
$category = SystemModuleCategory::where('id', $item['cid'])->field('title')->find();
$item['cid'] = $category ? $category['title'] : '';
}
if ($item['author']) {
$author = AdminUser::where('id', $item['author'])->field('name')->find();
$item['author'] = $author ? $author['name'] : '';
}
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => count($list)
]
]);
}
/**
* 编辑分类(添加/编辑)
* @param int $id 分类ID为空时新增有值时编辑
*/
public function editCategory(int $id = 0)
{
$data = request()->param();
// 过滤只保留数据库表需要的字段
$allowedFields = ['id', 'title', 'status'];
$saveData = [];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$saveData[$field] = $data[$field];
}
}
if ($id > 0) {
// 编辑模式
$saveData['update_time'] = date('Y-m-d H:i:s');
SystemModuleCategory::where('id', $id)->update($saveData);
$this->logSuccess('模块市场', '编辑模块分类', ['id' => $id, 'data' => $saveData]);
return json([
'code' => 200,
'msg' => '编辑成功'
]);
} else {
// 新增模式
$saveData['create_time'] = date('Y-m-d H:i:s');
$cateId = SystemModuleCategory::insertGetId($saveData);
$this->logSuccess('模块市场', '添加模块分类', ['data' => $saveData]);
return json([
'code' => 200,
'msg' => '添加成功'
]);
}
}
/**
* 编辑模块(添加/编辑)
* @param int $id 为空时新增,有值时编辑
*/
public function editModules(int $id = 0)
{
$data = request()->param();
// 过滤只保留数据库表需要的字段
$allowedFields = ['id', 'title', 'desc', 'thumb', 'cid', 'version', 'tatus'];
$saveData = [];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$saveData[$field] = $data[$field];
}
}
if ($id > 0) {
// 编辑模式
$saveData['update_time'] = date('Y-m-d H:i:s');
SystemModuleCenter::where('id', $id)->update($saveData);
$this->logSuccess('模块市场', '编辑模块', ['id' => $id, 'data' => $saveData]);
return json([
'code' => 200,
'msg' => '编辑成功'
]);
} else {
// 新增模式
$saveData['create_time'] = date('Y-m-d H:i:s');
$moduleId = SystemModuleCenter::insertGetId($saveData);
$this->logSuccess('模块市场', '添加模块', ['data' => $saveData]);
return json([
'code' => 200,
'msg' => '添加成功'
]);
}
}
}

View File

@ -112,3 +112,9 @@ Route::delete('modules/:id', 'app\\admin\\controller\\ModulesController@delete')
Route::post('modules/batchDelete', 'app\\admin\\controller\\ModulesController@batchDelete'); Route::post('modules/batchDelete', 'app\\admin\\controller\\ModulesController@batchDelete');
Route::post('modules/status', 'app\\admin\\controller\\ModulesController@changeStatus'); Route::post('modules/status', 'app\\admin\\controller\\ModulesController@changeStatus');
Route::get('modules/select/list', 'app\\admin\\controller\\ModulesController@getSelectList'); Route::get('modules/select/list', 'app\\admin\\controller\\ModulesController@getSelectList');
// 模块中心路由
Route::get('moduleCategory', 'app\\admin\\controller\\ModuleCenterController@getModuleCategory');
Route::get('moduleCenter/modules', 'app\\admin\\controller\\ModuleCenterController@getModuleList');
Route::post('moduleCenter/editCategory', 'app\\admin\\controller\\ModuleCenterController@editCategory');
Route::post('moduleCenter/editModules', 'app\\admin\\controller\\ModuleCenterController@editModules');

View File

@ -20,6 +20,7 @@ class AdminModules extends Model
'path' => 'string', 'path' => 'string',
'icon' => 'string', 'icon' => 'string',
'description' => 'string', 'description' => 'string',
'type' => 'string',
'sort' => 'integer', 'sort' => 'integer',
'status' => 'integer', 'status' => 'integer',
'is_show' => 'integer', 'is_show' => 'integer',

View File

@ -0,0 +1,41 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 单页模型
*/
class SystemModuleCategory extends Model
{
// 启用软删除
use SoftDelete;
// 数据库表名
protected $name = 'mete_system_module_category';
// 字段类型转换
protected $type = [
'id' => 'integer',
'title' => 'string',
'desc' => 'string',
'thumb' => 'string',
'status' => 'integer',
'sort' => 'integer',
'create_time' => 'datetime',
'update_time' => 'datetime',
'delete_time' => 'datetime',
];
}

View File

@ -0,0 +1,45 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 单页模型
*/
class SystemModuleCenter extends Model
{
// 启用软删除
use SoftDelete;
// 数据库表名
protected $name = 'mete_system_module_center';
// 字段类型转换
protected $type = [
'id' => 'integer',
'title' => 'string',
'desc' => 'string',
'thumb' => 'string',
'author' => 'integer',
'cid' => 'integer',
'downloads' => 'integer',
'status' => 'integer',
'sort' => 'integer',
'version' => 'string',
'create_time' => 'datetime',
'update_time' => 'datetime',
'delete_time' => 'datetime',
];
}

View File

@ -0,0 +1,37 @@
/**
* 编辑分类(添加/编辑)
* @param int $id 分类ID为空时新增有值时编辑
*/
public function editCategory(int $id = 0)
{
$data = request()->param();
// 过滤只保留数据库表需要的字段
$allowedFields = ['id', 'title', 'status'];
$saveData = [];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$saveData[$field] = $data[$field];
}
}
if ($id > 0) {
// 编辑模式
$saveData['update_time'] = date('Y-m-d H:i:s');
SystemModuleCategory::where('id', $id)->update($saveData);
$this->logSuccess('模块市场', '编辑模块分类', ['id' => $id, 'data' => $saveData]);
return json([
'code' => 200,
'msg' => '编辑成功'
]);
} else {
// 新增模式
$saveData['create_time'] = date('Y-m-d H:i:s');
$cateId = SystemModuleCategory::insertGetId($saveData);
$this->logSuccess('模块市场', '添加模块分类', ['data' => $saveData]);
return json([
'code' => 200,
'msg' => '添加成功'
]);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB