Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop

This commit is contained in:
Ghost
2025-04-10 16:40:48 +08:00
30 changed files with 2506 additions and 347 deletions

View File

@@ -1,5 +1,35 @@
<?php
use think\facade\Route;
// 超级管理员认证相关路由
Route::post('auth/login', 'app\\superadmin\\controller\\Auth@login');
// 超级管理员认证相关路由(不需要鉴权)
Route::post('auth/login', 'app\\superadmin\\controller\\Auth@login');
// 需要登录认证的路由组
Route::group('', function () {
// 菜单管理相关路由
Route::group('menu', function () {
Route::get('tree', 'app\\superadmin\\controller\\Menu@getMenuTree');
Route::get('list', 'app\\superadmin\\controller\\Menu@getMenuList');
Route::post('save', 'app\\superadmin\\controller\\Menu@saveMenu');
Route::delete('delete/:id', 'app\\superadmin\\controller\\Menu@deleteMenu');
Route::post('status', 'app\\superadmin\\controller\\Menu@updateStatus');
Route::get('toplevel', 'app\\superadmin\\controller\\Menu@getTopLevelMenus');
});
// 管理员相关路由
Route::group('administrator', function () {
// 获取管理员列表
Route::get('list', 'app\\superadmin\\controller\\Administrator@getList');
// 获取管理员详情
Route::get('detail/:id', 'app\\superadmin\\controller\\Administrator@getDetail');
// 更新管理员信息
Route::post('update', 'app\\superadmin\\controller\\Administrator@updateAdmin');
// 添加管理员
Route::post('add', 'app\\superadmin\\controller\\Administrator@addAdmin');
// 删除管理员
Route::post('delete', 'app\\superadmin\\controller\\Administrator@deleteAdmin');
});
// 系统信息相关路由
Route::get('system/info', 'app\\superadmin\\controller\\System@getInfo');
})->middleware(['app\\superadmin\\middleware\\AdminAuth']);

View File

@@ -0,0 +1,328 @@
<?php
namespace app\superadmin\controller;
use app\superadmin\model\AdministratorPermissions;
use think\Controller;
use app\superadmin\model\Administrator as AdminModel;
/**
* 管理员控制器
*/
class Administrator extends Controller
{
/**
* 获取管理员列表
* @return \think\response\Json
*/
public function getList()
{
// 获取分页参数
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 10);
$keyword = $this->request->param('keyword/s', '');
// 构建查询条件
$where = [
['deleteTime', '=', 0]
];
// 如果有搜索关键词
if (!empty($keyword)) {
$where[] = ['account|name', 'like', "%{$keyword}%"];
}
// 查询管理员数据
$total = AdminModel::where($where)->count();
$list = AdminModel::where($where)
->field('id, account, name, status, authId, createTime, lastLoginTime, lastLoginIp')
->order('id', 'desc')
->page($page, $limit)
->select();
// 格式化数据
$data = [];
foreach ($list as $item) {
$data[] = [
'id' => $item->id,
'username' => $item->account,
'name' => $item->name,
'role' => $this->getRoleName($item->authId),
'status' => $item->status,
'createdAt' => date('Y-m-d H:i:s', $item->createTime),
'lastLogin' => !empty($item->lastLoginTime) ? date('Y-m-d H:i:s', $item->lastLoginTime) : '从未登录',
'permissions' => $this->getPermissions($item->id)
];
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $data,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
}
/**
* 获取详细信息
* @param int $id 管理员ID
* @return \think\response\Json
*/
public function getDetail($id)
{
// 查询管理员信息
$admin = AdminModel::where('id', $id)
->where('deleteTime', 0)
->field('id, account, name, status, authId, createTime, lastLoginTime')
->find();
// 如果查不到记录
if (!$admin) {
return json([
'code' => 404,
'msg' => '管理员不存在',
'data' => null
]);
}
// 格式化数据
$data = [
'id' => $admin->id,
'username' => $admin->account,
'name' => $admin->name,
'status' => $admin->status,
'authId' => $admin->authId,
'roleName' => $this->getRoleName($admin->authId),
'createdAt' => $admin->createTime,
'lastLogin' => !empty($admin->lastLoginTime) ? date('Y-m-d H:i', $admin->lastLoginTime) : '从未登录',
'permissions' => $this->getPermissions($admin->authId)
];
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $data
]);
}
/**
* 根据权限ID获取角色名称
* @param int $authId 权限ID
* @return string
*/
private function getRoleName($authId)
{
// 可以从权限表中查询,这里为演示简化处理
switch($authId) {
case 1:
return '超级管理员';
case 2:
return '项目管理员';
case 3:
return '客户管理员';
default:
return '普通管理员';
}
}
/**
* 根据权限ID获取权限列表
* @param int $authId 权限ID
* @return array
*/
private function getPermissions($authId)
{
$ids = AdministratorPermissions::getPermissions($authId);
if ($ids) {
return \app\superadmin\model\Menu::getMenusNameByIds($ids);
}
return [];
}
/**
* 更新管理员信息
* @return \think\response\Json
*/
public function updateAdmin()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
// 获取当前登录的管理员信息
$currentAdmin = $this->request->adminInfo;
// 获取请求参数
$id = $this->request->post('id/d');
$username = $this->request->post('username/s');
$name = $this->request->post('name/s');
$password = $this->request->post('password/s');
$permissionIds = $this->request->post('permissionIds/a');
// 参数验证
if (empty($id) || empty($username) || empty($name)) {
return json(['code' => 400, 'msg' => '参数不完整']);
}
// 判断是否有权限修改
if ($currentAdmin->id != 1 && $currentAdmin->id != $id) {
return json(['code' => 403, 'msg' => '您没有权限修改其他管理员']);
}
// 查询管理员
$admin = AdminModel::where('id', $id)->where('deleteTime', 0)->find();
if (!$admin) {
return json(['code' => 404, 'msg' => '管理员不存在']);
}
// 准备更新数据
$data = [
'account' => $username,
'name' => $name,
'updateTime' => time()
];
// 如果提供了密码,则更新密码
if (!empty($password)) {
$data['password'] = md5($password);
}
// 更新管理员信息
$result = $admin->save($data);
// 如果当前是超级管理员(ID为1),并且修改的不是自己,则更新权限
if ($currentAdmin->id == 1 && $currentAdmin->id != $id && !empty($permissionIds)) {
\app\superadmin\model\AdministratorPermissions::savePermissions($id, $permissionIds);
}
return json([
'code' => 200,
'msg' => '更新成功',
'data' => null
]);
}
/**
* 添加管理员
* @return \think\response\Json
*/
public function addAdmin()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
// 获取当前登录的管理员信息
$currentAdmin = $this->request->adminInfo;
// 只有超级管理员(ID为1)可以添加管理员
if ($currentAdmin->id != 1) {
return json(['code' => 403, 'msg' => '您没有权限添加管理员']);
}
// 获取请求参数
$username = $this->request->post('username/s');
$name = $this->request->post('name/s');
$password = $this->request->post('password/s');
$permissionIds = $this->request->post('permissionIds/a');
// 参数验证
if (empty($username) || empty($name) || empty($password)) {
return json(['code' => 400, 'msg' => '参数不完整']);
}
// 检查账号是否已存在
$exists = AdminModel::where('account', $username)->where('deleteTime', 0)->find();
if ($exists) {
return json(['code' => 400, 'msg' => '账号已存在']);
}
// 创建管理员
$admin = new AdminModel();
$admin->account = $username;
$admin->name = $name;
$admin->password = md5($password);
$admin->status = 1;
$admin->createTime = time();
$admin->updateTime = time();
$admin->deleteTime = 0;
$admin->save();
// 保存权限
if (!empty($permissionIds)) {
\app\superadmin\model\AdministratorPermissions::savePermissions($admin->id, $permissionIds);
}
return json([
'code' => 200,
'msg' => '添加成功',
'data' => null
]);
}
/**
* 删除管理员
* @return \think\response\Json
*/
public function deleteAdmin()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
// 获取当前登录的管理员信息
$currentAdmin = $this->request->adminInfo;
// 获取请求参数
$id = $this->request->post('id/d');
// 参数验证
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数不完整']);
}
// 不能删除自己的账号
if ($currentAdmin->id == $id) {
return json(['code' => 403, 'msg' => '不能删除自己的账号']);
}
// 只有超级管理员(ID为1)可以删除管理员
if ($currentAdmin->id != 1) {
return json(['code' => 403, 'msg' => '您没有权限删除管理员']);
}
// 不能删除超级管理员账号
if ($id == 1) {
return json(['code' => 403, 'msg' => '不能删除超级管理员账号']);
}
// 查询管理员
$admin = AdminModel::where('id', $id)->where('deleteTime', 0)->find();
if (!$admin) {
return json(['code' => 404, 'msg' => '管理员不存在']);
}
// 执行软删除
$admin->deleteTime = time();
$result = $admin->save();
if ($result) {
return json([
'code' => 200,
'msg' => '删除成功',
'data' => null
]);
} else {
return json([
'code' => 500,
'msg' => '删除失败',
'data' => null
]);
}
}
}

View File

@@ -57,7 +57,7 @@ class Auth extends Controller
*/
private function createToken($admin)
{
$data = $admin->id . '|' . $admin->account . '|' . time();
$data = $admin->id . '|' . $admin->account;
return md5($data . 'cunkebao_admin_secret');
}
}

View File

@@ -0,0 +1,183 @@
<?php
namespace app\superadmin\controller;
use think\Controller;
use app\superadmin\model\Menu as MenuModel;
/**
* 菜单控制器
*/
class Menu extends Controller
{
/**
* 获取菜单列表(树状结构)
* @return \think\response\Json
*/
public function getMenuTree()
{
// 参数处理
$onlyEnabled = $this->request->param('only_enabled', 1);
$useCache = $this->request->param('use_cache', 0); // 由于要根据用户权限过滤,默认不使用缓存
// 获取当前登录的管理员信息
$adminInfo = $this->request->adminInfo;
// 调用模型获取菜单树
if ($adminInfo->id == 1) {
// 超级管理员获取所有菜单
$menuTree = MenuModel::getMenuTree($onlyEnabled, $useCache);
} else {
// 非超级管理员根据权限获取菜单
$permissionIds = \app\superadmin\model\AdministratorPermissions::getPermissions($adminInfo->id);
$menuTree = MenuModel::getMenuTreeByPermissions($permissionIds, $onlyEnabled);
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $menuTree
]);
}
/**
* 获取所有菜单(平铺结构,便于后台管理)
* @return \think\response\Json
*/
public function getMenuList()
{
// 查询条件
$where = [];
$status = $this->request->param('status');
if ($status !== null && $status !== '') {
$where[] = ['status', '=', intval($status)];
}
// 获取所有菜单
$menus = MenuModel::where($where)
->order('sort', 'asc')
->select();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $menus
]);
}
/**
* 添加或更新菜单
* @return \think\response\Json
*/
public function saveMenu()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
// 获取参数
$data = $this->request->post();
// 验证参数
$validate = $this->validate($data, [
'title|菜单名称' => 'require|max:50',
'path|路由路径' => 'require|max:100',
'parent_id|父菜单ID' => 'require|number',
'status|状态' => 'require|in:0,1',
'sort|排序' => 'require|number',
]);
if ($validate !== true) {
return json(['code' => 400, 'msg' => $validate]);
}
// 保存菜单
$result = MenuModel::saveMenu($data);
if ($result) {
return json(['code' => 200, 'msg' => '保存成功']);
} else {
return json(['code' => 500, 'msg' => '保存失败']);
}
}
/**
* 删除菜单
* @param int $id 菜单ID
* @return \think\response\Json
*/
public function deleteMenu($id)
{
if (!$this->request->isDelete()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
if (empty($id) || !is_numeric($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
$result = MenuModel::deleteMenu($id);
if ($result) {
return json(['code' => 200, 'msg' => '删除成功']);
} else {
return json(['code' => 500, 'msg' => '删除失败,可能存在子菜单']);
}
}
/**
* 更新菜单状态
* @return \think\response\Json
*/
public function updateStatus()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
$id = $this->request->post('id');
$status = $this->request->post('status');
if (empty($id) || !is_numeric($id) || !in_array($status, [0, 1])) {
return json(['code' => 400, 'msg' => '参数错误']);
}
$menu = MenuModel::find($id);
if (!$menu) {
return json(['code' => 404, 'msg' => '菜单不存在']);
}
$menu->status = $status;
$result = $menu->save();
// 清除缓存
MenuModel::clearMenuCache();
if ($result) {
return json(['code' => 200, 'msg' => '状态更新成功']);
} else {
return json(['code' => 500, 'msg' => '状态更新失败']);
}
}
/**
* 获取一级菜单(供权限设置使用)
* @return \think\response\Json
*/
public function getTopLevelMenus()
{
// 获取所有启用的一级菜单
$menus = \app\superadmin\model\Menu::where([
['parent_id', '=', 0],
['status', '=', 1]
])
->field('id, title')
->order('sort', 'asc')
->select();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $menus
]);
}
}

View File

@@ -1,2 +0,0 @@
INSERT INTO `tk_administrators` (`name`, `account`, `password`, `status`, `createTime`, `updateTime`)
VALUES ('超级管理员', 'admin', MD5('123456'), 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

View File

@@ -0,0 +1,74 @@
<?php
namespace app\superadmin\middleware;
/**
* 超级管理员后台登录认证中间件
*/
class AdminAuth
{
/**
* 处理请求
* @param \think\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
// 获取Cookie中的管理员信息
$adminId = cookie('admin_id');
$adminToken = cookie('admin_token');
// 如果没有登录信息返回401未授权
if (empty($adminId) || empty($adminToken)) {
return json([
'code' => 401,
'msg' => '请先登录',
'data' => null
]);
}
// 获取管理员信息
$admin = \app\superadmin\model\Administrator::where([
['id', '=', $adminId],
['status', '=', 1],
['deleteTime', '=', 0]
])->find();
// 如果管理员不存在返回401未授权
if (!$admin) {
return json([
'code' => 401,
'msg' => '管理员账号不存在或已被禁用',
'data' => null
]);
}
// 验证Token是否有效
$expectedToken = $this->createToken($admin);
if ($adminToken !== $expectedToken) {
return json([
'code' => 401,
'msg' => '登录已过期,请重新登录',
'data' => null
]);
}
// 将管理员信息绑定到请求对象,方便后续控制器使用
$request->adminInfo = $admin;
// 继续执行后续操作
return $next($request);
}
/**
* 创建登录令牌
* @param \app\superadmin\model\Administrator $admin
* @return string
*/
private function createToken($admin)
{
$data = $admin->id . '|' . $admin->account;
return md5($data . 'cunkebao_admin_secret');
}
}

View File

@@ -11,20 +11,6 @@ class Administrator extends Model
// 设置数据表名
protected $name = 'administrators';
// 设置数据表前缀
protected $prefix = 'tk_';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
protected $deleteTime = 'deleteTime';
// 隐藏字段
protected $hidden = [
'password'

View File

@@ -0,0 +1,90 @@
<?php
namespace app\superadmin\model;
use think\Model;
/**
* 超级管理员权限配置模型类
*/
class AdministratorPermissions extends Model
{
// 设置数据表名
protected $name = 'administrator_permissions';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
protected $deleteTime = 'deleteTime';
// 定义字段类型
protected $type = [
'id' => 'integer',
'adminId' => 'integer',
'permissions' => 'json',
'createTime' => 'integer',
'updateTime' => 'integer',
'deleteTime' => 'integer'
];
/**
* 保存管理员权限
* @param int $adminId 管理员ID
* @param array $permissionIds 权限ID数组
* @return bool
*/
public static function savePermissions($adminId, $permissionIds)
{
// 检查是否已有记录
$record = self::where('adminId', $adminId)->find();
// 准备权限数据
$permissionData = [
'ids' => is_array($permissionIds) ? implode(',', $permissionIds) : $permissionIds
];
if ($record) {
// 更新已有记录
return $record->save([
'permissions' => json_encode($permissionData),
'updateTime' => time()
]);
} else {
// 创建新记录
return self::create([
'adminId' => $adminId,
'permissions' => json_encode($permissionData),
'createTime' => time(),
'updateTime' => time(),
'deleteTime' => 0
]);
}
}
/**
* 获取管理员权限
* @param int $adminId 管理员ID
* @return array 权限ID数组
*/
public static function getPermissions($adminId)
{
$record = self::where('adminId', $adminId)->find();
if (!$record || empty($record->permissions)) {
return [];
}
$permissions = $record->permissions ? json_decode($record->permissions, true) : [];
if (isset($permissions['ids']) && !empty($permissions['ids'])) {
return is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
}
return [];
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace app\superadmin\model;
use think\Model;
use think\facade\Cache;
/**
* 菜单模型类
*/
class Menu extends Model
{
// 设置数据表名
protected $name = 'menus';
/**
* 获取所有菜单,并组织成树状结构
* @param bool $onlyEnabled 是否只获取启用的菜单
* @param bool $useCache 是否使用缓存
* @return array
*/
public static function getMenuTree($onlyEnabled = true, $useCache = true)
{
$cacheKey = 'superadmin_menu_tree' . ($onlyEnabled ? '_enabled' : '_all');
// 查询条件
$where = [];
if ($onlyEnabled) {
$where[] = ['status', '=', 1];
}
// 获取所有菜单
$allMenus = self::where($where)
->order('sort', 'asc')
->select()
->toArray();
// 组织成树状结构
$menuTree = self::buildMenuTree($allMenus);
// 缓存结果
if ($useCache) {
Cache::set($cacheKey, $menuTree, 3600); // 缓存1小时
}
return $menuTree;
}
public static function getMenusNameByIds($ids)
{
return self::whereIn('id', $ids)->column('title');
}
/**
* 构建菜单树
* @param array $menus 所有菜单
* @param int $parentId 父菜单ID
* @return array
*/
private static function buildMenuTree($menus, $parentId = 0)
{
$tree = [];
foreach ($menus as $menu) {
if ($menu['parent_id'] == $parentId) {
$children = self::buildMenuTree($menus, $menu['id']);
if (!empty($children)) {
$menu['children'] = $children;
}
$tree[] = $menu;
}
}
return $tree;
}
/**
* 根据权限ID获取相应的菜单树
* @param array $permissionIds 权限ID数组
* @param bool $onlyEnabled 是否只获取启用的菜单
* @return array
*/
public static function getMenuTreeByPermissions($permissionIds, $onlyEnabled = true)
{
// 如果没有权限,返回空数组
if (empty($permissionIds)) {
return [];
}
// 查询条件
$where = [];
if ($onlyEnabled) {
$where[] = ['status', '=', 1];
}
// 获取所有一级菜单(用户拥有权限的)
$topMenus = self::where($where)
->where('parent_id', 0)
->whereIn('id', $permissionIds)
->order('sort', 'asc')
->select()
->toArray();
// 菜单ID集合用于获取子菜单
$menuIds = array_column($topMenus, 'id');
// 获取所有子菜单
$childMenus = self::where($where)
->where('parent_id', 'in', $menuIds)
->order('sort', 'asc')
->select()
->toArray();
// 将子菜单按照父ID进行分组
$childMenusGroup = [];
foreach ($childMenus as $menu) {
$childMenusGroup[$menu['parent_id']][] = $menu;
}
// 构建菜单树
$menuTree = [];
foreach ($topMenus as $topMenu) {
// 添加子菜单
if (isset($childMenusGroup[$topMenu['id']])) {
$topMenu['children'] = $childMenusGroup[$topMenu['id']];
}
$menuTree[] = $topMenu;
}
return $menuTree;
}
}

View File

@@ -0,0 +1,35 @@
-- 创建菜单表
CREATE TABLE IF NOT EXISTS `tk_menus` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`title` varchar(50) NOT NULL COMMENT '菜单名称',
`path` varchar(100) NOT NULL COMMENT '路由路径',
`icon` varchar(50) DEFAULT NULL COMMENT '图标名称',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '父菜单ID0表示顶级菜单',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态1启用0禁用',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序,数值越小越靠前',
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
`update_time` int(11) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_parent_id` (`parent_id`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统菜单表';
-- 插入超级管理员顶级菜单
INSERT INTO `tk_menus` (`title`, `path`, `icon`, `parent_id`, `status`, `sort`, `create_time`, `update_time`) VALUES
('仪表盘', '/dashboard', 'LayoutDashboard', 0, 1, 10, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('项目管理', '/dashboard/projects', 'FolderKanban', 0, 1, 20, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('客户池', '/dashboard/customers', 'Users', 0, 1, 30, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('管理员权限', '/dashboard/admins', 'Settings', 0, 1, 40, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('系统设置', '/settings', 'Cog', 0, 1, 50, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
-- 插入子菜单
INSERT INTO `tk_menus` (`title`, `path`, `icon`, `parent_id`, `status`, `sort`, `create_time`, `update_time`) VALUES
('项目列表', '/dashboard/projects', 'List', 2, 1, 21, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('新建项目', '/dashboard/projects/new', 'PlusCircle', 2, 1, 22, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('客户管理', '/dashboard/customers', 'Users', 3, 1, 31, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('客户分析', '/dashboard/customers/analytics', 'BarChart', 3, 1, 32, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('管理员列表', '/dashboard/admins', 'UserCog', 4, 1, 41, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('角色管理', '/dashboard/admins/roles', 'ShieldCheck', 4, 1, 42, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('权限设置', '/dashboard/admins/permissions', 'Lock', 4, 1, 43, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('基本设置', '/settings/general', 'Settings', 5, 1, 51, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('安全设置', '/settings/security', 'Shield', 5, 1, 52, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

View File

@@ -12,7 +12,7 @@
// [ 应用入口文件 ]
namespace think;
//处理跨域预检请求
////处理跨域预检请求
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
//允许的源域名
header("Access-Control-Allow-Origin: *");

View File

@@ -0,0 +1,173 @@
<?php
/**
* 菜单表初始化脚本
* 执行该脚本,会创建菜单表并插入初始菜单数据
* 执行方式: php init-menu.php
*/
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
define('RUNTIME_PATH', __DIR__ . '/../runtime/');
define('ROOT_PATH', __DIR__ . '/../');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/base.php';
// 加载环境变量
use think\facade\Env;
$rootPath = realpath(__DIR__ . '/../');
Env::load($rootPath . '/.env');
// 读取数据库配置
$dbConfig = [
'type' => Env::get('database.type', 'mysql'),
'hostname' => Env::get('database.hostname', '127.0.0.1'),
'database' => Env::get('database.database', 'database'),
'username' => Env::get('database.username', 'root'),
'password' => Env::get('database.password', 'root'),
'hostport' => Env::get('database.hostport', '3306'),
'charset' => Env::get('database.charset', 'utf8mb4'),
'prefix' => Env::get('database.prefix', 'tk_'),
];
// 连接数据库
try {
$dsn = "{$dbConfig['type']}:host={$dbConfig['hostname']};port={$dbConfig['hostport']};dbname={$dbConfig['database']};charset={$dbConfig['charset']}";
$pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "数据库连接成功!\n";
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage() . "\n");
}
// 创建菜单表SQL
$createTableSql = "
CREATE TABLE IF NOT EXISTS `{$dbConfig['prefix']}menus` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`title` varchar(50) NOT NULL COMMENT '菜单名称',
`path` varchar(100) NOT NULL COMMENT '路由路径',
`icon` varchar(50) DEFAULT NULL COMMENT '图标名称',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '父菜单ID0表示顶级菜单',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态1启用0禁用',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序,数值越小越靠前',
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
`update_time` int(11) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_parent_id` (`parent_id`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统菜单表';
";
// 执行创建表SQL
try {
$pdo->exec($createTableSql);
echo "菜单表创建成功!\n";
} catch (PDOException $e) {
echo "菜单表创建失败: " . $e->getMessage() . "\n";
}
// 检查表中是否已有数据
$checkSql = "SELECT COUNT(*) FROM `{$dbConfig['prefix']}menus`";
try {
$count = $pdo->query($checkSql)->fetchColumn();
if ($count > 0) {
echo "菜单表中已有 {$count} 条数据,跳过数据初始化\n";
exit(0);
}
} catch (PDOException $e) {
echo "检查数据失败: " . $e->getMessage() . "\n";
exit(1);
}
// 插入顶级菜单数据
$topMenus = [
['仪表盘', '/dashboard', 'LayoutDashboard', 0, 1, 10],
['项目管理', '/dashboard/projects', 'FolderKanban', 0, 1, 20],
['客户池', '/dashboard/customers', 'Users', 0, 1, 30],
['管理员权限', '/dashboard/admins', 'Settings', 0, 1, 40],
['系统设置', '/settings', 'Cog', 0, 1, 50],
];
$insertTopMenuSql = "INSERT INTO `{$dbConfig['prefix']}menus`
(`title`, `path`, `icon`, `parent_id`, `status`, `sort`, `create_time`, `update_time`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$timestamp = time();
$insertStmt = $pdo->prepare($insertTopMenuSql);
$pdo->beginTransaction();
try {
foreach ($topMenus as $index => $menu) {
$insertStmt->execute([
$menu[0], // title
$menu[1], // path
$menu[2], // icon
$menu[3], // parent_id
$menu[4], // status
$menu[5], // sort
$timestamp,
$timestamp
]);
}
$pdo->commit();
echo "顶级菜单数据插入成功!\n";
} catch (PDOException $e) {
$pdo->rollBack();
echo "顶级菜单数据插入失败: " . $e->getMessage() . "\n";
exit(1);
}
// 查询刚插入的顶级菜单ID
$menuIds = [];
$queryTopMenuSql = "SELECT id, title FROM `{$dbConfig['prefix']}menus` WHERE parent_id = 0";
try {
$topMenusResult = $pdo->query($queryTopMenuSql)->fetchAll(PDO::FETCH_ASSOC);
foreach ($topMenusResult as $menu) {
$menuIds[$menu['title']] = $menu['id'];
}
} catch (PDOException $e) {
echo "查询顶级菜单失败: " . $e->getMessage() . "\n";
exit(1);
}
// 插入子菜单数据
$subMenus = [
['项目列表', '/dashboard/projects', 'List', $menuIds['项目管理'], 1, 21],
['新建项目', '/dashboard/projects/new', 'PlusCircle', $menuIds['项目管理'], 1, 22],
['客户管理', '/dashboard/customers', 'Users', $menuIds['客户池'], 1, 31],
['客户分析', '/dashboard/customers/analytics', 'BarChart', $menuIds['客户池'], 1, 32],
['管理员列表', '/dashboard/admins', 'UserCog', $menuIds['管理员权限'], 1, 41],
['角色管理', '/dashboard/admins/roles', 'ShieldCheck', $menuIds['管理员权限'], 1, 42],
['权限设置', '/dashboard/admins/permissions', 'Lock', $menuIds['管理员权限'], 1, 43],
['基本设置', '/settings/general', 'Settings', $menuIds['系统设置'], 1, 51],
['安全设置', '/settings/security', 'Shield', $menuIds['系统设置'], 1, 52],
];
$insertSubMenuSql = "INSERT INTO `{$dbConfig['prefix']}menus`
(`title`, `path`, `icon`, `parent_id`, `status`, `sort`, `create_time`, `update_time`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$pdo->beginTransaction();
try {
$insertStmt = $pdo->prepare($insertSubMenuSql);
foreach ($subMenus as $menu) {
$insertStmt->execute([
$menu[0], // title
$menu[1], // path
$menu[2], // icon
$menu[3], // parent_id
$menu[4], // status
$menu[5], // sort
$timestamp,
$timestamp
]);
}
$pdo->commit();
echo "子菜单数据插入成功!\n";
} catch (PDOException $e) {
$pdo->rollBack();
echo "子菜单数据插入失败: " . $e->getMessage() . "\n";
exit(1);
}
echo "菜单初始化完成!\n";