image.png
image.png
image.png
菜单权限分配设计到 菜单表和角色表关联
image.png
image.png

3-1 角色路由表

src/routes/roles.ts

  1. import Router from '@koa/router'
  2. import {
  3. addRoleController,
  4. getAllRoleController,
  5. updateRoleController,
  6. removeRoleController
  7. } from '../controller/roles'
  8. const router = new Router({
  9. prefix: '/api/role'
  10. })
  11. /**
  12. * 添加角色
  13. * post /api/role
  14. */
  15. router.post('/', async ctx => {
  16. ctx.body = await addRoleController(ctx.request.body)
  17. })
  18. /**
  19. * 获取全部角色
  20. * get /api/role
  21. */
  22. router.get('/', async ctx => {
  23. const { pageNum = 0, pageSize = 10 } = ctx.request.query
  24. ctx.body = await getAllRoleController({
  25. offset: Number(pageNum),
  26. limit: Number(pageSize)
  27. })
  28. })
  29. /**
  30. * 编辑角色
  31. * put /api/role/:id
  32. */
  33. router.put('/:id', async ctx => {
  34. const { id } = ctx.params
  35. ctx.body = await updateRoleController(Number(id), ctx.request.body)
  36. })
  37. /**
  38. * 删除角色
  39. * delete /api/role/:id
  40. */
  41. router.delete('/:id', async ctx => {
  42. const { id } = ctx.params
  43. ctx.body = await removeRoleController(Number(id))
  44. })
  45. export default router

3-2 角色controller

src/controller/roles.ts

  1. import { RoleModelProps } from '../db/models/roles'
  2. import { createRole, getRole, getAllRoleService, updateRoleById, removeRoleById } from '../services/roles'
  3. import { createErrorResponse, SuccessResponse } from '../utils/Response'
  4. import errorInfo from '../constants/errorInfo'
  5. const {
  6. addAccessFailInfo,
  7. addRoleNameExistInfo,
  8. updateRoleFailInfo,
  9. updateRoleNameExistInfo,
  10. removeRoleFailInfo
  11. } = errorInfo
  12. // 添加菜单
  13. export const addRoleController = async (params: RoleModelProps) => {
  14. const result = await getRole(params.name)
  15. if (result) {
  16. return createErrorResponse(addRoleNameExistInfo)
  17. }
  18. if (params) {
  19. try {
  20. const result = await createRole({
  21. ...params
  22. })
  23. return new SuccessResponse(result)
  24. } catch (error) {
  25. console.error(error.message)
  26. return createErrorResponse(addAccessFailInfo)
  27. }
  28. }
  29. }
  30. // 获取全部菜单
  31. interface RoleListParams {
  32. offset: number;
  33. limit: number;
  34. }
  35. export const getAllRoleController = async ({ offset, limit }: RoleListParams) => {
  36. try {
  37. const result = await getAllRoleService(offset, limit)
  38. return new SuccessResponse(result)
  39. } catch (error) {
  40. console.error(error.message)
  41. return createErrorResponse(addAccessFailInfo)
  42. }
  43. }
  44. // 编辑角色
  45. export const updateRoleController = async (id: number, data: RoleModelProps) => {
  46. const result = await getRole(data.name || '')
  47. if (result && result.id !== id) {
  48. return createErrorResponse(updateRoleNameExistInfo)
  49. }
  50. try {
  51. await updateRoleById(id, data)
  52. return new SuccessResponse(null, '角色编辑成功!')
  53. } catch (error) {
  54. console.error(error.message)
  55. return createErrorResponse(updateRoleFailInfo)
  56. }
  57. }
  58. // 删除角色
  59. export const removeRoleController = async (id: number) => {
  60. try {
  61. await removeRoleById(id)
  62. return new SuccessResponse(null, '删除成功!')
  63. } catch (error) {
  64. console.error(error.message)
  65. return createErrorResponse(removeRoleFailInfo)
  66. }
  67. }

3-3 角色service

src/services/roles.ts

  1. import RoleModel, { RoleModelProps } from '../db/models/roles'
  2. // 创建菜单资源
  3. export const createRole = async (params: RoleModelProps) => {
  4. const result = await RoleModel.create({
  5. ...params
  6. })
  7. return result.toJSON()
  8. }
  9. // 根据角色名称获取角色
  10. export const getRole = async (name: string) => {
  11. const result = await RoleModel.findOne({
  12. where: {
  13. name
  14. }
  15. })
  16. if (result == null) return null
  17. return result.toJSON() as RoleModelProps
  18. }
  19. // 获取全部角色
  20. export const getAllRoleService = async (offset = 0, limit = 10) => {
  21. const { count, rows } = await RoleModel.findAndCountAll({
  22. limit,
  23. offset: limit * offset
  24. })
  25. return {
  26. roles: rows,
  27. count
  28. }
  29. }
  30. // 编辑角色
  31. export const updateRoleById = async (id: number, data: RoleModelProps) => {
  32. const { name, description, is_default } = data
  33. const result = await RoleModel.update({
  34. name,
  35. description,
  36. is_default
  37. }, {
  38. where: {
  39. id
  40. }
  41. })
  42. return result
  43. }
  44. // 删除角色
  45. export const removeRoleById = async (id: number) => {
  46. const result = await RoleModel.destroy({
  47. where: {
  48. id
  49. }
  50. })
  51. return result
  52. }

3-4 角色model

src/db/models/roles.ts

  1. import {
  2. Model,
  3. DataTypes,
  4. Optional
  5. } from 'sequelize'
  6. import seq from '../seq'
  7. export interface RoleModelProps {
  8. id: number;
  9. name: string;
  10. description: string;
  11. is_default: number;
  12. }
  13. interface RoleCreationAttributes extends Optional<RoleModelProps, "id" | "is_default"> {}
  14. interface RoleInstance
  15. extends Model<RoleModelProps, RoleCreationAttributes>,
  16. RoleModelProps {}
  17. const Role = seq.define<RoleInstance>('Role', {
  18. id: {
  19. primaryKey: true,
  20. type: DataTypes.INTEGER.UNSIGNED,
  21. autoIncrement: true
  22. },
  23. name: {
  24. type: DataTypes.STRING,
  25. allowNull: false,
  26. // unique: true,
  27. comment: '角色名称 唯一'
  28. },
  29. description: {
  30. type: DataTypes.TEXT,
  31. comment: '说明描述'
  32. },
  33. is_default: {
  34. type: DataTypes.BOOLEAN,
  35. defaultValue: 0,
  36. comment: '默认角色 1是 0不是'
  37. }
  38. })
  39. export default Role

导入到models/index
db/models/index.ts

  1. import UserModel from './user'
  2. import AccessModel from './access'
  3. import RolesModel from './roles'
  4. export {
  5. UserModel,
  6. AccessModel,
  7. RolesModel,
  8. }

同步model

  1. npm run db

目前最新参考源码

https://gitee.com/brolly/vue3-admin-server