vue+nodejs,后端根据角色不同返回不同的路由,前端根据不同的路径渲染动态权限导航
2条回答 默认 最新
关注【相关推荐】
- 看下这篇博客,也许你就懂了,链接:vue3上传图片,以及nodejs接口处理
- 您还可以看一下 何韬老师的Node.js进阶教程第五步:用VUE.JS学习ES6课程中的 课程介绍小节, 巩固相关知识点
- 除此之外, 这篇博客: 前端项目:基于Nodejs+vue开发实现酒店管理系统中的 四,核心代码展示 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
const { Op } = require('sequelize')//nodejs的sequelize模块 const express = require('express')//express框架 const admin = require('../crud/table/admin.js')//引入管理员信息表 const token = require('../comment/token.js')//引入token模块 const adminRouter = express.Router()//express路由 adminRouter.post('/register',(req,res) =>{//管理员注册 const { adminId, adminName, adminPassword } = req.body; admin.findOne({ where:{ adminId:adminId } }) .then(data =>{ if(data){ res.status(200).json({ msg:'该用户已经注册', success:false }) return new Promise(() =>{}) }else{ return admin.create({ adminId, adminName, adminPassword }) } }) .then(data =>{ res.status(200).json({ success:true, msg:'管理员注册成功!', admin:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'内部服务器错误!' }) }) }) adminRouter.get('/login',(req,res) =>{//登录 const { adminId, adminPassword } = req.query; let adminInfo; admin.findOne({ where:{ adminId } }) .then(data =>{ if(data){ if(data.get().adminPassword==adminPassword){ adminInfo = data.get(); return token.setToken(data.get()) }else{ res.status(200).json({ success:false, msg:'用户密码错误!' }); return new Promise(() =>{}) } }else{ res.status(200).json({ success:false, msg:'该用户还未注册!' }) return new Promise(() =>{}) } }) .then(data =>{ res.status(200).json({ success:true, admin:adminInfo, token:data, msg:'登录成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'内部服务器出错!' }) }) }) adminRouter.put('/update',(req,res) =>{//修改管理员信息 const { adminId, adminName, adminPassword, adminSex, adminAge } = req.body; admin.findOne({ where:{ adminId,adminPassword } }) .then(data =>{ if(data){ return admin.update({ adminName,adminSex,adminAge },{ where:{ adminId,adminPassword } }) }else{ res.status(200).json({ success:false, msg:'管理员账号或者密码错误!' }) return new Promise(() =>{}) } }) .then(data =>{ return admin.findOne({ where:{ adminId,adminPassword } }) }) .then(data =>{ res.status(200).json({ success:true, msg:'管理员信息修改成功!', admin:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) adminRouter.delete('/del',(req,res) =>{ const { adminId } = req.body || req.query; admin.destroy({ where:{ adminId } }) .then(data =>{ res.status(200).json({ success:true, msg:'删除成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'删除失败!' }) }) }) module.exports = adminRouter;const { Op } = require('sequelize')//nodejs的sequelize模块 const express = require('express')//express框架 const customer = require('../crud/table/customer.js')//引入用户表 const customerRouter = express.Router()//express路由 customerRouter.post('/add',(req,res) =>{//用户注册 const { customerIdCard, customerName, customerSex, customerPhoneNumber } = req.body; customer.findOne({ where:{ customerIdCard } }) .then(data =>{ if(data){ res.status(200).json({ success:false, msg:'该用户已经注册!' }) return new Promise(() =>{}) }else{ return customer.create({ customerIdCard, customerName, customerSex, customerPhoneNumber }) } }) .then(data =>{ res.status(200).json({ success:true, msg:'用户信息录入成功!', customer:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'内部服务器错误!' }) }) }) // customerRouter.get('/login',(req,res) =>{//用户登录 // const { customerIdCard, cust} // }) customerRouter.put('/update',(req,res) =>{//用户基本信息修改 const { customerIdCard, customerName, customerSex, customerPhoneNumber } = req.body; customer.findOne({ where:{ customerIdCard:{ [Op.eq]:customerIdCard } } }) .then(data =>{ if(data){ return customer.update({ customerName, customerSex, customerPhoneNumber },{ where:{ customerIdCard:{ [Op.eq]:customerIdCard } } }) }else{ res.status(200).json({ success:false, msg:'该用户还未注册!' }) return new Promise(() =>{}) } }) .then(data =>{ res.status(200).json({ success:true, msg:'用户信息修改成功!', customer:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器出错!' }) }) }) customerRouter.delete('/del',(req,res) =>{//删除用户 const { customerIdCard } = req.body; customer.destroy({ where:{ customerIdCard } }) .then(data =>{ res.status(200).json({ success:true, msg:'用户删除成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) customerRouter.put('/updatevip',(req,res) =>{//购买会员 const { customerIdCard, level } = req.body; customer.findOne({ where:{ customerIdCard } }) .then(data =>{ if(data){ return customer.update({ level },{ where:{ customerIdCard } }) }else{ res.status(200).json({ success:false, msg:'该用户未注册!' }) return new Promise(() =>{}) } }) .then(data =>{ return customer.findOne({ where:{ customerIdCard } }) }) .then(data =>{ res.status(200).json({ success:true, msg:'vip更改成功!', customer:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) customerRouter.put('/updatemoney',(req,res) =>{//修改用户总消费金额 const { customerIdCard, money} = req.body; customer.findOne({ where:{ customerIdCard } }) .then(data =>{ if(data){ let oldMoney = data.get().totalAmount; let newMoney = oldMoney + money; return customer.update({ totalAmount: newMoney },{ where:{ customerIdCard } }) }else{ res.status(200).json({ success:false, msg:'该用户为注册!' }) } }) .then(data =>{ return customer.findOne({ where:{ customerIdCard } }) }) .then(data =>{ res.status(200).json({ success:true, msg:'用户消费金额修改成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) customerRouter.get('/getAllCustomer',(req,res) =>{//查询所有顾客 customer.findAndCountAll() .then(data =>{ res.status(200).json({ success:true, msg:'顾客信息查询成功!', customerList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器出错!' }) }) }) customerRouter.get('/queryCustomer',(req,res) =>{//模糊查询顾客信息 const { queryName } = req.query; customer.findAndCountAll({ where:{ customerName:{ [Op.like]:'%'+queryName+'%' } } }) .then(data =>{ res.status(200).json({ success:true, msg:'顾客信息查询成功!', customerList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .then(err =>{ res.status(200).json({ success:false, msg:'服务器出错!' }) }) }) module.exports = customerRouter;const express = require('express')//express框架 const { Op } = require('sequelize')//nodejs的sequelize模块 const order = require('../crud/table/myorder.js')//引入订单信息表 const customer = require('../crud/table/customer.js')//引入顾客信息表 const room = require('../crud/table/room.js')//引入房间信息表 order.hasOne(customer,{ sourceKey:'customerIdCard', foreignKey:'customerIdCard' }) order.hasOne(room,{ sourceKey:'roomNumber', foreignKey:'roomNumber' }) const orderRouter = express.Router() orderRouter.post('/add',(req,res) =>{//创建订单 console.log(req.body) const { orderNumber, orderStatus,customerIdCard,roomNumber, checkInTime,checkOutTime,totalMoney,remarks } = req.body; order.findOne({ where:{ orderNumber } }) .then(data =>{ if(data){ res.status(200).json({ success:false, msg:'该订单已经存在!' }) return new Promise(() =>{}) }else{ return customer.findOne({ where:{ customerIdCard } }) } }) .then(data =>{ if(data){ return room.update({ roomStatus:'已入住' },{ where:{ roomNumber } }) }else{ res.status(200).json({ success:false, msg:'该用户还未注册!' }); return new Promise(() =>{}); } }) .then(data =>{ return order.create({ orderNumber, orderStatus,customerIdCard,roomNumber, checkInTime,checkOutTime,totalMoney,remarks }) }) .then(data =>{ res.status(200).json({ success:'true', msg:'订单创建成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'内部服务器出错!' }) }) }) orderRouter.delete('/del',(req,res) =>{//删除订单 const { orderNumber } = req.body; order.findOne({ where:{ orderNumber } }) .then(data =>{ if(data){ return order.destroy({ where:{ orderNumber } }) }else{ res.status(200).json({ success:false, msg:'该订单不存在!' }) return new Promise(() =>{}) } }) .then(data =>{ res.status(200).json({ success:true, msg:'删除成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) orderRouter.put('/update',(req,res) =>{//修改订单状态 const { orderStatus, orderNumber,totalMoney,roomNumber } = req.body; order.findOne({ where:{ orderNumber } }) .then(data =>{ if(data){ return room.update({ roomStatus:'未入住' },{ where:{ roomNumber } }) }else{ res.status(200).json({ success:false, msg:'该订单不存在!' }) return new Promise(() =>{}) } }) .then(data =>{ return order.update({ orderStatus,totalMoney },{ where:{ orderNumber } }) }) .then(data =>{ return order.findOne({ where:{ orderNumber } }) }) .then(data =>{ res.status(200).json({ success:true, msg:'订单修改成功!', order:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) orderRouter.get('/getAllOrder',(req,res) =>{//查询所有订单 const { pageSize, offset } = req.query; order.findAndCountAll({ limit:pageSize, offset:offset, include:[ { model:customer, foreignKey:'customerIdCard', attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount'] }, { model:room, foreignKey:'roomNumber', attributes:['type','remarks'] } ] }) .then(data =>{ res.status(200).json({ success:true, msg:'所有订单查询成功!', orderList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) orderRouter.get('/getAllNotPayOrder',(req,res) =>{//查询所有未支付的订单 const { pageSize, offset } = req.query; order.findAndCountAll({ limit:pageSize, offset:offset, where:{ orderStatus:{ [Op.eq]:'未支付' } }, include:[ { model:customer, foreignKey:'customerIdCard', attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount'] }, { model:room, foreignKey:'roomNumber', attributes:['type'] } ] }) .then(data =>{ res.status(200).json({ success:true, msg:'未支付订单查询成功!', orderList:data.rows.map(item =>{ return item.get() }), count:data.count, pageSize:pageSize, offset:offset }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) orderRouter.get('/getAllPayOrder',(req,res) =>{//查询所有已支付的订单 const { pageSize, offset } = req.query; order.findAndCountAll({ limit:pageSize, offset:offset, where:{ orderStatus:{ [Op.eq]:'已支付' } }, include:[ { model:customer, foreignKey:'customerIdCard', attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount'] }, { model:room, foreignKey:'roomNumber', attributes:['type'] } ] }) .then(data =>{ res.status(200).json({ success:true, msg:'未支付订单查询成功!', orderList:data.rows.map(item =>{ return item.get() }), count:data.count, pageSize:pageSize, offset:offset }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) orderRouter.get('/getStatusOrder',(req,res) =>{//查询所有该状态的订单 const { pageSize, offset, orderStatus } = req.query; order.findAndCountAll({ limit:pageSize, offset:offset, where:{ orderStatus:{ [Op.eq]:orderStatus } }, include:[ { model:customer, foreignKey:'customerIdCard', attributes:['customerName','customerSex','customerVipLevel','customerPhoneNumber','totalAmount'] }, { model:room, foreignKey:'roomNumber', attributes:['type'] } ] }) .then(data =>{ res.status(200).json({ success:true, msg:'状态订单查询成功!', orderList:data.rows.map(item =>{ return item.get() }), count:data.count, pageSize:pageSize, offset:offset }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) module.exports = orderRouter;const express = require('express')//express框架 const { Op } = require('sequelize')//nodejs的sequelize模块 const room = require('../crud/table/room.js')//引入放假信息表 const roomType = require('../crud/table/roomType.js')//引入所有房间类型表 room.hasOne(roomType,{ sourceKey:'type',foreignKey:'type'})//每个房间都有一个房间类型 const roomRouter = express.Router() roomRouter.post('/add',(req,res) =>{//添加房间 const { roomNumber, type, roomStatus, remarks } = req.body; room.findOne({ where:{ roomNumber } }) .then(data =>{ if(data){ res.status(200).json({ success:false, msg:'该房间已经存在!' }) return new Promise(() =>{}) }else{ return room.create({ roomNumber, type, roomStatus, remarks }) } }) .then(data =>{ return room.findOne({ where:{ roomNumber }, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) }) .then(data =>{ res.status(200).json({ success:true, msg:'房间添加成功!', room:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.delete('/del',(req,res) =>{ const { roomNumber } = req.body; room.findOne({ where:{ roomNumber } }) .then(data =>{ if(data){ return room.destroy({ where:{ roomNumber } }) }else{ res.status(200).json({ success:false, msg:'房间删除失败!' }) return new Promise(() =>{}) } }) .then(data =>{ res.status(200).json({ success:true, msg:'该房间删除成功!' }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.put('/update',(req,res) =>{//修改房间信息 const { roomNumber, type, roomStatus,remarks } = req.body; room.findOne({ where:{ roomNumber } }) .then(data =>{ if(data){ return room.update({ type, roomStatus,remarks },{ where:{ roomNumber } }) }else{ res.status(200).json({ success:false, msg:'该房间不存在!' }) return new Promise(() =>{}) } }) .then(data =>{ return room.findOne({ where:{ roomNumber } }) }) .then(data =>{ res.status(200).json({ success:true, msg:'房间信息修改成功!', room:data.get() }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误' }) }) }) roomRouter.get('/getAllRoom',(req,res) =>{//获取所有房间信息 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset }) .then(data =>{ let roomList = data.rows.map(item =>{ return item.get() }) res.status(200).json({ success:true, msg:'房间信息查询成功!', roomList:roomList, count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.get('/getOneRoom',(req,res) =>{//获取某个房间号的房间信息 const { roomNumber } = req.query; room.findOne({ where:{ roomNumber } }) .then(data =>{ if(data){ res.status(200).json({ success:true, msg:'房间查询成功!', room:data.get() }) }else{ res.status(200).json({ success:false, msg:'未查询到该房间信息' }) } }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.get('/getNotInRoom',(req,res) =>{//获取未入住房间信息 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, where:{ roomStatus:{ [Op.eq]:'未入住' } } }) .then(data =>{ res.status(200).json({ success:true, msg:'未入住房间查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.get('/getInRoom',(req,res) =>{//查询已入住房间信息 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, where:{ roomStatus:{ [Op.eq]:'已入住' } } }) .then(data =>{ res.status(200).json({ success:true, msg:'已入住房间查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.get('/getAllRoomPrice',(req,res) =>{//查询所有的房间以及价格 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) .then(data =>{ res.status(200).json({ success:true, msg:'房间信息查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器内部错误!' }) }) }) roomRouter.get('/getAllNotINRoomPrice',(req,res) =>{//获取所有未入住的房间信息 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, where:{ roomStatus:{ [Op.eq]:'未入住' } }, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) .then(data =>{ res.status(200).json({ success:true, msg:'房间信息查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) }) roomRouter.get('/getAllINRoomPrice',(req,res) =>{//获取所有已入住的房间信息 const { pageSize, offset } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, where:{ roomStatus:{ [Op.eq]:'已入住' } }, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) .then(data =>{ res.status(200).json({ success:true, msg:'房间信息查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) }) roomRouter.get('/getAllRoomTypePrice',(req,res) =>{ const { pageSize, offset, type } = req.query; room.findAndCountAll({ where:{ type:{ [Op.eq]:type } }, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) .then(data =>{ res.status(200).json({ success:true, msg:'房型查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) .catch(err =>{ res.status(200).json({ success:false, msg:'服务器出错!' }) }) }) roomRouter.get('/getAllStatusRoom',(req,res) =>{//获取所有该状态的房间信息 const { pageSize, offset, roomStatus } = req.query; room.findAndCountAll({ limit:pageSize, offset:offset, where:{ roomStatus:{ [Op.eq]:roomStatus } }, include:[{ model:roomType, foreignKey:'type', attributes:['price','url'] }] }) .then(data =>{ res.status(200).json({ success:true, msg:'房间信息查询成功!', roomList:data.rows.map(item =>{ return item.get() }), count:data.count }) }) }) module.exports = roomRouter
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^解决 无用评论 打赏 举报