user.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { Random } = require('mockjs')
  2. const tokens = {
  3. admin: `admin-token-${Random.guid()}-${new Date().getTime()}`,
  4. editor: `editor-token-${Random.guid()}-${new Date().getTime()}`,
  5. test: `test-token-${Random.guid()}-${new Date().getTime()}`,
  6. }
  7. const username2role = {
  8. admin: ['Admin'],
  9. editor: ['Editor'],
  10. test: ['Admin', 'Editor'],
  11. }
  12. const role2permission = {
  13. Admin: ['read:system', 'write:system', 'delete:system'],
  14. Editor: ['read:system', 'write:system'],
  15. Test: ['read:system'],
  16. }
  17. module.exports = [
  18. {
  19. url: '/publicKey',
  20. type: 'get',
  21. response() {
  22. return {
  23. code: 200,
  24. msg: 'success',
  25. data: {
  26. mockServer: true,
  27. publicKey:
  28. 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBT2vr+dhZElF73FJ6xiP181txKWUSNLPQQlid6DUJhGAOZblluafIdLmnUyKE8mMHhT3R+Ib3ssZcJku6Hn72yHYj/qPkCGFv0eFo7G+GJfDIUeDyalBN0QsuiE/XzPHJBuJDfRArOiWvH0BXOv5kpeXSXM8yTt5Na1jAYSiQ/wIDAQAB',
  29. },
  30. }
  31. },
  32. },
  33. {
  34. url: '/login',
  35. type: 'post',
  36. response(config) {
  37. const { username } = config.body
  38. const token = tokens[username]
  39. if (!token)
  40. return {
  41. code: 500,
  42. msg: '帐户或密码不正确',
  43. }
  44. return {
  45. code: 200,
  46. msg: 'success',
  47. data: { token },
  48. }
  49. },
  50. },
  51. {
  52. url: '/socialLogin',
  53. type: 'post',
  54. response(config) {
  55. const { code } = config.body
  56. if (!code)
  57. return {
  58. code: 500,
  59. msg: '未成功获取Token',
  60. }
  61. return {
  62. code: 200,
  63. msg: 'success',
  64. data: { token: tokens['admin'] },
  65. }
  66. },
  67. },
  68. {
  69. url: '/register',
  70. type: 'post',
  71. response() {
  72. return {
  73. code: 200,
  74. msg: '模拟注册成功',
  75. data: { token: tokens['editor'] },
  76. }
  77. },
  78. },
  79. {
  80. url: '/userInfo',
  81. type: 'get',
  82. response(config) {
  83. const authorization =
  84. config.headers.authorization || config.headers.Authorization
  85. if (!authorization.startsWith('Bearer '))
  86. return {
  87. code: 401,
  88. msg: '令牌无效',
  89. }
  90. const _authorization = authorization.replace('Bearer ', '')
  91. const isTrue = _authorization.includes('-token-')
  92. const username = isTrue ? _authorization.split('-token-')[0] : 'admin'
  93. const roles = username2role[username] || []
  94. const permissions = [
  95. ...new Set(roles.flatMap((role) => role2permission[role])),
  96. ]
  97. return {
  98. code: 200,
  99. msg: 'success',
  100. data: {
  101. username,
  102. roles,
  103. permissions,
  104. avatar: 'https://i.gtimg.cn/club/item/face/img/2/16022_100.gif',
  105. },
  106. }
  107. },
  108. },
  109. {
  110. url: '/logout',
  111. type: 'get',
  112. response() {
  113. return {
  114. code: 200,
  115. msg: 'success',
  116. }
  117. },
  118. },
  119. ]