Blogger Information
Blog 77
fans 0
comment 0
visits 55568
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实战ele与tp:左侧菜单、管理员列表、翻页与搜索功能
Jet的博客
Original
374 people have browsed it

一、总体代码:

文件:

  • shop\src\view\Login.vue (更新)
  • shop\src\view\Index.vue (更新)
  • shop\src\network\index.js (更新)
  • tp6\app\admin\controller\index.php (更新)
  • shop\src\view\User.vue (新建)

1、Login.vue文件代码

  1. <template>
  2. <div class="container">
  3. <div style="text-align: center">
  4. <!--<img src="@/assets/logo.jpg" alt="logo" />-->
  5. <h3>后台管理</h3>
  6. </div>
  7. <div class="main">
  8. <!-- label-width="auto" 导致ElementPlusError: [ElForm] unexpected width 0 -->
  9. <el-form :model="form" size="large">
  10. <el-form-item prop="account">
  11. <el-input v-model="form.account" name="account" class="w-50 m-2" placeholder="请输入账号">
  12. <template #prefix>
  13. <el-icon class="el-input__icon" style="color: #1890ff"><Avatar /></el-icon>
  14. </template>
  15. </el-input>
  16. </el-form-item>
  17. <el-form-item prop="password">
  18. <el-input v-model="form.password" name="password" type="password" class="w-50 m-2" placeholder="请输入密码" show-password>
  19. <template #prefix>
  20. <el-icon class="el-input__icon" style="color: #1890ff"><Lock /></el-icon>
  21. </template>
  22. </el-input>
  23. </el-form-item>
  24. <el-form-item prop="remember">
  25. <el-checkbox v-model="form.remember" label="1" size="large">保存账密</el-checkbox>
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button type="primary" style="width: 100%" @click="onSubmit()">登录</el-button>
  29. </el-form-item>
  30. </el-form>
  31. </div>
  32. </div>
  33. </template>
  34. <style>
  35. .container {
  36. position: relative;
  37. width: 100%;
  38. min-height: 100%;
  39. padding: 110px 0 144px;
  40. background-repeat: no-repeat;
  41. background-position: center 110px;
  42. background-size: 100%;
  43. }
  44. .main {
  45. width: 368px;
  46. min-width: 260px;
  47. margin: 50px auto;
  48. }
  49. .el-icon {
  50. color: #359eff;
  51. }
  52. </style>
  53. <script>
  54. import { reactive } from "vue";
  55. //import { request } from "../network/request.js";
  56. import { login } from "../network/index.js";
  57. // 登录成功用路由进行跳转:
  58. import { useRouter } from "vue-router";
  59. export default {
  60. setup() {
  61. const form = reactive({
  62. account: "123",
  63. password: "123",
  64. remember: true,
  65. });
  66. const router = useRouter();
  67. const onSubmit = ()=>{
  68. login({
  69. account : form.account,
  70. password : form.password,
  71. }).then(res =>{
  72. alert(res.data.msg);
  73. if(res.data.code == 0){
  74. // 跳转到新页面
  75. router.push("/Index");
  76. }
  77. })
  78. }
  79. return {
  80. form,
  81. onSubmit
  82. };
  83. }
  84. };
  85. </script>

2、Index.vue文件代码

  1. <template>
  2. <el-container class="layout-container-demo" style="height: 500px">
  3. <el-aside width="200px">
  4. <el-scrollbar>
  5. <el-menu :default-openeds="['-1']">
  6. <!--二级路由,跳转-->
  7. <router-link to="/index/home">
  8. <el-menu-item index="-1">首页</el-menu-item>
  9. </router-link>
  10. <el-sub-menu index="1">
  11. <template #title>
  12. <el-icon><message /></el-icon>系统管理
  13. </template>
  14. <el-menu-item-group>
  15. <router-link to="/index/menu">
  16. <el-menu-item index="1-2">菜单管理</el-menu-item>
  17. </router-link>
  18. </el-menu-item-group>
  19. </el-sub-menu>
  20. <el-sub-menu index="2">
  21. <template #title>
  22. <el-icon><User /></el-icon>管理员管理
  23. </template>
  24. <el-menu-item-group>
  25. <router-link to="/index/user">
  26. <el-menu-item index="2-1">管理员列表</el-menu-item>
  27. </router-link>
  28. <router-link to="/index/group">
  29. <el-menu-item index="2-2">管理组列表</el-menu-item>
  30. </router-link>
  31. </el-menu-item-group>
  32. </el-sub-menu>
  33. <el-sub-menu index="3">
  34. <template #title>
  35. <el-icon><setting /></el-icon>商城相关
  36. </template>
  37. <el-menu-item-group>
  38. <template #title>商品</template>
  39. <el-menu-item index="3-1">商品列表</el-menu-item>
  40. </el-menu-item-group>
  41. <el-menu-item-group title="订单">
  42. <el-menu-item index="3-3">订单列表</el-menu-item>
  43. </el-menu-item-group>
  44. <el-menu-item-group title="用户">
  45. <el-menu-item index="3-4">用户列表</el-menu-item>
  46. </el-menu-item-group>
  47. </el-sub-menu>
  48. </el-menu>
  49. </el-scrollbar>
  50. </el-aside>
  51. <el-container>
  52. <el-main>
  53. <router-view></router-view>
  54. </el-main>
  55. </el-container>
  56. </el-container>
  57. </template>
  58. <script>
  59. import { defineComponent, ref } from "vue";
  60. export default defineComponent({
  61. data() {
  62. return {
  63. collapsed: ref(false),
  64. selectedKeys: ref(["1"]),
  65. };
  66. },
  67. });
  68. </script>
  69. <style scoped>
  70. .logo {
  71. height: 32px;
  72. margin: 16px;
  73. background: rgba(255, 255, 255, 0.3);
  74. }
  75. .site-layout .site-layout-background {
  76. background: #fff;
  77. }
  78. [data-theme="dark"] .site-layout .site-layout-background {
  79. background: #141414;
  80. }
  81. .layout-container-demo .el-header {
  82. position: relative;
  83. background-color: var(--el-color-primary-light-7);
  84. color: var(--el-text-color-primary);
  85. }
  86. .layout-container-demo .el-aside {
  87. color: var(--el-text-color-primary);
  88. background: var(--el-color-primary-light-8);
  89. }
  90. .layout-container-demo .el-menu {
  91. border-right: none;
  92. }
  93. .layout-container-demo .el-main {
  94. padding: 0;
  95. }
  96. .layout-container-demo .toolbar {
  97. display: inline-flex;
  98. align-items: center;
  99. justify-content: center;
  100. height: 100%;
  101. right: 20px;
  102. }
  103. </style>

3、index.js文件代码

  1. import { request } from "./request.js";
  2. // 跳转至tp项目文件
  3. export function login(data){
  4. return request ({
  5. url : "Login/index",
  6. data
  7. })
  8. }
  9. export function UserLists(data){
  10. return request ({
  11. url : "Index/UserLists",
  12. data
  13. })
  14. }

4、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 index()
  8. {
  9. 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>';
  10. }
  11. // 管理员列表
  12. public function UserLists(){
  13. $p = input('post.p',1);
  14. $search = input('post.search','');
  15. // 在查询数出来的时候,
  16. // $user = Db::table('xpcms_admin')->select()->toArray();
  17. // limit方法、pgae方法
  18. // count方法统计数据库有多少条数据
  19. $where = [];
  20. if(!empty($search)){
  21. $where1 = [['username','like','%'.$search.'%']];
  22. $where2 = [['real_name','like','%'.$search.'%']];
  23. $where3 = [['mobile','like','%'.$search.'%']];
  24. $where = [$where1,$where2,$where3];
  25. }
  26. $count = Db::table('xpcms_admin')->whereOr($where)->count();
  27. $user = Db::table('xpcms_admin')->whereOr($where)->page($p,5)->select()->toArray();
  28. $arr = [
  29. 'count' => $count,
  30. 'user' => $user,
  31. ];
  32. // php接口、只能有一次输出结果值
  33. echo json_encode([
  34. 'code' => 0,
  35. 'msg' => '成功',
  36. 'data' => $arr
  37. ]);
  38. }
  39. public function hello($name = 'ThinkPHP6')
  40. {
  41. return 'hello,' . $name;
  42. }
  43. }

5、User.vue文件代码

  1. <template>
  2. <div>
  3. <el-input v-model="data.search" size="small" placeholder="Type to search" />
  4. <el-button type="" @click="search()">搜索</el-button>
  5. <el-table
  6. :data="data.table"
  7. stripe
  8. style="width: 100%"
  9. @select="select"
  10. >
  11. <el-table-column label="选择" type="selection" />
  12. <el-table-column prop="id" label="id" />
  13. <el-table-column prop="username" label="username" />
  14. <el-table-column prop="real_name" label="姓名1" />
  15. <el-table-column prop="mobile" label="手机号" />
  16. <el-table-column prop="status" label="状态">
  17. <!--插槽-->
  18. <!-- scope 相当于循环里的一次数据
  19. foreach ( $data['table] as $v)
  20. scope.row 就是相当于是 $v
  21. -->
  22. <template #default="scope">
  23. <span>
  24. {{ scope.row.status == 0 ? "正常" : "冻结" }}
  25. </span>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. <el-pagination
  30. layout="prev,pager,next"
  31. :total="data.total"
  32. :page-size="5"
  33. @current-change="fun"
  34. />
  35. <!--
  36. total:总条数
  37. page-size:一页显示多少条
  38. -->
  39. </div>
  40. </template>
  41. <script>
  42. import { reactive } from "vue"; // 引入动态创建
  43. import { UserLists } from "../network/index";
  44. export default{
  45. setup () {
  46. const data = reactive({
  47. table : [],
  48. total : 0,
  49. });
  50. UserLists().then( (e)=>{
  51. data.table = e.data.data.user;
  52. data.total = e.data.data.count;
  53. console.log(data.table);
  54. })
  55. const select = (selection,row)=>{
  56. alert('这是选择事件')
  57. console.log(selection);
  58. console.log(row);
  59. }
  60. const fun = (p) => {
  61. UserLists({p:p}).then( (e)=>{
  62. data.table = e.data.data.user;
  63. data.total = e.data.data.count;
  64. })
  65. // Index.php接收传值
  66. }
  67. const search = () => {
  68. UserLists({search:data.search}).then( (e)=>{
  69. data.table = e.data.data.user;
  70. data.total = e.data.data.count;
  71. })
  72. }
  73. return {
  74. data,
  75. select,
  76. fun,
  77. search,
  78. }
  79. }
  80. }
  81. </script>

二、一些细节点

1、左侧菜单栏 Index.vue

实际目前只是使用ele写固定栏目,实行二级路由跳转

  1. <router-link to="/index/home">
  2. <el-menu-item index="-1">首页</el-menu-item>
  3. </router-link>


2、管理员列表 User.vue\Index.js\Index.php

  1. 使用ele框架,引入动态创建 import { reactive } from "vue";
  1. // 管理员列表
  2. public function UserLists(){
  3. // 在查询数出来的时候,
  4. $user = Db::table('xpcms_admin')->select()->toArray();
  5. echo json_encode([
  6. 'code' => 0,
  7. 'msg' => '成功',
  8. 'data' => $user
  9. ]);
  10. }
  1. UserLists().then( (e)=>{
  2. data.table = e.data.data.user;
  3. })

3、翻页 User.vue\Index.php

  1. <el-pagination
  2. layout="prev,pager,next"
  3. :total="data.total"
  4. :page-size="5"
  5. @current-change="fun"
  6. />
  7. <!--
  8. total:总条数
  9. page-size:一页显示多少条
  10. -->
  11. const fun = (p) => {
  12. UserLists({p:p}).then( (e)=>{
  13. data.table = e.data.data.user;
  14. data.total = e.data.data.count;
  15. })
  16. // Index.php接收传值
  17. }
  1. public function UserLists(){
  2. $p = input('post.p',1);
  3. // 在查询数出来的时候,
  4. // $user = Db::table('xpcms_admin')->select()->toArray();
  5. // limit方法、pgae方法
  6. // count方法统计数据库有多少条数据
  7. $count = Db::table('xpcms_admin')->count();
  8. $user = Db::table('xpcms_admin')->page($p,5)->select()->toArray();
  9. $arr = [
  10. 'count' => $count,
  11. 'user' => $user
  12. ];
  13. // php接口、只能有一次输出结果值
  14. echo json_encode([
  15. 'code' => 0,
  16. 'msg' => '成功',
  17. 'data' => $arr
  18. ]);
  19. }

4、搜索

  1. public function UserLists(){
  2. $p = input('post.p',1);
  3. $search = input('post.search','');
  4. // 在查询数出来的时候,
  5. // $user = Db::table('xpcms_admin')->select()->toArray();
  6. // limit方法、pgae方法
  7. // count方法统计数据库有多少条数据
  8. $where = [];
  9. if(!empty($search)){
  10. $where1 = [['username','like','%'.$search.'%']];
  11. $where2 = [['real_name','like','%'.$search.'%']];
  12. $where3 = [['mobile','like','%'.$search.'%']];
  13. $where = [$where1,$where2,$where3];
  14. }
  15. $count = Db::table('xpcms_admin')->whereOr($where)->count();
  16. $user = Db::table('xpcms_admin')->whereOr($where)->page($p,5)->select()->toArray();
  17. $arr = [
  18. 'count' => $count,
  19. 'user' => $user,
  20. ];
  21. // php接口、只能有一次输出结果值
  22. echo json_encode([
  23. 'code' => 0,
  24. 'msg' => '成功',
  25. 'data' => $arr
  26. ]);
  27. }
  1. const search = () => {
  2. UserLists({search:data.search}).then( (e)=>{
  3. data.table = e.data.data.user;
  4. data.total = e.data.data.count;
  5. })
  6. }


  1. 搜索与一开始读取用户列表,用一个语句,涉及到多个条件查询使用whereOr,若没有输入搜索条件,则全部展示,若输入条件,则判断3where条件进行查询。
  2. 分页使用page(data1,data2),data1:第几页;data2:一页显示多少条

运行效果图:


Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post