零基础快速开发Vue图书管理系统—角色管理篇(五)

简介: 零基础快速开发Vue图书管理系统—角色管理篇(五)

一、用户管理页面前端结构部分

<template>
    <div>
        <a-card>
            <h2>用户管理</h2>
            <a-divider></a-divider>
            <a-button>添加用户</a-button>
            <a-divider></a-divider>
            <div>
                <a-table 
                bordered 
                :pagination="false"
                :columns ="columns"
                :data-source ="list"
                >
                <template #createdAt="{record}">
                    {{ formatTimestamp(record.meta.createdAt) }}
                </template>
                <template #actions="{record}">
                <a href="javascript;">重置密码</a>
               &nbsp;
                <a href="javascript;">删除</a>
                </template>
            </a-table>
            </div>
            <space-between1 style="margin-top:24px;">
                <a-pagination></a-pagination>
            </space-between1>
        </a-card>
    </div>
</template>
<script src="./index.js"></script>
<style lang="scss" scoped>
     @import './index.scss';
</style>
import { defineComponent, ref, onMounted } from 'vue';
import { user } from '@/service';
import { result, formatTimestamp } from '@/helpers/utils'
const columns = [{
    title: '账户',
    dataIndex: 'account'
}, {
    title: '创建日期',
    slots: { customRender: 'createdAt' }
}, {
    title: '操作',
    slots: { customRender: 'actions' }
}]
export default defineComponent({
    setup() {
        const list = ref([]);
        const total = ref(0);
        const curPage = ref(1);
        const getUser = async() => {
            const res = await user.list(curPage.value);
            result(res)
                .success(({ data: { list: refList, total: resTotal } }) => {
                    list.value = refList;
                    total.value = resTotal;
                });
        };
        onMounted(() => {
            getUser();
        })
        return {
            list,
            total,
            curPage,
            columns,
            formatTimestamp
        }
    },
});

二、删除接口实现

三、删除接口前端联调

const remove = async({ _id }) => {
            const res = await user.remove(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg);
                    getUser();
                })
        }

四、添加用户前端弹框实现

<template>
    <div>
        <a-card>
            <h2>用户管理</h2>
            <a-divider></a-divider>
            <a-button @click="showAddModal =true">添加用户</a-button>
            <a-divider></a-divider>
            <div>
                <a-table 
                 bordered 
                :pagination="false"
                :columns ="columns"
                :data-source ="list"
                >
                <template #createdAt="{record}">
                    {{ formatTimestamp(record.meta.createdAt) }}
                </template>
                <template #actions="{record}">
                <a href="javascript;">重置密码</a>
               &nbsp;
                <a href="javascript;" @click="remove(record)">删除</a>
                </template>
            </a-table>
            </div>
            <space-between1 style="margin-top:24px;">
                <a-pagination></a-pagination>
            </space-between1>
        </a-card>
        <add-one v-model:show="showAddModal"
         />
    </div>
</template>
<script src="./index.js"></script>
<style lang="scss" scoped>
     @import './index.scss';
</style>
import { defineComponent, ref, onMounted } from 'vue';
import { user } from '@/service';
import { result, formatTimestamp } from '@/helpers/utils'
import { message } from 'ant-design-vue'
import AddOne from './AddOne/index.vue'
const columns = [{
    title: '账户',
    dataIndex: 'account'
}, {
    title: '创建日期',
    slots: { customRender: 'createdAt' }
}, {
    title: '操作',
    slots: { customRender: 'actions' }
}]
export default defineComponent({
    components: {
        AddOne,
    },
    setup() {
        const list = ref([]);
        const total = ref(0);
        const curPage = ref(1);
        const showAddModal = ref(false);
        const getUser = async() => {
            const res = await user.list(curPage.value);
            result(res)
                .success(({ data: { list: refList, total: resTotal } }) => {
                    list.value = refList;
                    total.value = resTotal;
                });
        };
        onMounted(() => {
            getUser();
        })
        const remove = async({ _id }) => {
            const res = await user.remove(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg);
                    getUser();
                })
        }
        return {
            list,
            total,
            curPage,
            columns,
            formatTimestamp,
            remove,
            showAddModal
        }
    },
});

五、添加用户接口实现

const Router = require('@koa/router');
const mongoose = require('mongoose');
// const { getBody } = require('../../helpers/utils')
const { v4: uuidv4 } = require('uuid');
const User = mongoose.model('User');
const router = new Router({
    prefix: '/user',
});
router.get('/list', async(ctx) => {
    let { page, size } = ctx.query;
    page = Number(page);
    size = Number(size);
    const list = await User
        .find()
        .skip((page - 1) * size)
        .limit(size)
        .exec();
    const total = await User.countDocuments().exec();
    ctx.body = {
        msg: '获取列表成功',
        data: {
            list,
            page,
            size,
            total,
        },
        code: 1,
    }
})
router.delete('/:id', async(ctx) => {
    const { id } = ctx.params;
    const delMsg = await User.deleteOne({
        _id: id,
    });
    ctx.body = {
        data: delMsg,
        code: 1,
        msg: '删除成功'
    }
})
router.post('/add', async(ctx) => {
    const { account, password = '123123' } =
    ctx.request.body;
    const user = new User({
        account,
        password,
    })
    const res = await user.save()
    ctx.body = {
        data: res,
        code: 1,
        msg: '添加成功'
    };
});
module.exports = router;

六、实现用户接口联调和分页效果实现

<template>
    <div>
        <a-card>
            <h2>用户管理</h2>
            <a-divider></a-divider>
            <a-button @click="showAddModal =true">添加用户</a-button>
            <a-divider></a-divider>
            <div>
                <a-table 
                 bordered 
                :pagination="false"
                :columns ="columns"
                :data-source ="list"
                >
                <template #createdAt="{record}">
                    {{ formatTimestamp(record.meta.createdAt) }}
                </template>
                <template #actions="{record}">
                <a href="javascript;">重置密码</a>
               &nbsp;
                <a href="javascript;" @click="remove(record)">删除</a>
                </template>
            </a-table>
            </div>
            <space-between1 style="margin-top:24px;">
                <a-pagination  
                v-model:current="curPage"
                :total="total"
                :page-size="3"
                @change="setPage"
                >
                </a-pagination>
            </space-between1>
        </a-card>
        <add-one 
        v-model:show="showAddModal"
        @getList="getUser"
         />
    </div>
</template>
<script src="./index.js"></script>
<style lang="scss" scoped>
     @import './index.scss';
</style>

七、重置密码接口实现

import { defineComponent, ref, onMounted } from 'vue';
import { user } from '@/service';
import { result, formatTimestamp } from '@/helpers/utils'
import { message } from 'ant-design-vue'
import AddOne from './AddOne/index.vue'
const columns = [{
    title: '账户',
    dataIndex: 'account'
}, {
    title: '创建日期',
    slots: { customRender: 'createdAt' }
}, {
    title: '操作',
    slots: { customRender: 'actions' }
}]
export default defineComponent({
    components: {
        AddOne,
    },
    setup() {
        const list = ref([]);
        const total = ref(0);
        const curPage = ref(1);
        const showAddModal = ref(false);
        const getUser = async() => {
            const res = await user.list(curPage.value, 3);
            result(res)
                .success(({ data: { list: refList, total: resTotal } }) => {
                    list.value = refList;
                    total.value = resTotal;
                });
        };
        onMounted(() => {
            getUser();
        })
        const remove = async({ _id }) => {
            const res = await user.remove(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg);
                    getUser();
                });
        };
        const setPage = (page) => {
            curPage.value = page;
            getUser();
        };
        const resetPassword = async({ _id }) => {
            const res = await user.resetPassword(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg)
                })
        }
        return {
            list,
            total,
            curPage,
            columns,
            formatTimestamp,
            remove,
            showAddModal,
            getUser,
            setPage,
            resetPassword
        }
    },
});

八、根据账户查找用户

import { defineComponent, ref, onMounted } from 'vue';
import { user } from '@/service';
import { result, formatTimestamp } from '@/helpers/utils'
import { message } from 'ant-design-vue'
import AddOne from './AddOne/index.vue'
const columns = [{
    title: '账户',
    dataIndex: 'account'
}, {
    title: '创建日期',
    slots: { customRender: 'createdAt' }
}, {
    title: '操作',
    slots: { customRender: 'actions' }
}]
export default defineComponent({
    components: {
        AddOne,
    },
    setup() {
        const list = ref([]);
        const total = ref(0);
        const curPage = ref(1);
        const showAddModal = ref(false);
        const keyword = ref('');
        const isSearch = ref(false);
        const getUser = async() => {
            const res = await user.list(curPage.value, 3, keyword.value);
            result(res)
                .success(({ data: { list: refList, total: resTotal } }) => {
                    list.value = refList;
                    total.value = resTotal;
                });
        };
        onMounted(() => {
            getUser();
        })
        const remove = async({ _id }) => {
            const res = await user.remove(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg);
                    getUser();
                });
        };
        const setPage = (page) => {
            curPage.value = page;
            getUser();
        };
        const resetPassword = async({ _id }) => {
            const res = await user.resetPassword(_id);
            result(res)
                .success(({ msg }) => {
                    message.success(msg)
                })
        };
        const onSearch = () => {
            getUser();
            isSearch.value = !!keyword.value;
        };
        const backAll = () => {
            isSearch.value = false;
            keyword.value = '';
            getUser();
        };
        return {
            list,
            total,
            curPage,
            columns,
            formatTimestamp,
            remove,
            showAddModal,
            getUser,
            setPage,
            resetPassword,
            keyword,
            isSearch,
            onSearch,
            backAll
        }
    },
});


相关文章
|
2月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
287 2
|
29天前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
265 137
|
5月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
745 0
|
4月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
395 1
|
4月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
240 0
|
7月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
984 4
|
5月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
6月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
794 77
|
7月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
5月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
458 17