utils

image.png

Response.ts

其他没变化

src/utils/Response.ts

  1. // 请求响应封装
  2. interface ResponseData {
  3. code: number;
  4. data?: any;
  5. message?: string;
  6. }
  7. class BaseResponse {
  8. public code!: number;
  9. public data: any;
  10. public message!: string;
  11. constructor({ code, data, message }: ResponseData) {
  12. this.code = code
  13. if (data) this.data = data
  14. if (message) this.message = message
  15. }
  16. }
  17. export class SuccessResponse extends BaseResponse {
  18. constructor(data: any, message?: string) {
  19. super({
  20. code: 0,
  21. data,
  22. message
  23. })
  24. }
  25. }
  26. export class ErrorResponse extends BaseResponse {
  27. constructor(code: number, message: string) {
  28. super({
  29. code,
  30. message
  31. })
  32. }
  33. }
  34. interface ErrorInfo {
  35. code: number;
  36. message: string;
  37. }
  38. export const createErrorResponse = (errorInfo: ErrorInfo) => {
  39. const { code, message } = errorInfo
  40. return new ErrorResponse(code, message)
  41. }

controller types.ts

src/controller/types.ts

  1. import { RegisterModel } from '../db/models/user'
  2. export type RegisterPropsWithRoles = RegisterModel & {
  3. roleIds?: number[]
  4. }

serivces types.ts

src/services/types.ts

  1. import { UserModelProps } from '../db/models/user'
  2. import { AccessModelProps } from '../db/models/access';
  3. export interface UserWhereProps {
  4. username: string;
  5. password?: string;
  6. id?: number;
  7. }
  8. interface Role {
  9. id: number;
  10. name: string;
  11. description: string;
  12. }
  13. interface UserRole {
  14. id: number,
  15. Role: Role
  16. }
  17. // 根据用户id 获取用户以及角色信息
  18. export type UserInfo = UserModelProps & {
  19. UserRoles?: UserRole[];
  20. roles?: Role[];
  21. }
  22. // 根据角色id获取资源
  23. export type AccessRole = AccessModelProps & {
  24. RoleAccesses?: UserRole[];
  25. roles?: Role[];
  26. }

errorInfo.ts

src/constants/errorInfo.ts

  1. /**
  2. * @description 失败信息集合,包括 code 和 message
  3. * @author 拔都
  4. */
  5. export default {
  6. // 用户名已存在
  7. registerUserNameExistInfo: {
  8. code: 10001,
  9. message: '用户名已存在'
  10. },
  11. // 注册失败
  12. registerFailInfo: {
  13. code: 10002,
  14. message: '注册失败,请重试'
  15. },
  16. // 用户名不存在
  17. registerUserNameNotExistInfo: {
  18. code: 10003,
  19. message: '用户名未存在'
  20. },
  21. // 登录失败
  22. loginFailInfo: {
  23. code: 10004,
  24. message: '登录失败,用户名或密码错误'
  25. },
  26. // 未登录
  27. loginCheckFailInfo: {
  28. code: 10005,
  29. message: '您尚未登录'
  30. },
  31. // 修改密码失败
  32. changePasswordFailInfo: {
  33. code: 10006,
  34. message: '修改密码失败,请重试'
  35. },
  36. // 用户信息失败
  37. getUserInfoFailInfo: {
  38. code: 10007,
  39. message: '用户信息获取失败 token验证无效'
  40. },
  41. getUserListFailInfo: {
  42. code: 10008,
  43. message: '用户列表获取失败, 请重试'
  44. },
  45. editUserInfoFailInfo: {
  46. code: 10009,
  47. message: '用户信息修改失败, 请重试'
  48. },
  49. deleteUserInfoFailInfo: {
  50. code: 10010,
  51. message: '用户删除失败, 请重试'
  52. },
  53. updateUserRoleFailInfo: {
  54. code: 10011,
  55. message: '用户角色修改失败,请重试'
  56. },
  57. addAccessFailInfo: {
  58. code: 10012,
  59. message: '菜单添加失败'
  60. },
  61. getAccessAllFailInfo: {
  62. code: 10013,
  63. message: '获取全部菜单失败'
  64. },
  65. removeAccessFailInfo: {
  66. code: 10014,
  67. message: '删除菜单失败'
  68. },
  69. updateAccessFailInfo: {
  70. code: 10015,
  71. message: '编辑菜单失败'
  72. },
  73. addRoleFailInfo: {
  74. code: 10016,
  75. message: '添加角色失败'
  76. },
  77. addRoleNameExistInfo: {
  78. code: 10017,
  79. message: '角色已存在, 不能重复添加'
  80. },
  81. updateRoleFailInfo: {
  82. code: 10018,
  83. message: '编辑角色失败'
  84. },
  85. updateRoleNameExistInfo: {
  86. code: 10019,
  87. message: '编辑失败,已存在同名角色'
  88. },
  89. removeRoleFailInfo: {
  90. code: 10020,
  91. message: '角色删除失败'
  92. },
  93. allocRoleAccessFailInfo: {
  94. code: 10021,
  95. message: '角色分配权限失败'
  96. },
  97. getRoleAccessFailInfo: {
  98. code: 10022,
  99. message: '根据角色获取权限失败'
  100. },
  101. updateUserExistFailInfo: {
  102. code: 10023,
  103. message: '用户信息修改失败,已存在同名用户'
  104. },
  105. allocUserRoleFailInfo: {
  106. code: 10024,
  107. message: '用户角色分配失败'
  108. },
  109. accountForbiddenFailInfo: {
  110. code: 10027,
  111. message: '登录失败,账号已被禁用!'
  112. }
  113. }

目前最新源码

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