零基础快速开发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 前端开发
vue动态添加style样式
vue动态添加style样式
|
2天前
|
JavaScript API
vue学习(13)监视属性
vue学习(13)监视属性
11 2
|
2天前
|
JavaScript
vue 函数化组件
vue 函数化组件
|
2天前
|
JavaScript 前端开发
vue学习(15)watch和computed
vue学习(15)watch和computed
9 1
|
2天前
|
JavaScript
vue学习(14)深度监视
vue学习(14)深度监视
10 0
|
3天前
|
JavaScript
vue中watch的用法
vue中watch的用法
|
3天前
|
JavaScript 前端开发
Vue项目使用px2rem
Vue项目使用px2rem
|
10天前
|
JavaScript
vue学习(4)数据绑定
vue学习(4)数据绑定
32 10
|
10天前
|
JavaScript 前端开发
vue学习(6)
vue学习(6)
31 9
|
10天前
|
JavaScript 开发者
vue学习(5)
vue学习(5)
24 7