request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if (response.statusCode === 200) {
  46. const { data } = response;
  47. if (data.status === 999 || data.code === 999) {
  48. const error = new Error(data.message || "微信登录失败: code无效");
  49. error.status = 999;
  50. return Promise.reject(error);
  51. }
  52. if (data.status === 2e3 || data.code === 0 || data.code === 2e3) {
  53. if (data.data) {
  54. return data.data;
  55. }
  56. if (data.openid || data.userId || data.user_id) {
  57. return data;
  58. }
  59. return data;
  60. } else {
  61. const error = utils_errorHandler.errorHandler.handleBusinessError(data);
  62. return Promise.reject(error);
  63. }
  64. } else {
  65. const error = utils_errorHandler.errorHandler.handleHttpError(response.statusCode, response);
  66. return Promise.reject(error);
  67. }
  68. };
  69. const request = (options = {}) => {
  70. return new Promise((resolve, reject) => {
  71. options = requestInterceptor(options);
  72. if (!options.timeout) {
  73. options.timeout = TIMEOUT;
  74. }
  75. common_vendor.index.request({
  76. ...options,
  77. success: (res) => {
  78. try {
  79. const data = responseInterceptor(res);
  80. resolve(data);
  81. } catch (error) {
  82. utils_errorHandler.errorHandler.logError(error, "request.responseInterceptor");
  83. reject(error);
  84. }
  85. },
  86. fail: (err) => {
  87. const error = utils_errorHandler.errorHandler.handleRequestFail(err);
  88. utils_errorHandler.errorHandler.logError(error, "request.fail");
  89. reject(error);
  90. }
  91. });
  92. });
  93. };
  94. const http = {
  95. get(url, data = {}, options = {}) {
  96. return request({
  97. url,
  98. data,
  99. method: "GET",
  100. ...options
  101. });
  102. },
  103. post(url, data = {}, options = {}) {
  104. return request({
  105. url,
  106. data,
  107. method: "POST",
  108. ...options
  109. });
  110. },
  111. put(url, data = {}, options = {}) {
  112. return request({
  113. url,
  114. data,
  115. method: "PUT",
  116. ...options
  117. });
  118. },
  119. delete(url, data = {}, options = {}) {
  120. return request({
  121. url,
  122. data,
  123. method: "DELETE",
  124. ...options
  125. });
  126. },
  127. // 上传文件
  128. upload(url, filePath, name = "file", formData = {}, options = {}) {
  129. return new Promise((resolve, reject) => {
  130. const token = common_vendor.index.getStorageSync("token");
  131. const header = options.header || {};
  132. if (token) {
  133. header["Authorization"] = `Bearer ${token}`;
  134. }
  135. if (!url.startsWith("http")) {
  136. url = BASE_URL + url;
  137. }
  138. common_vendor.index.uploadFile({
  139. url,
  140. filePath,
  141. name,
  142. formData,
  143. header,
  144. success: (res) => {
  145. try {
  146. if (typeof res.data === "string") {
  147. res.data = JSON.parse(res.data);
  148. }
  149. const data = responseInterceptor({
  150. statusCode: res.statusCode,
  151. data: res.data
  152. });
  153. resolve(data);
  154. } catch (error) {
  155. reject(error);
  156. }
  157. },
  158. fail: (err) => {
  159. showError("文件上传失败");
  160. reject(err);
  161. }
  162. });
  163. });
  164. }
  165. };
  166. exports.http = http;