实战3 ele于tp:菜单操作(增删改)、左侧栏目循环

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
一、涉及代码文件文件:
  • shop/src/views/Menu.vue (新增:菜单管理 新建/修改/删除方法)
  • shop/src/network/Index.js (更新:菜单管理 新建/修改/删除请求方法)
  • tp6/app/admin/controller/Index.php (更新:菜单管理 新建/修改/删除 方法、左侧栏目 循环展示方法,给 Menu.vue 和 Index.vue 访问提供数据)
  • shop/src/views/Index.vue (更新:左侧栏目 循环展示)
1、Menu.vue文件代码
  1. <template>
  2. <div>
  3. <!--
  4. <el-space wrap style="margin-bottom:20px;">
  5. <el-input v-model="data.search" size="small" placeholder="Type to search" />
  6. <el-button type="" @click="search()">搜索</el-button>
  7. </el-space>-->
  8. <el-space wrap style="margin-bottom:20px;">
  9. <el-button type="" @click="add()">添加</el-button>
  10. <el-button type="" @click="go_url(1)" v-if="data.level==2" >上一级</el-button>
  11. </el-space>
  12. <el-table
  13. :data="data.table"
  14. border
  15. style="width: 100%"
  16. @select="select"
  17. >
  18. <!--
  19. <el-table-column align="right">
  20. </el-table-column>
  21. <el-table-column label="选择" type="selection" />-->
  22. <el-table-column prop="sort" label="排序" min-width="50" />
  23. <el-table-column prop="mid" label="ID" />
  24. <el-table-column prop="label" label="菜单名">
  25. </el-table-column>
  26. <el-table-column prop="type" label="菜单类型">
  27. <!--插槽-->
  28. <!-- scope 相当于循环里的一次数据
  29. foreach ( $data['table] as $v)
  30. scope.row 就是相当于是 $v
  31. -->
  32. <template #default="scope">
  33. <span v-if="scope.row.type == 0">
  34. 分组
  35. </span>
  36. <span v-else-if="scope.row.type == 1">
  37. 内部跳转
  38. </span>
  39. <span v-else-if="scope.row.type == 2">
  40. 外部跳转
  41. </span>
  42. </template>
  43. </el-table-column>
  44. <el-table-column prop="src" label="链接源">
  45. <template #default="scope">
  46. {{ scope.row.src ? scope.row.src : '' }}
  47. </template>
  48. </el-table-column>
  49. <el-table-column prop="status" label="状态">
  50. <template #default="scope">
  51. <span v-if="scope.row.status == 1">
  52. 正常
  53. </span>
  54. <span v-else-if="scope.row.status == 2">
  55. 隐藏
  56. </span>
  57. </template>
  58. </el-table-column>
  59. <el-table-column prop="mid" label="操作">
  60. <template #default="scope">
  61. <!-- 可直接返回
  62. <el-button type="primary" @click="go_url(scope.row)" v-if="scope.row.parent_id == 0">下一级</el-button>-->
  63. <el-button type="primary" @click="go_url(2,scope.row.mid)" v-if="scope.row.type == 0">下一级</el-button>
  64. <el-button type="warning" @click="add(scope.row)">修改</el-button>
  65. <el-button type="danger" @click="del(scope.row)">删除</el-button>
  66. </template>
  67. </el-table-column>
  68. </el-table>
  69. <el-pagination
  70. layout="prev, pager, next"
  71. :total="data.total"
  72. :page-size="10"
  73. @current-change="fun"
  74. />
  75. <!--
  76. total:总条数
  77. page-size:一页显示多少条
  78. -->
  79. <!--添加/修改 弹窗-->
  80. <el-dialog v-model="data.add_form" :title="data.title">
  81. <el-form :model="form" label-width="120px" size="large">
  82. <el-form-item label="菜单名">
  83. <el-input v-model="form.label" />
  84. </el-form-item>
  85. <el-form-item label="所属">
  86. <el-select v-model="form.parent_id" class="m-2" placeholder="Select" size="large">
  87. <el-option label="顶层" value="0" />
  88. <el-option :label="item.label" :value="item.mid" v-for="(item,index,key) in data.ss" />
  89. </el-select>
  90. </el-form-item>
  91. <el-form-item label="菜单类型">
  92. <el-select v-model="form.type" class="m-2" placeholder="Select" size="large">
  93. <el-option label="分组" :value="0" />
  94. <el-option label="内部跳转" :value="1" />
  95. <el-option label="外部跳转" :value="2" />
  96. </el-select>
  97. </el-form-item>
  98. <el-form-item label="链接源">
  99. <el-input v-model="form.src" />
  100. </el-form-item>
  101. <el-form-item label="排序">
  102. <el-input v-model="form.sort" />
  103. </el-form-item>
  104. <el-form-item label="状态">
  105. <el-select v-model="form.status" class="m-2" placeholder="Select" size="large">
  106. <el-option label="开启" value="1" />
  107. <el-option label="关闭" value="0" />
  108. </el-select>
  109. </el-form-item>
  110. </el-form>
  111. <template #footer>
  112. <span class="dialog-footer">
  113. <el-button @click="data.add_form = false">取消</el-button>
  114. <el-button type="primary" @click="add_form_json()">
  115. {{ data.title }}
  116. </el-button>
  117. </span>
  118. </template>
  119. </el-dialog>
  120. </div>
  121. </template>
  122. <script>
  123. import { reactive } from "vue"; // 引入动态创建
  124. import { useRoute, useRouter } from "vue-router"; //跳转路由
  125. import { MenuLists , MenuAdd , MenuDel } from "../network/index";
  126. export default{
  127. setup () {
  128. // 展示数据
  129. const data = reactive({
  130. table : [],
  131. total : 0,
  132. ss : [],
  133. search : '',
  134. add_form : false,
  135. title : '添加',
  136. level : 1 , // 当前为1, 下面判断上一级下一级按钮是否显示依据
  137. });
  138. // 展示的请求接口
  139. MenuLists().then( (e)=>{
  140. data.table = e.data.data.menu;
  141. data.total = e.data.data.count;
  142. //console.log(data.table);
  143. data.ss = e.data.data.ss;
  144. })
  145. // 选择事件
  146. const select = (selection,row)=>{
  147. console.log(selection);
  148. console.log(row);
  149. }
  150. // 翻页方法
  151. const fun = (p) => {
  152. MenuLists({p:p}).then( (e)=>{
  153. data.table = e.data.data.menu;
  154. })
  155. // Index.php接收传值
  156. }
  157. // 菜单 下一跳转
  158. const router = useRouter();
  159. /* 这方法可行
  160. const go_url = (e) => {
  161. /*
  162. router.push({
  163. path: "/index/menu",
  164. query: {
  165. mid : e.mid
  166. }
  167. })
  168. MenuLists({mid:e.mid}).then( (e)=>{
  169. data.table = e.data.data.menu;
  170. data.total = e.data.data.count;
  171. })
  172. }
  173. */
  174. const go_url = (e,mid) => {
  175. let m = 0;
  176. if ( e == 2 ){
  177. m = mid;
  178. data.level = 2;
  179. } else {
  180. m = 0;
  181. data.level = 1;
  182. }
  183. MenuLists({mid:m}).then( (ee)=>{
  184. data.table = ee.data.data.menu;
  185. data.total = ee.data.data.count;
  186. //data.ss = e.data.data.ss;
  187. })
  188. }
  189. // 搜索方法
  190. const search = () => {
  191. MenuLists({search:data.search}).then( (e)=>{
  192. data.table = e.data.data.menu;
  193. data.total = e.data.data.count;
  194. data.ss = e.data.data.ss;
  195. })
  196. }
  197. // 添加 修改
  198. // 展开form表单
  199. const form = reactive({
  200. mid : 0,
  201. label: '',
  202. type: '0',
  203. src : '',
  204. sort : 0,
  205. status: '1',
  206. parent_id: '0',
  207. })
  208. // 添加方法,如果为真,打开表单
  209. // 点击接收传uid数据,若有
  210. const add = (e) => {
  211. console.log(e);
  212. data.add_form = true;
  213. if(e){
  214. form.mid = e.mid;
  215. form.label = e.label;
  216. form.parent_id = e.parent_id;
  217. form.type = e.type;
  218. form.src = e.src;
  219. form.sort = e.sort;
  220. form.status = e.status;
  221. data.title = '修改';
  222. }else{
  223. form.mid = 0;
  224. form.label = '';
  225. form.parent_id = '0';
  226. form.type = '0' ;
  227. form.src = '';
  228. form.sort = 0 ;
  229. form.status = '1' ;
  230. data.title = '添加';
  231. }
  232. }
  233. // 确定提交添加的数据
  234. const add_form_json = () => {
  235. // 需要把数据提交给php接口
  236. // console.log(form);
  237. MenuAdd(form).then((e) =>{
  238. // console.log(e);
  239. alert(e.data.msg);
  240. if(e.data.code == 0){
  241. MenuLists().then( (e)=>{
  242. // 添加成功后自动刷新数据
  243. data.table = e.data.data.menu;
  244. data.total = e.data.data.count;
  245. console.log(data.table);
  246. // 关闭添加表单
  247. data.add_form = false;
  248. })
  249. }
  250. })
  251. }
  252. // 删除
  253. const del = (e) => {
  254. MenuDel({mid:e.mid}).then( (res) => {
  255. alert(res.data.msg);
  256. if( res.data.code == 0 ){
  257. MenuLists().then( (e)=>{
  258. // 添加成功后自动刷新数据
  259. data.table = e.data.data.menu;
  260. data.total = e.data.data.count;
  261. data.add_form = false;
  262. })
  263. }
  264. })
  265. }
  266. // 数据暴露出去
  267. return {
  268. data,
  269. select,
  270. fun,
  271. go_url,
  272. search,
  273. add,
  274. form,
  275. add_form_json,
  276. del,
  277. }
  278. }
  279. }
  280. </script>

2、index.js文件代码
  1. import { request } from "./request.js";
  2. // 跳转至tp项目文件
  3. export function login(data){
  4. return request ({
  5. url : "Login/index",
  6. method: "POST",
  7. data
  8. })
  9. }
  10. // 首页左侧菜单
  11. export function Index(data){
  12. return request ({
  13. url : "Index/index",
  14. method: "POST",
  15. data
  16. })
  17. }
  18. // =======================管理员列表===============================
  19. // 展示管理员请求
  20. export function UserLists(data){
  21. return request ({
  22. url : "Index/UserLists",
  23. method: "POST",
  24. data
  25. })
  26. }
  27. // 添加数据请求
  28. export function UserAdd(data){
  29. return request ({
  30. url : "Index/UserAdd",
  31. method: "POST",
  32. data
  33. })
  34. }
  35. // 修改数据请求
  36. export function UserEdit(data){
  37. return request ({
  38. url : "Index/UserEdit",
  39. method: "POST",
  40. data
  41. })
  42. }
  43. // 删除数据请求
  44. export function UserDel(data){
  45. return request ({
  46. url : "Index/UserDel",
  47. method: "POST",
  48. data
  49. })
  50. }
  51. // =======================管理组列表===============================
  52. // 展示管理组请求
  53. export function GroupLists(data){
  54. return request ({
  55. url : "Index/GroupLists",
  56. method: "POST",
  57. data
  58. })
  59. }
  60. // 新增管理组请求
  61. export function GroupAdd(data){
  62. return request ({
  63. url : "Index/GroupAdd",
  64. method: "post",
  65. data
  66. })
  67. }
  68. // 删除管理组请求
  69. export function GroupDel(data){
  70. return request ({
  71. url : "Index/GroupDel",
  72. method: "post",
  73. data
  74. })
  75. }
  76. // =======================菜单列表===============================
  77. // 展示菜单数据请求
  78. export function MenuLists(data){
  79. return request ({
  80. url : "Index/MenuLists",
  81. method: "POST",
  82. data
  83. })
  84. }
  85. // 新增菜单请求
  86. export function MenuAdd(data){
  87. return request ({
  88. url : "Index/MenuAdd",
  89. method: "post",
  90. data
  91. })
  92. }
  93. // 删除菜单请求
  94. export function MenuDel(data){
  95. return request ({
  96. url : "Index/MenuDel",
  97. method: "post",
  98. data
  99. })
  100. }

3、Index.php文件代码
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. class Index extends BaseController
  6. {
  7. public function __construct(){
  8. // 跨域请求
  9. header("Access-Control-Allow-Origin:*");
  10. }
  11. // 首页左侧菜单
  12. public function index(){
  13. // 状态为1的全部展示出来,但是1级和2级的都在一起
  14. $tmp = Db::table('bew_admin_sys_menu')->where('status',1)->order('parent_id')->select()->toArray();
  15. $menu = [];
  16. foreach ($tmp as $v){
  17. if($v['parent_id'] == 0){
  18. // 将 menu为1000或者100的下标 保存到 这个菜单
  19. $menu[ $v['mid'] ] = $v;
  20. } else {
  21. // 现在时二级菜单,它的上一级,就是parent_id,找到它自己的parent_id
  22. // $v['mid'] 一级菜单的下标 $v['parent_id'] 二级菜单的上级下标
  23. $menu[ $v['parent_id'] ] ['son'] [] = $v;
  24. }
  25. }
  26. echo json_encode([
  27. 'code' => 0,
  28. 'msg' => '成功',
  29. 'data' => $menu
  30. ]);
  31. }
  32. /*
  33. public function index()
  34. {
  35. return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
  36. }
  37. */
  38. // =======================管理员方法===============================
  39. // 管理员列表
  40. public function UserLists(){
  41. $p = input('post.p',1);
  42. $search = input('post.search','');
  43. // 在查询数出来的时候,
  44. // $user = Db::table('xpcms_admin')->select()->toArray();
  45. // limit方法、pgae方法
  46. // count方法统计数据库有多少条数据
  47. $where = [];
  48. if(!empty($search)){
  49. $where1 = [['account','like','%'.$search.'%']];
  50. $where2 = [['name','like','%'.$search.'%']];
  51. $where3 = [['phone','like','%'.$search.'%']];
  52. $where = [$where1,$where2,$where3];
  53. }
  54. $count = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->count();
  55. $user = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->page($p,5)->select()->toArray();
  56. // toArray 数组形式,显示时间戳
  57. foreach($user as &$u){
  58. $u['time_add_s'] = date('Y-m-d H:i:s',$u['time_add']);
  59. }
  60. $arr = [
  61. 'count' => $count,
  62. 'user' => $user,
  63. ];
  64. // php接口、只能有一次输出结果值
  65. echo json_encode([
  66. 'code' => 0,
  67. 'msg' => '成功',
  68. 'data' => $arr
  69. ]);
  70. }
  71. // 添加&修改管理员,接收数据
  72. public function UserAdd(){
  73. $post = input('post.');
  74. $data = [
  75. 'account' => $post['account'],
  76. 'name' => $post['name'],
  77. 'phone' => $post['phone'],
  78. 'qq' => $post['qq'],
  79. 'sex' => $post['sex'],
  80. 'time_last' => time(),
  81. ];
  82. // 查询单条数据
  83. $user = Db::table('bew_admin_user')->where('account',$post['account'])->find();
  84. if ( $post['uid'] == 0 ){
  85. // 判断查询出来的用户是否存在,若存在就退出,否则新建
  86. if(!empty($user)){
  87. echo json_encode([
  88. 'code' => 1,
  89. 'msg' => '账号已存在'
  90. ]);
  91. exit;
  92. }
  93. // 如果为0.就添加
  94. $data['password'] = md5($post['password']);
  95. $data['time_add'] = time();
  96. $ret = Db::table('bew_admin_user')->insert($data);
  97. } else {
  98. // 判断查询出来的用户名是否存在,且是不等于查询出来的uid,则修改
  99. if(!empty($user) && $post['uid'] != $user['uid']){
  100. echo json_encode([
  101. 'code' => 1,
  102. 'msg' => '账号已存在'
  103. ]);
  104. exit;
  105. }
  106. // 如果不为0 就修改
  107. if(!empty($post['password'])){
  108. $data['password'] = md5($post['password']);
  109. }
  110. $ret = Db::table('bew_admin_user')->where('uid', $post['uid'])->update($data);
  111. }
  112. // 添加&修改 后输出结果
  113. if( $post['uid'] == 0 ){
  114. if(empty($ret)){
  115. echo json_encode([
  116. 'code' => 1,
  117. 'msg' => '添加失败,请重试'
  118. ]);
  119. exit;
  120. };
  121. echo json_encode([
  122. 'code' => 0,
  123. 'msg' => '添加成功'
  124. ]);
  125. } else {
  126. if(empty($ret)){
  127. echo json_encode([
  128. 'code' => 1,
  129. 'msg' => '修改失败,请重试'
  130. ]);
  131. exit;
  132. };
  133. echo json_encode([
  134. 'code' => 0,
  135. 'msg' => '修改成功'
  136. ]);
  137. }
  138. }
  139. // 删除方法
  140. public function UserDel(){
  141. $uid = input('post.uid',0);
  142. if($uid ==0){
  143. echo json_encode([
  144. 'code' => 1,
  145. 'msg' => '请选择要删除的账号'
  146. ]);
  147. exit;
  148. }
  149. $del = Db::table('bew_admin_user')->where('uid', $uid)->update([
  150. 'status'=>2,
  151. 'time_last' => time(),
  152. ]);
  153. if(empty($del)){
  154. echo json_encode([
  155. 'code' => 1,
  156. 'msg' => '删除失败,请重试'
  157. ]);
  158. exit;
  159. }
  160. echo json_encode([
  161. 'code' => 0,
  162. 'msg' => '删除成功'
  163. ]);
  164. exit;
  165. }
  166. // =======================管理组方法===============================
  167. // 管理组列表接口
  168. public function GroupLists(){
  169. $p = input('post.p',1);
  170. $search = input('post.search','');
  171. $where = [];
  172. if(!empty($search)){
  173. $where[] = ['group_name','like','%'.$search.'%'];
  174. }
  175. $count = Db::table('bew_admin_user_group')->where('status','<>',0)->where($where)->count();
  176. $group = Db::table('bew_admin_user_group')->where('status','<>',0)->where($where)->page($p,3)->select()->toArray();
  177. foreach($group as &$u){
  178. $u['time_add_s'] = date('Y-m-d H:i:s',$u['time_add']);
  179. }
  180. // =================================================================================================
  181. // 新增权限管理
  182. // 状态为1的全部展示出来,但是1级和2级的都在一起
  183. $tmp = Db::table('bew_admin_sys_menu')->where('status',1)->order('parent_id')->select()->toArray();
  184. $menu = [];
  185. foreach ($tmp as $v){
  186. if($v['parent_id'] == 0){
  187. // 将 menu为1000或者100的下标 保存到 这个菜单
  188. $menu[ $v['mid'] ] = $v;
  189. } else {
  190. // 现在时二级菜单,它的上一级,就是parent_id,找到它自己的parent_id
  191. // $v['mid'] 一级菜单的下标 $v['parent_id'] 二级菜单的上级下标
  192. $menu[ $v['parent_id'] ] ['son'] [] = $v;
  193. }
  194. }
  195. // =================================================================================================
  196. $arr = [
  197. 'count' => $count,
  198. 'group' => $group,
  199. 'menu' => $menu,
  200. ];
  201. // php接口、只能有一次输出结果值
  202. echo json_encode([
  203. 'code' => 0,
  204. 'msg' => '成功',
  205. 'data' => $arr
  206. ]);
  207. }
  208. // 添加管理组
  209. public function GroupAdd(){
  210. $post = input('post.');
  211. $group = Db::table('bew_admin_user_group')->where('group_name', $post['group_name'])->find();
  212. if($post['group_id'] == 0){
  213. if(!empty($group)){
  214. echo json_encode([
  215. 'code' => 1,
  216. 'msg' => '添加失败,部门已存在'
  217. ]);
  218. exit;
  219. }
  220. $ret = Db::table('bew_admin_user_group')->insert([
  221. 'group_name' => $post['group_name'],
  222. 'status' => $post['status'],
  223. 'rights' => json_encode($post['checkList']),
  224. 'time_add' => time()
  225. ]);
  226. } else {
  227. if(!empty($group) && $group['group_id'] != $post['group_id']){
  228. echo json_encode([
  229. 'code' => 1,
  230. 'msg' => '修改失败,部门已存在'
  231. ]);
  232. exit;
  233. }
  234. $ret = Db::table('bew_admin_user_group')->where('group_id',$post['group_id'])->update([
  235. 'group_name' => $post['group_name'],
  236. 'status' => $post['status'],
  237. 'rights' => json_encode($post['checkList']),
  238. 'time_add' => time(),
  239. 'time_last' => time()
  240. ]);
  241. }
  242. if($post['group_id'] == 0 ){
  243. if(empty($ret)){
  244. echo json_encode([
  245. 'code' => 1,
  246. 'msg' => '添加失败,请重试'
  247. ]);
  248. exit;
  249. }
  250. echo json_encode([
  251. 'code' => 0,
  252. 'msg' => '添加成功'
  253. ]);
  254. } else {
  255. if(empty($ret)){
  256. echo json_encode([
  257. 'code' => 1,
  258. 'msg' => '修改失败,请重试'
  259. ]);
  260. exit;
  261. }
  262. echo json_encode([
  263. 'code' => 0,
  264. 'msg' => '修改成功'
  265. ]);
  266. }
  267. }
  268. // 删除管理组
  269. public function GroupDel(){
  270. $group_id = input('post.group_id',0);
  271. if($group_id == 0){
  272. echo json_encode([
  273. 'code' => 1,
  274. 'msg' => '请选择要删除的管理组'
  275. ]);
  276. exit;
  277. }
  278. $ret = Db::table('bew_admin_user_group')->where('group_id',$group_id)->update([
  279. 'status' => 0,
  280. 'time_last' => time()
  281. ]);
  282. if(empty($ret)){
  283. echo json_encode([
  284. 'code' => 1,
  285. 'msg' => '删除失败,请重试'
  286. ]);
  287. exit;
  288. }
  289. echo json_encode([
  290. 'code' => 0,
  291. 'msg' => '删除成功'
  292. ]);
  293. }
  294. // =======================菜单方法===============================
  295. /**
  296. * 一般分php文件,是根据功能来分
  297. * 用户相关放一个文件,并且统一处理
  298. * 用户个人中心、订单列表、订单详情、充值功能、登录日志等
  299. * 登录、注册放在一个文件,用户的未登录文件,统一处理的
  300. *
  301. * @return void
  302. */
  303. public function MenuLists(){
  304. $p = input('post.p',1);
  305. // 接收数据 mid
  306. $mid = input('post.mid',0);
  307. $count = Db::table('bew_admin_sys_menu')->where('parent_id',$mid)->count();
  308. $menu = Db::table('bew_admin_sys_menu')->where('parent_id',$mid)->order('sort DESC')->page($p,10)->select()->toArray();
  309. //需要把一级菜单获取出来,作为添加时选择项
  310. $ss = Db::table('bew_admin_sys_menu')->where('parent_id',0)->where('type',0)->order('sort DESC')->select()->toArray();
  311. $arr = [
  312. 'menu' => $menu,
  313. 'count' => $count,
  314. 'ss' => $ss,
  315. ];
  316. echo json_encode([
  317. 'code' => 0,
  318. 'msg' => '成功',
  319. 'data' => $arr
  320. ]);
  321. }
  322. // 添加 修改 菜单方法
  323. public function MenuAdd(){
  324. $post = input('post.');
  325. $data = [
  326. 'label' => $post['label'],
  327. 'parent_id' => $post['parent_id'],
  328. 'type' => $post['type'],
  329. 'src' => $post['src'],
  330. 'sort' => $post['sort'],
  331. 'status' => $post['status'],
  332. 'time_last' => time(),
  333. ];
  334. if($post['mid'] == 0){
  335. $ret = Db::table('bew_admin_sys_menu')->insert($data);
  336. } else {
  337. $ret = Db::table('bew_admin_sys_menu')->where('mid',$post['mid'])->update($data);
  338. }
  339. if($post['mid'] == 0 ){
  340. if(empty($ret)){
  341. echo json_encode([
  342. 'code' => 1,
  343. 'msg' => '添加失败,请重试'
  344. ]);
  345. exit;
  346. }
  347. echo json_encode([
  348. 'code' => 0,
  349. 'msg' => '添加成功'
  350. ]);
  351. } else {
  352. if(empty($ret)){
  353. echo json_encode([
  354. 'code' => 1,
  355. 'msg' => '修改失败,请重试'
  356. ]);
  357. exit;
  358. }
  359. echo json_encode([
  360. 'code' => 0,
  361. 'msg' => '修改成功'
  362. ]);
  363. }
  364. }
  365. // 删除菜单方法
  366. public function MenuDel(){
  367. $mid = input('post.mid',0);
  368. if($mid == 0){
  369. echo json_encode([
  370. 'code' => 1,
  371. 'msg' => '请选择要删除的菜单'
  372. ]);
  373. exit;
  374. }
  375. $del = Db::table('bew_admin_sys_menu')->where('mid',$mid)->update([
  376. 'status' => 2,
  377. 'time_last' => time(),
  378. ]);
  379. if(empty($del)){
  380. echo json_encode([
  381. 'code' => 1,
  382. 'msg' => '删除失败,请重试'
  383. ]);
  384. exit;
  385. }
  386. echo json_encode([
  387. 'code' => 0,
  388. 'msg' => '删除菜单成功'
  389. ]);
  390. }
  391. public function hello($name = 'ThinkPHP6')
  392. {
  393. return 'hello,' . $name;
  394. }
  395. }

4、Index.vue文件代码
  1. <!-- 1、新建 /src/views/Index/Index.vue 文件 -->
  2. <template>
  3. <el-container class="layout" style="height: 100%">
  4. <aside style="height: 100%">
  5. <el-menu default-active="-1" class="el-menu-vertical-demo" :collapse="isCollapse" active-text-color="#ffd04b" background-color="#545c64" text-color="#fff">
  6. <div class="logo">
  7. <router-link to="/index/home" class="router">
  8. <!--<img src="@/assets/logo.jpg" alt="logo" />-->
  9. <h1>phpAdmin</h1>
  10. </router-link>
  11. </div>
  12. <router-link to="/index/home">
  13. <el-menu-item index="-1">
  14. <template #title>首页</template>
  15. </el-menu-item>
  16. </router-link>
  17. <router-link to="/index/home">
  18. <el-menu-item index="-2">
  19. <template #title>个人中心</template>
  20. </el-menu-item>
  21. </router-link>
  22. <template v-for="(item,index,key) in menu">
  23. <el-sub-menu :index="item.mid" v-if="item.type == 0">
  24. <template #title>
  25. <span>{{ item.label }}</span>
  26. </template>
  27. <template v-for="(items,indexs,keys) in item.son">
  28. <router-link :to="items.src" v-if="items.type == 1">
  29. <el-menu-item :index="item.mid + items.mid">
  30. <span>{{ items.label }}</span>
  31. </el-menu-item>
  32. </router-link>
  33. <a :href="items.src" target="_blank" v-if="items.type == 2">
  34. <el-menu-item :index="item.mid + items.mid">
  35. <span>{{ items.label }}</span>
  36. </el-menu-item>
  37. </a>
  38. </template>
  39. </el-sub-menu>
  40. <router-link :to="item.src" v-if="item.type == 1">
  41. <el-menu-item :index="index + item.mid">
  42. <template #title>{{ item.label }}</template>
  43. </el-menu-item>
  44. </router-link>
  45. </template>
  46. </el-menu>
  47. <div class="flexible" @click="isCollapse = !isCollapse">
  48. <el-icon v-if="isCollapse" color="white" :size="40"><ArrowRight /></el-icon>
  49. <el-icon v-else color="white" :size="40"><ArrowLeft /></el-icon>
  50. </div>
  51. </aside>
  52. <el-container>
  53. <el-header style="text-align: right; font-size: 20px">
  54. <div class="toolbar">
  55. <el-dropdown size="large" type="primary">
  56. <span>{{name}}<el-icon style="margin-left: 8px; margin-top: 1px"><ArrowDown /></el-icon></span>
  57. <template #dropdown>
  58. <el-dropdown-menu>
  59. <el-dropdown-item>个人中心</el-dropdown-item>
  60. <el-dropdown-item>退出</el-dropdown-item>
  61. </el-dropdown-menu>
  62. </template>
  63. </el-dropdown>
  64. </div>
  65. </el-header>
  66. <el-main>
  67. <router-view></router-view>
  68. </el-main>
  69. </el-container>
  70. </el-container>
  71. </template>
  72. <script>
  73. import { reactive, toRefs } from "vue";
  74. import { Index } from "../network/index";
  75. export default {
  76. name: "App",
  77. setup() {
  78. // 获取左侧菜单和用户信息
  79. const state = reactive({
  80. isCollapse: false,
  81. name : "欧阳克",
  82. menu : []
  83. });
  84. Index().then( (e) => {
  85. state.menu = e.data.data;
  86. console.log(state);
  87. })
  88. return {
  89. ...toRefs(state),
  90. };
  91. }
  92. };
  93. </script>
  94. <style>
  95. body,
  96. html{
  97. margin : 0px 0px;
  98. height: 100%;
  99. }
  100. a {
  101. text-decoration: none;
  102. }
  103. .layout {
  104. background-color: #f0f2f5;
  105. }
  106. .layout .el-header {
  107. position: relative;
  108. background-color: white;
  109. color: var(--el-text-color-primary);
  110. }
  111. .layout aside {
  112. color: var(--el-text-color-primary);
  113. background: #001529;
  114. }
  115. .layout .el-menu {
  116. border-right: none;
  117. }
  118. .layout .el-main {
  119. margin: 30px 10px;
  120. background-color: white;
  121. }
  122. .layout .toolbar {
  123. display: inline-flex;
  124. align-items: center;
  125. justify-content: center;
  126. height: 100%;
  127. right: 20px;
  128. }
  129. aside {
  130. position: relative;
  131. }
  132. .flexible {
  133. background-color: #002140;
  134. text-align: center;
  135. position: absolute;
  136. bottom: 0px;
  137. left: 0px;
  138. right: 0px;
  139. }
  140. .el-menu-vertical-demo:not(.el-menu--collapse) {
  141. width: 200px;
  142. min-height: 100vh;
  143. }
  144. .layout aside .logo .router {
  145. display: flex;
  146. justify-content: space-evenly;
  147. align-items: center;
  148. animation-duration: 0.1s;
  149. overflow: hidden;
  150. text-decoration: none;
  151. padding-left: 1px;
  152. position: relative;
  153. left: 2px;
  154. }
  155. .layout aside .logo .router h1 {
  156. margin-left: 10px;
  157. color: aliceblue;
  158. overflow: hidden;
  159. }
  160. .layout aside .logo .router img {
  161. padding-left: 5px;
  162. width: 48px;
  163. height: 48px;
  164. }
  165. </style>

5、运行截图5.1、菜单添加效果:


5.2、菜单修改效果:


5.3、菜单删除效果:



5.4、左侧菜单循环展示:


6、记录遇到的两个问题6.1、管理人员列表多页,在第4页进行修改,修改成功后返回第1页,但是页码还是保留在原页码上



6.2、菜单栏目多页,在第二页点击进入下一级(无法进入),会跳回第一页



以上两个问题,应该都是页码问题导致,还需要研究!

【文章出处:香港站群多ip服务器 http://www.558idc.com/hkzq.html提供,感恩】