request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. mergeConfig,
  3. dispatchRequest,
  4. jsonpRequest
  5. } from "./utils"
  6. export default class request {
  7. constructor(options) {
  8. // 请求公共地址
  9. this.baseUrl = options.baseUrl || "";
  10. // 公共文件上传请求地址
  11. this.fileUrl = options.fileUrl || "";
  12. // 超时时间
  13. this.timeout = options.timeout || 6000;
  14. // 服务器上传图片默认url
  15. this.defaultUploadUrl = options.defaultUploadUrl || "";
  16. // 默认请求头
  17. this.header = options.header || {};
  18. // 默认配置
  19. this.config = options.config || {
  20. isPrompt: true,
  21. load: true,
  22. isFactory: true,
  23. resend: 0
  24. };
  25. }
  26. // post请求
  27. post(url = '', data = {}, options = {}) {
  28. return this.request({
  29. method: "POST",
  30. data: data,
  31. url: url,
  32. ...options
  33. });
  34. }
  35. // get请求
  36. get(url = '', data = {}, options = {}) {
  37. return this.request({
  38. method: "GET",
  39. data: data,
  40. url: url,
  41. ...options
  42. });
  43. }
  44. // put请求
  45. put(url = '', data = {}, options = {}) {
  46. return this.request({
  47. method: "PUT",
  48. data: data,
  49. url: url,
  50. ...options
  51. });
  52. }
  53. // delete请求
  54. delete(url = '', data = {}, options = {}) {
  55. return this.request({
  56. method: "DELETE",
  57. data: data,
  58. url: url,
  59. ...options
  60. });
  61. }
  62. // jsonp请求(只限于H5使用)
  63. jsonp(url = '', data = {}, options = {}) {
  64. return this.request({
  65. method: "JSONP",
  66. data: data,
  67. url: url,
  68. ...options
  69. });
  70. }
  71. // 接口请求方法
  72. async request(data) {
  73. // 请求数据
  74. let requestInfo,
  75. // 是否运行过请求开始钩子
  76. runRequestStart = false;
  77. try {
  78. if (!data.url) {
  79. throw {
  80. errMsg: "【request】缺失数据url",
  81. statusCode: 0
  82. }
  83. }
  84. // 数据合并
  85. requestInfo = mergeConfig(this, data);
  86. // 代表之前运行到这里
  87. runRequestStart = true;
  88. // 请求前回调
  89. if (this.requestStart) {
  90. let requestStart = this.requestStart(requestInfo);
  91. if (typeof requestStart == "object") {
  92. let changekeys = ["data", "header", "isPrompt", "load", "isFactory"];
  93. changekeys.forEach(key => {
  94. requestInfo[key] = requestStart[key];
  95. });
  96. } else {
  97. throw {
  98. errMsg: "【request】请求开始拦截器未通过",
  99. statusCode: 0,
  100. data: requestInfo.data,
  101. method: requestInfo.method,
  102. header: requestInfo.header,
  103. url: requestInfo.url,
  104. }
  105. }
  106. }
  107. let requestResult = {};
  108. if (requestInfo.method == "JSONP") {
  109. requestResult = await jsonpRequest(requestInfo);
  110. } else {
  111. requestResult = await dispatchRequest(requestInfo);
  112. }
  113. // 是否用外部的数据处理方法
  114. if (requestInfo.isFactory && this.dataFactory) {
  115. // 数据处理
  116. let result = await this.dataFactory({
  117. ...requestInfo,
  118. response: requestResult
  119. });
  120. return Promise.resolve(result);
  121. } else {
  122. return Promise.resolve(requestResult);
  123. }
  124. } catch (err) {
  125. this.requestError && this.requestError(err);
  126. return Promise.reject(err);
  127. } finally {
  128. // 如果请求开始未运行到,请求结束也不运行
  129. if (runRequestStart) {
  130. this.requestEnd && this.requestEnd(requestInfo);
  131. }
  132. }
  133. }
  134. }