request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const utils_errorHandler = require("./errorHandler.js");
  4. const common_config = require("../common/config.js");
  5. const BASE_URL = common_config.apiBaseUrl;
  6. const TIMEOUT = 6e4;
  7. const requestInterceptor = (config) => {
  8. const token = common_vendor.index.getStorageSync("token");
  9. const csrfToken = common_vendor.index.getStorageSync("csrfToken");
  10. if (!config.header) {
  11. config.header = {};
  12. }
  13. if (token) {
  14. config.header["Authorization"] = `Bearer ${token}`;
  15. }
  16. if (csrfToken) {
  17. config.header["X-CSRF-Token"] = csrfToken;
  18. }
  19. if (!config.header["Content-Type"]) {
  20. config.header["Content-Type"] = "application/json";
  21. }
  22. if (!config.url.startsWith("http")) {
  23. config.url = BASE_URL + config.url;
  24. }
  25. if (config.method === "POST" && config.data) {
  26. config.data._csrf = csrfToken;
  27. }
  28. return config;
  29. };
  30. const showError = (message, duration = 2e3) => {
  31. if (showError.lastMessage === message && Date.now() - showError.lastTime < 3e3) {
  32. return;
  33. }
  34. common_vendor.index.showToast({
  35. title: message,
  36. icon: "none",
  37. duration
  38. });
  39. showError.lastMessage = message;
  40. showError.lastTime = Date.now();
  41. };
  42. showError.lastMessage = "";
  43. showError.lastTime = 0;
  44. const responseInterceptor = (response) => {
  45. console.log("response:", response);
  46. if (response.statusCode === 200 || response.statusCode === 400) {
  47. const { data } = response;
  48. if (data.status === 999 || data.code === 999) {
  49. const error = new Error(data && (data.msg || data.message) || "请求失败");
  50. error.status = 999;
  51. error.code = 999;
  52. error.data = data;
  53. return Promise.reject(error);
  54. }
  55. if (data.status === 2e3 || data.code === 0 || data.code === 2e3) {
  56. if (data.data) {
  57. return data.data;
  58. }
  59. if (data.openid || data.userId || data.user_id) {
  60. return data;
  61. }
  62. return data;
  63. } else {
  64. const error = utils_errorHandler.errorHandler.handleBusinessError(data);
  65. return Promise.reject(error);
  66. }
  67. } else {
  68. const error = utils_errorHandler.errorHandler.handleHttpError(response.statusCode, response);
  69. return Promise.reject(error);
  70. }
  71. };
  72. const request = (options = {}) => {
  73. return new Promise((resolve, reject) => {
  74. options = requestInterceptor(options);
  75. if (!options.timeout) {
  76. options.timeout = TIMEOUT;
  77. }
  78. common_vendor.index.request({
  79. ...options,
  80. success: (res) => {
  81. try {
  82. const data = responseInterceptor(res);
  83. resolve(data);
  84. } catch (error) {
  85. utils_errorHandler.errorHandler.logError(error, "request.responseInterceptor");
  86. reject(error);
  87. }
  88. },
  89. fail: (err) => {
  90. const error = utils_errorHandler.errorHandler.handleRequestFail(err);
  91. utils_errorHandler.errorHandler.logError(error, "request.fail");
  92. reject(error);
  93. }
  94. });
  95. });
  96. };
  97. const http = {
  98. get(url, data = {}, options = {}) {
  99. return request({
  100. url,
  101. data,
  102. method: "GET",
  103. ...options
  104. });
  105. },
  106. post(url, data = {}, options = {}) {
  107. return request({
  108. url,
  109. data,
  110. method: "POST",
  111. ...options
  112. });
  113. },
  114. put(url, data = {}, options = {}) {
  115. return request({
  116. url,
  117. data,
  118. method: "PUT",
  119. ...options
  120. });
  121. },
  122. delete(url, data = {}, options = {}) {
  123. return request({
  124. url,
  125. data,
  126. method: "DELETE",
  127. ...options
  128. });
  129. },
  130. // 上传文件
  131. upload(url, filePath, name = "file", formData = {}, options = {}) {
  132. return new Promise((resolve, reject) => {
  133. const token = common_vendor.index.getStorageSync("token");
  134. const header = options.header || {};
  135. if (token) {
  136. header["Authorization"] = `Bearer ${token}`;
  137. }
  138. if (!url.startsWith("http")) {
  139. url = BASE_URL + url;
  140. }
  141. common_vendor.index.uploadFile({
  142. url,
  143. filePath,
  144. name,
  145. formData,
  146. header,
  147. success: (res) => {
  148. try {
  149. if (typeof res.data === "string") {
  150. res.data = JSON.parse(res.data);
  151. }
  152. const data = responseInterceptor({
  153. statusCode: res.statusCode,
  154. data: res.data
  155. });
  156. resolve(data);
  157. } catch (error) {
  158. reject(error);
  159. }
  160. },
  161. fail: (err) => {
  162. showError("文件上传失败");
  163. reject(err);
  164. }
  165. });
  166. });
  167. }
  168. };
  169. exports.http = http;