request.js 2.8 KB

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