uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

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