151 lines
4.5 KiB
PHP
151 lines
4.5 KiB
PHP
<?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 分类ID,0表示全部
|
||
* @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' => '添加成功'
|
||
]);
|
||
}
|
||
}
|
||
}
|