api.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const DEV_BASE_URL = "http://localhost:8081";
  4. const BASE_URL = DEV_BASE_URL;
  5. const getImageUrl = (url) => {
  6. if (!url)
  7. return "";
  8. if (url.startsWith("http") || url.startsWith("/static/")) {
  9. return url;
  10. }
  11. return BASE_URL + url;
  12. };
  13. const getToken = () => {
  14. return common_vendor.index.getStorageSync("user_token") || null;
  15. };
  16. const request = (options) => {
  17. return new Promise((resolve, reject) => {
  18. const token = getToken();
  19. const header = options.header || {};
  20. if (token) {
  21. header["Authorization"] = `Bearer ${token}`;
  22. }
  23. common_vendor.index.request({
  24. url: `${BASE_URL}${options.url}`,
  25. method: options.method || "GET",
  26. data: options.data || {},
  27. header,
  28. success: (res) => {
  29. const newToken = res.header["New-Token"] || res.header["new-token"];
  30. if (newToken) {
  31. console.log("检测到新token,自动续期");
  32. common_vendor.index.setStorageSync("user_token", newToken);
  33. }
  34. if (res.statusCode === 200) {
  35. resolve(res.data);
  36. } else if (res.statusCode === 401) {
  37. common_vendor.index.showToast({
  38. title: "登录已过期,请重新登录",
  39. icon: "none",
  40. duration: 2e3
  41. });
  42. common_vendor.index.removeStorageSync("user_token");
  43. common_vendor.index.removeStorageSync("user_info");
  44. setTimeout(() => {
  45. common_vendor.index.showModal({
  46. title: "登录提示",
  47. content: '登录已过期,请前往"模拟排名"或"强势池"页面重新登录',
  48. confirmText: "去登录",
  49. cancelText: "取消",
  50. success: (modalRes) => {
  51. if (modalRes.confirm) {
  52. common_vendor.index.switchTab({
  53. url: "/pages/rank/rank"
  54. });
  55. }
  56. }
  57. });
  58. }, 2e3);
  59. reject(new Error("未授权"));
  60. } else {
  61. reject(new Error(res.data.message || "服务暂不可用"));
  62. }
  63. },
  64. fail: (err) => {
  65. reject(new Error("网络异常"));
  66. }
  67. });
  68. });
  69. };
  70. const wxSilentLoginApi = (params) => {
  71. return request({
  72. url: "/v1/auth/wx/silent-login",
  73. method: "POST",
  74. header: {
  75. "content-type": "application/json"
  76. },
  77. data: params
  78. });
  79. };
  80. const wxPhoneLoginApi = (params) => {
  81. return request({
  82. url: "/v1/auth/wx/phone-verify",
  83. method: "POST",
  84. header: {
  85. "content-type": "application/json"
  86. },
  87. data: params
  88. });
  89. };
  90. const wxCompleteUserInfoApi = (params) => {
  91. return request({
  92. url: "/v1/auth/wx/register",
  93. method: "POST",
  94. header: {
  95. "content-type": "application/json"
  96. },
  97. data: params
  98. });
  99. };
  100. const getUserInfoApi = () => {
  101. return request({
  102. url: "/v1/user/info",
  103. method: "GET"
  104. });
  105. };
  106. const uploadFile = {
  107. url: `${BASE_URL}/v1/file/upload`
  108. };
  109. const updateUserProfile = (data) => {
  110. return request({
  111. url: "/v1/user/profile",
  112. method: "PUT",
  113. header: {
  114. "content-type": "application/json"
  115. },
  116. data
  117. });
  118. };
  119. const getSuggestions = (keyword) => {
  120. return request({
  121. url: "/v1/stock/suggestion",
  122. method: "GET",
  123. data: { keyword }
  124. });
  125. };
  126. const searchStocks = (keyword) => {
  127. return request({
  128. url: "/v1/stock/search",
  129. method: "POST",
  130. header: {
  131. "content-type": "application/json"
  132. },
  133. data: { keyword }
  134. });
  135. };
  136. const getStockQuotes = (codes) => {
  137. return request({
  138. url: "/api/stock/fetch",
  139. method: "GET",
  140. data: { codes }
  141. });
  142. };
  143. const getIndexQuote = (code) => {
  144. return request({
  145. url: "/api/stock/index",
  146. method: "GET",
  147. data: { code }
  148. });
  149. };
  150. const getUserStocks = () => {
  151. return request({
  152. url: "/v1/user/stock/list",
  153. method: "GET"
  154. });
  155. };
  156. const addUserStock = (data) => {
  157. return request({
  158. url: "/v1/user/stock/add",
  159. method: "POST",
  160. header: {
  161. "content-type": "application/json"
  162. },
  163. data
  164. });
  165. };
  166. const deleteUserStock = (stockCode) => {
  167. return request({
  168. url: `/v1/user/stock/delete?stockCode=${encodeURIComponent(stockCode)}`,
  169. method: "DELETE"
  170. });
  171. };
  172. const getStockPoolList = (poolType) => {
  173. return request({
  174. url: "/v1/stock/pool/list",
  175. method: "GET",
  176. data: { poolType }
  177. });
  178. };
  179. const createOrder = (data) => {
  180. return request({
  181. url: "/v1/order/create",
  182. method: "POST",
  183. header: {
  184. "content-type": "application/json"
  185. },
  186. data
  187. });
  188. };
  189. const getUserOrders = () => {
  190. return request({
  191. url: "/v1/order/list",
  192. method: "GET"
  193. });
  194. };
  195. const getUserSubscriptions = () => {
  196. return request({
  197. url: "/v1/order/subscriptions",
  198. method: "GET"
  199. });
  200. };
  201. const checkSubscription = (poolType) => {
  202. return request({
  203. url: "/v1/order/check-subscription",
  204. method: "GET",
  205. data: { poolType }
  206. });
  207. };
  208. const wxPay = (payParams) => {
  209. return new Promise((resolve, reject) => {
  210. common_vendor.index.requestPayment({
  211. provider: "wxpay",
  212. timeStamp: payParams.timeStamp,
  213. nonceStr: payParams.nonceStr,
  214. package: payParams.packageValue,
  215. signType: payParams.signType,
  216. paySign: payParams.paySign,
  217. success: (res) => {
  218. resolve(res);
  219. },
  220. fail: (err) => {
  221. if (err.errMsg.includes("cancel")) {
  222. reject(new Error("用户取消支付"));
  223. } else {
  224. reject(new Error("支付失败:" + err.errMsg));
  225. }
  226. }
  227. });
  228. });
  229. };
  230. const mockPay = (orderNo) => {
  231. return request({
  232. url: `/v1/order/mock-pay?orderNo=${encodeURIComponent(orderNo)}`,
  233. method: "POST"
  234. });
  235. };
  236. exports.BASE_URL = BASE_URL;
  237. exports.addUserStock = addUserStock;
  238. exports.checkSubscription = checkSubscription;
  239. exports.createOrder = createOrder;
  240. exports.deleteUserStock = deleteUserStock;
  241. exports.getImageUrl = getImageUrl;
  242. exports.getIndexQuote = getIndexQuote;
  243. exports.getStockPoolList = getStockPoolList;
  244. exports.getStockQuotes = getStockQuotes;
  245. exports.getSuggestions = getSuggestions;
  246. exports.getUserInfoApi = getUserInfoApi;
  247. exports.getUserOrders = getUserOrders;
  248. exports.getUserStocks = getUserStocks;
  249. exports.getUserSubscriptions = getUserSubscriptions;
  250. exports.mockPay = mockPay;
  251. exports.searchStocks = searchStocks;
  252. exports.updateUserProfile = updateUserProfile;
  253. exports.uploadFile = uploadFile;
  254. exports.wxCompleteUserInfoApi = wxCompleteUserInfoApi;
  255. exports.wxPay = wxPay;
  256. exports.wxPhoneLoginApi = wxPhoneLoginApi;
  257. exports.wxSilentLoginApi = wxSilentLoginApi;