request.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // 预发布地址
  2. import { apple } from "../config/config";
  3. let httpTrackLog = "https://hyperion-track-app.mokamrp.com";
  4. let httpServiceURL = "";
  5. let version = __wxConfig.envVersion;
  6. import { https } from "../config/config";
  7. switch (version) {
  8. case "develop":
  9. httpServiceURL = https.dev_apiHttps;
  10. break;
  11. case "trial":
  12. httpServiceURL = https.test_apiHttps;
  13. break;
  14. case "release": //正式版
  15. httpServiceURL = https.apiHttps;
  16. break;
  17. default: //未知,默认调用正式版
  18. httpServiceURL = https.apiHttps;
  19. break;
  20. }
  21. // 添加登录锁和请求队列
  22. let isLoggingIn = false;
  23. let loginPromise = null;
  24. let pendingRequests = [];
  25. // 执行等待队列中的请求
  26. const executePendingRequests = () => {
  27. const requests = [...pendingRequests];
  28. pendingRequests = [];
  29. return Promise.all(requests.map((req) => req()));
  30. };
  31. // 普通POST请求
  32. export const post = (url, body = {}) => {
  33. return new Promise((resolve, reject) => {
  34. wx.request({
  35. url: url,
  36. data: body,
  37. method: "POST",
  38. dataType: "json",
  39. success: (res) => {
  40. if (res.statusCode == 200 && res.data.code == 200) {
  41. resolve(res.data.data);
  42. } else {
  43. reject(res);
  44. }
  45. },
  46. fail: (res) => {
  47. console.log(url, "error--------", res);
  48. reject(res);
  49. },
  50. });
  51. });
  52. };
  53. // 普通Get请求
  54. export const get = (url) => {
  55. return new Promise((resolve, reject) => {
  56. wx.request({
  57. url: url,
  58. method: "GET",
  59. success: (res) => {
  60. if (res.statusCode == 200) {
  61. resolve(res.data);
  62. } else {
  63. reject(res);
  64. }
  65. },
  66. fail: (res) => {
  67. reject(res);
  68. },
  69. });
  70. });
  71. };
  72. /**
  73. * 打点请求
  74. */
  75. export function uploadTrackLog(url, body) {
  76. if (!body) {
  77. body = {};
  78. }
  79. return post(httpTrackLog + url, body);
  80. }
  81. export function bookPlatformPost(url, body) {
  82. if (!body) {
  83. body = {};
  84. }
  85. return post("https://book-platform-api.mokamrp.com" + url, body);
  86. }
  87. export const requestLogin = (data) => {
  88. console.log("开始登录请求,参数:", data);
  89. return new Promise((resolve, reject) => {
  90. // 检查是否已经有token
  91. const app = getApp();
  92. const existingToken = app.globalData.accessToken;
  93. if (existingToken) {
  94. console.log("已存在token,跳过登录");
  95. return resolve({ accessToken: existingToken });
  96. }
  97. wx.request({
  98. url: httpServiceURL + "/user/loginWx",
  99. data: data,
  100. method: "POST",
  101. dataType: "json",
  102. header: {},
  103. success: function (res) {
  104. if (res.data.code == 200) {
  105. console.log("登录成功,保存token");
  106. const app = getApp();
  107. wx.setStorageSync("accessToken", res.data.data.accessToken);
  108. app.globalData.accessToken = res.data.data.accessToken;
  109. resolve(res.data.data);
  110. } else {
  111. console.log("登录失败,错误信息:", res.data);
  112. wx.showToast({
  113. title: res.data.msg || res.data.error || "登录失败...",
  114. icon: "none",
  115. });
  116. reject(res.data);
  117. }
  118. },
  119. fail: function (res) {
  120. console.log("登录请求失败:", res);
  121. wx.showToast({
  122. title: "登录失败...",
  123. icon: "none",
  124. });
  125. reject(res);
  126. },
  127. });
  128. });
  129. };
  130. export const requestAll = (
  131. requestUrl,
  132. body,
  133. method = "POST",
  134. customHeaders = {},
  135. ) => {
  136. let url = httpServiceURL + requestUrl;
  137. if (method === "GET") {
  138. if (body) {
  139. url += "?";
  140. for (const k in body) {
  141. url += `&${k}=${body[k]}`;
  142. }
  143. }
  144. }
  145. // 封装请求函数
  146. const doRequest = () => {
  147. return new Promise((resolve, reject) => {
  148. const app = getApp();
  149. const token = app.globalData.accessToken || "";
  150. wx.request({
  151. url: url,
  152. data: body,
  153. method: method,
  154. dataType: "json",
  155. header: {
  156. token,
  157. },
  158. success: function (res) {
  159. if (res.data.code == 200) {
  160. if (typeof res.data.data === "number") {
  161. resolve(res.data.data);
  162. } else {
  163. resolve(res.data.data || []);
  164. }
  165. } else if (res.data.code == 20001) {
  166. console.error("令牌过期", url);
  167. reject({ code: 20001, msg: "令牌过期" });
  168. } else {
  169. reject(res.data);
  170. }
  171. },
  172. fail: function (res) {
  173. wx.showToast({
  174. title: "网络出错,请重试...",
  175. icon: "none",
  176. });
  177. reject(res);
  178. },
  179. });
  180. });
  181. };
  182. // 执行请求,如果token过期则重新登录后重试
  183. return doRequest().catch(async (error) => {
  184. if (error.code == 20001) {
  185. // 如果已经在登录中,将请求添加到队列
  186. if (isLoggingIn) {
  187. return new Promise((resolve, reject) => {
  188. pendingRequests.push(() => doRequest().then(resolve).catch(reject));
  189. });
  190. }
  191. // 如果没有在登录,开始登录流程
  192. isLoggingIn = true;
  193. try {
  194. // 使用单例登录Promise
  195. if (!loginPromise) {
  196. const app = getApp();
  197. wx.removeStorageSync("accessToken");
  198. app.globalData.accessToken = "";
  199. loginPromise = app.login();
  200. }
  201. await loginPromise;
  202. // 重置登录状态和Promise
  203. isLoggingIn = false;
  204. loginPromise = null;
  205. // 执行所有请求
  206. if (pendingRequests.length > 0) {
  207. const app = getApp();
  208. console.log(app.globalData.accessToken);
  209. pendingRequests.push(() => doRequest());
  210. return executePendingRequests();
  211. }
  212. return doRequest();
  213. } catch (loginError) {
  214. // 登录失败,重置状态
  215. isLoggingIn = false;
  216. loginPromise = null;
  217. pendingRequests = [];
  218. console.error("重新登录失败:", loginError);
  219. throw loginError;
  220. }
  221. }
  222. throw error;
  223. });
  224. };
  225. /**
  226. * 添加公共参数
  227. */
  228. export function postWithCommonParams(url, body, method = "POST") {
  229. const app = getApp();
  230. if (!body) {
  231. body = {};
  232. }
  233. if (app.globalData.uuid) {
  234. body.uuid = app.globalData.uuid;
  235. } else {
  236. body.uuid = "fuck";
  237. }
  238. body.gh_id = apple.ghid;
  239. // 判断是否为完整URL
  240. const isFullUrl = url.startsWith("http://") || url.startsWith("https://");
  241. const fullUrl = isFullUrl ? url : https.apiHttps + url;
  242. if (method === "POST") {
  243. return post(fullUrl, body);
  244. } else {
  245. return get(fullUrl);
  246. }
  247. }
  248. export function feedbackPost(url, body) {
  249. if (!body) {
  250. body = {};
  251. }
  252. return post("https://moka-private-pangu.mokamrp.com" + url, body);
  253. }