request.js 2.7 KB

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