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

简介: 户管理页面前端结构部分

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

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

2345_image_file_copy_568.jpg

<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
        }
    },
});

2345_image_file_copy_569.jpg

二、删除接口实现

2345_image_file_copy_570.jpg

三、删除接口前端联调

2345_image_file_copy_571.jpg

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

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

2345_image_file_copy_572.jpg

<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;

2345_image_file_copy_573.jpg

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

2345_image_file_copy_574.jpg

<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>

2345_image_file_copy_575.jpg

七、重置密码接口实现

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
        }
    },
});

2345_image_file_copy_576.jpg

2345_image_file_copy_577.jpg

2345_image_file_copy_578.jpg

八、根据账户查找用户

2345_image_file_copy_579.jpg

2345_image_file_copy_580.jpg

2345_image_file_copy_581.jpg

2345_image_file_copy_582.jpg

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
        }
    },
});


相关文章
|
3天前
|
移动开发 JavaScript API
Vue Router 核心原理
Vue Router 是 Vue.js 的官方路由管理器,用于实现单页面应用(SPA)的路由功能。其核心原理包括路由配置、监听浏览器事件和组件渲染等。通过定义路径与组件的映射关系,Vue Router 将用户访问的路径与对应的组件关联,支持哈希和历史模式监听 URL 变化,确保页面导航时正确渲染组件。
|
7天前
|
监控 JavaScript 前端开发
ry-vue-flowable-xg:震撼来袭!这款基于 Vue 和 Flowable 的企业级工程项目管理项目,你绝不能错过
基于 Vue 和 Flowable 的企业级工程项目管理平台,免费开源且高度定制化。它覆盖投标管理、进度控制、财务核算等全流程需求,提供流程设计、部署、监控和任务管理等功能,适用于企业办公、生产制造、金融服务等多个场景,助力企业提升效率与竞争力。
57 12
|
3天前
|
JavaScript 前端开发 开发者
Vue中的class和style绑定
在 Vue 中,class 和 style 绑定是基于数据驱动视图的强大功能。通过 class 绑定,可以动态更新元素的 class 属性,支持对象和数组语法,适用于普通元素和组件。style 绑定则允许以对象或数组形式动态设置内联样式,Vue 会根据数据变化自动更新 DOM。
|
3天前
|
JavaScript 前端开发 数据安全/隐私保护
Vue Router 简介
Vue Router 是 Vue.js 官方的路由管理库,用于构建单页面应用(SPA)。它将不同页面映射到对应组件,支持嵌套路由、路由参数和导航守卫等功能,简化复杂前端应用的开发。主要特性包括路由映射、嵌套路由、路由参数、导航守卫和路由懒加载,提升性能和开发效率。安装命令:`npm install vue-router`。
|
24天前
|
JavaScript 安全 API
iframe嵌入页面实现免登录思路(以vue为例)
通过上述步骤,可以在Vue.js项目中通过 `iframe`实现不同应用间的免登录功能。利用Token传递和消息传递机制,可以确保安全、高效地在主应用和子应用间共享登录状态。这种方法在实际项目中具有广泛的应用前景,能够显著提升用户体验。
53 8
|
存储 前端开发 JavaScript
为什么我不再用Vue,改用React?
当我走进现代前端开发行业的时候,我做了一个每位开发人员都要做的决策:选择一个合适的框架。当时正逢 jQuery 被淘汰,前端开发者们不再用它编写难看的、非结构化的老式 JavaScript 程序了。
|
2月前
|
JavaScript
vue使用iconfont图标
vue使用iconfont图标
145 1
|
25天前
|
存储 设计模式 JavaScript
Vue 组件化开发:构建高质量应用的核心
本文深入探讨了 Vue.js 组件化开发的核心概念与最佳实践。
70 1
|
3月前
|
JavaScript 前端开发 开发者
vue 数据驱动视图
总之,Vue 数据驱动视图是一种先进的理念和技术,它为前端开发带来了巨大的便利和优势。通过理解和应用这一特性,开发者能够构建出更加动态、高效、用户体验良好的前端应用。在不断发展的前端领域中,数据驱动视图将继续发挥重要作用,推动着应用界面的不断创新和进化。
109 58
|
2月前
|
JavaScript 关系型数据库 MySQL
基于VUE的校园二手交易平台系统设计与实现毕业设计论文模板
基于Vue的校园二手交易平台是一款专为校园用户设计的在线交易系统,提供简洁高效、安全可靠的二手商品买卖环境。平台利用Vue框架的响应式数据绑定和组件化特性,实现用户友好的界面,方便商品浏览、发布与管理。该系统采用Node.js、MySQL及B/S架构,确保稳定性和多功能模块设计,涵盖管理员和用户功能模块,促进物品循环使用,降低开销,提升环保意识,助力绿色校园文化建设。

热门文章

最新文章