request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // request请求封装
  2. import {
  3. BASE_URL,
  4. API_URL
  5. } from '@/env.js';
  6. export default class Request {
  7. constructor() {
  8. // 默认配置
  9. this.config = {
  10. baseUrl: '/web',
  11. header: {
  12. 'content-type': 'application/json;charset=UTF-8',
  13. "Accept": "application/json;charset=UTF-8"
  14. },
  15. url: '',
  16. data: {},
  17. params: {},
  18. method: 'GET',
  19. dataType: 'json',
  20. responseType: 'text',
  21. custom: {},
  22. sslVerify: false
  23. }
  24. /* 拦截器 */
  25. this.interceptor = {
  26. request: cb => {
  27. if (cb) {
  28. this.requestBefore = cb
  29. } else {
  30. this.requestBefore = request => request
  31. }
  32. },
  33. response: (cb) => {
  34. if (cb) {
  35. this.requestAfter = cb
  36. } else {
  37. this.requestAfter = response => response
  38. }
  39. }
  40. }
  41. }
  42. /* 判断url是否完整 */
  43. static isUrl(url) {
  44. return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
  45. }
  46. static addQueryString(params) {
  47. let paramsData = ''
  48. Object.keys(params).forEach(key => {
  49. paramsData += key + '=' + encodeURIComponent(params[key]) + '&'
  50. })
  51. return paramsData.substring(0, paramsData.length - 1)
  52. }
  53. /* 请求前 */
  54. static requestBefore(config) {
  55. return config
  56. }
  57. /* 请求后 */
  58. static requestAfter(response) {
  59. return response
  60. }
  61. /*设置全局配置*/
  62. setConfig(func) {
  63. return func(this.config)
  64. }
  65. /**
  66. * @Function
  67. * @param {Object} options - 请求配置项
  68. * @prop {String} options.url - 请求路径
  69. * @prop {Object} options.data - 请求参数
  70. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  71. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  72. * @prop {Object} [options.header = config.header] - 请求header
  73. * @prop {Object} [options.method = config.method] - 请求方法
  74. * @returns {Promise<unknown>}
  75. */
  76. async request(options = {}) {
  77. options = {
  78. ...options,
  79. ...this.config,
  80. ...this.requestBefore(options)
  81. }
  82. return new Promise((resolve, reject) => {
  83. let mergeUrl = Request.isUrl(options.url) ? options.url : (options.baseUrl + options.url)
  84. if (JSON.stringify(options.params) !== '{}') {
  85. let query = Request.addQueryString(options.params);
  86. mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}`
  87. }
  88. options.url = mergeUrl
  89. options.success = res => {
  90. resolve(this.requestAfter(res.data))
  91. }
  92. options.fail = err => {
  93. reject(this.requestAfter(err))
  94. }
  95. uni.request(options)
  96. })
  97. }
  98. get(url, options = {}) {
  99. return this.request({
  100. url,
  101. method: 'GET',
  102. ...options
  103. })
  104. }
  105. post(url, data, options = {}) {
  106. return this.request({
  107. url,
  108. data,
  109. method: 'POST',
  110. ...options
  111. })
  112. }
  113. }