user.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: '/login',
  10. type: 'post',
  11. response(config) {
  12. const { username } = config.body
  13. const token = tokens[username]
  14. if (!token)
  15. return {
  16. code: 500,
  17. msg: '帐户或密码不正确',
  18. }
  19. return {
  20. code: 200,
  21. msg: 'success',
  22. data: { token },
  23. }
  24. },
  25. },
  26. {
  27. url: '/register',
  28. type: 'post',
  29. response() {
  30. return {
  31. code: 200,
  32. msg: '模拟注册成功',
  33. }
  34. },
  35. },
  36. {
  37. url: '/userInfo',
  38. type: 'get',
  39. response(config) {
  40. const authorization =
  41. config.headers.authorization || config.headers.Authorization
  42. let roles = ['admin']
  43. let ability = ['READ']
  44. let username = 'admin'
  45. if (authorization.includes('admin-token')) {
  46. roles = ['admin']
  47. ability = ['READ', 'WRITE', 'DELETE']
  48. username = 'admin'
  49. }
  50. if (authorization.includes('editor-token')) {
  51. roles = ['editor']
  52. ability = ['READ', 'WRITE']
  53. username = 'editor'
  54. }
  55. if (authorization.includes('test-token')) {
  56. roles = ['admin', 'editor']
  57. ability = ['READ']
  58. username = 'test'
  59. }
  60. return {
  61. code: 200,
  62. msg: 'success',
  63. data: {
  64. roles,
  65. ability,
  66. username,
  67. avatar: 'https://i.gtimg.cn/club/item/face/img/2/16022_100.gif',
  68. },
  69. }
  70. },
  71. },
  72. {
  73. url: '/logout',
  74. type: 'get',
  75. response() {
  76. return {
  77. code: 200,
  78. msg: 'success',
  79. }
  80. },
  81. },
  82. ]