零基础快速开发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
        }
    },
});


相关文章
|
25天前
|
JavaScript API 开发者
Vue是如何进行组件化的
Vue是如何进行组件化的
|
1天前
|
JavaScript 关系型数据库 MySQL
基于VUE的校园二手交易平台系统设计与实现毕业设计论文模板
基于Vue的校园二手交易平台是一款专为校园用户设计的在线交易系统,提供简洁高效、安全可靠的二手商品买卖环境。平台利用Vue框架的响应式数据绑定和组件化特性,实现用户友好的界面,方便商品浏览、发布与管理。该系统采用Node.js、MySQL及B/S架构,确保稳定性和多功能模块设计,涵盖管理员和用户功能模块,促进物品循环使用,降低开销,提升环保意识,助力绿色校园文化建设。
|
25天前
|
JavaScript 前端开发 开发者
Vue是如何劫持响应式对象的
Vue是如何劫持响应式对象的
22 1
|
25天前
|
JavaScript 前端开发 开发者
Vue是如何进行组件化的
Vue是如何进行组件化的
|
25天前
|
存储 JavaScript 前端开发
介绍一下Vue的核心功能
介绍一下Vue的核心功能
|
27天前
|
JavaScript 前端开发 开发者
vue 数据驱动视图
总之,Vue 数据驱动视图是一种先进的理念和技术,它为前端开发带来了巨大的便利和优势。通过理解和应用这一特性,开发者能够构建出更加动态、高效、用户体验良好的前端应用。在不断发展的前端领域中,数据驱动视图将继续发挥重要作用,推动着应用界面的不断创新和进化。
|
28天前
|
JavaScript 前端开发 开发者
vue学习第一章
欢迎来到我的博客!我是瑞雨溪,一名热爱前端的大一学生,专注于JavaScript与Vue,正向全栈进发。博客分享Vue学习心得、命令式与声明式编程对比、列表展示及计数器案例等。关注我,持续更新中!🎉🎉🎉
32 1
vue学习第一章
|
28天前
|
JavaScript 前端开发 索引
vue学习第三章
欢迎来到瑞雨溪的博客,一名热爱JavaScript与Vue的大一学生。本文介绍了Vue中的v-bind指令,包括基本使用、动态绑定class及style等,希望能为你的前端学习之路提供帮助。持续关注,更多精彩内容即将呈现!🎉🎉🎉
26 1
vue学习第三章
|
28天前
|
缓存 JavaScript 前端开发
vue学习第四章
欢迎来到我的博客!我是瑞雨溪,一名热爱JavaScript与Vue的大一学生。本文介绍了Vue中计算属性的基本与复杂使用、setter/getter、与methods的对比及与侦听器的总结。如果你觉得有用,请关注我,将持续更新更多优质内容!🎉🎉🎉
35 1
vue学习第四章
|
28天前
|
JavaScript 前端开发 算法
vue学习第7章(循环)
欢迎来到瑞雨溪的博客,一名热爱JavaScript和Vue的大一学生。本文介绍了Vue中的v-for指令,包括遍历数组和对象、使用key以及数组的响应式方法等内容,并附有综合练习实例。关注我,将持续更新更多优质文章!🎉🎉🎉
24 1
vue学习第7章(循环)