static.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * 导入所有 controller 模块,浏览器环境中自动输出controller文件夹下Mock接口,请勿修改。
  3. */
  4. import Mock from 'mockjs'
  5. import { paramObj } from '@/utils'
  6. const mocks = []
  7. const files = require.context('../../mock/controller', false, /\.js$/)
  8. files.keys().forEach((key) => {
  9. mocks.push(...files(key))
  10. })
  11. export function mockXHR() {
  12. Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
  13. Mock.XHR.prototype.send = function () {
  14. if (this.custom.xhr) {
  15. this.custom.xhr.withCredentials = this.withCredentials || false
  16. if (this.responseType) {
  17. this.custom.xhr.responseType = this.responseType
  18. }
  19. }
  20. this.proxy_send(...arguments)
  21. }
  22. function XHRHttpRequst(respond) {
  23. return function (options) {
  24. let result
  25. if (respond instanceof Function) {
  26. const { body, type, url } = options
  27. result = respond({
  28. method: type,
  29. body: JSON.parse(body),
  30. query: paramObj(url),
  31. })
  32. } else {
  33. result = respond
  34. }
  35. return Mock.mock(result)
  36. }
  37. }
  38. mocks.forEach((item) => {
  39. Mock.mock(
  40. new RegExp(item.url),
  41. item.type || 'get',
  42. XHRHttpRequst(item.response)
  43. )
  44. })
  45. }