request.js 3.1 KB

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