user.js 2.5 KB

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