【前端】智能库房综合管理系统前端项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

3 years ago
  1. const Mock = require('mockjs')
  2. const { param2Obj } = require('./utils')
  3. const user = require('./user')
  4. const role = require('./role')
  5. const article = require('./article')
  6. const search = require('./remote-search')
  7. const mocks = [
  8. ...user,
  9. ...role,
  10. ...article,
  11. ...search
  12. ]
  13. function mockXHR() {
  14. Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
  15. Mock.XHR.prototype.send = function() {
  16. if (this.custom.xhr) {
  17. this.custom.xhr.withCredentials = this.withCredentials || false
  18. if (this.responseType) {
  19. this.custom.xhr.responseType = this.responseType
  20. }
  21. }
  22. this.proxy_send(...arguments)
  23. }
  24. function XHR2ExpressReqWrap(respond) {
  25. return function(options) {
  26. let result = null
  27. if (respond instanceof Function) {
  28. const { body, type, url } = options
  29. result = respond({
  30. method: type,
  31. body: JSON.parse(body),
  32. query: param2Obj(url)
  33. })
  34. } else {
  35. result = respond
  36. }
  37. return Mock.mock(result)
  38. }
  39. }
  40. for (const i of mocks) {
  41. Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
  42. }
  43. }
  44. module.exports = {
  45. mocks,
  46. mockXHR
  47. }