request.js 4.3 KB

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