api.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const ENV = "prod";
  4. const CONFIG = {
  5. dev: "http://192.168.1.171:8081",
  6. // 开发环境
  7. local: "http://10.167.44.71:8081",
  8. prod: "https://www.whzhangsheng.cn/applet-api"
  9. // 生产环境
  10. };
  11. const BASE_URL = CONFIG[ENV];
  12. const getImageUrl = (url) => {
  13. if (!url)
  14. return "";
  15. if (url.startsWith("wxfile://") || url.startsWith("http://tmp/") || url.includes("/tmp/wx")) {
  16. console.warn("[getImageUrl] 检测到微信临时路径,无法显示:", url);
  17. return "";
  18. }
  19. if (url.startsWith("http") || url.startsWith("/static/")) {
  20. return url;
  21. }
  22. return BASE_URL + url;
  23. };
  24. const getToken = () => {
  25. return common_vendor.index.getStorageSync("user_token") || null;
  26. };
  27. const request = (options) => {
  28. return new Promise((resolve, reject) => {
  29. const token = getToken();
  30. const header = options.header || {};
  31. if (token) {
  32. header["Authorization"] = `Bearer ${token}`;
  33. }
  34. common_vendor.index.request({
  35. url: `${BASE_URL}${options.url}`,
  36. method: options.method || "GET",
  37. data: options.data || {},
  38. header,
  39. success: (res) => {
  40. const newToken = res.header["New-Token"] || res.header["new-token"];
  41. if (newToken) {
  42. console.log("检测到新token,自动续期");
  43. common_vendor.index.setStorageSync("user_token", newToken);
  44. }
  45. if (res.statusCode === 200) {
  46. resolve(res.data);
  47. } else if (res.statusCode === 401) {
  48. common_vendor.index.showToast({
  49. title: "登录已过期,请重新登录",
  50. icon: "none",
  51. duration: 2e3
  52. });
  53. common_vendor.index.removeStorageSync("user_token");
  54. common_vendor.index.removeStorageSync("user_info");
  55. setTimeout(() => {
  56. common_vendor.index.showModal({
  57. title: "登录提示",
  58. content: '登录已过期,请前往"模拟排名"或"强势池"页面重新登录',
  59. confirmText: "去登录",
  60. cancelText: "取消",
  61. success: (modalRes) => {
  62. if (modalRes.confirm) {
  63. common_vendor.index.switchTab({
  64. url: "/pages/rank/rank"
  65. });
  66. }
  67. }
  68. });
  69. }, 2e3);
  70. reject(new Error("未授权"));
  71. } else {
  72. reject(new Error(res.data.message || "服务暂不可用"));
  73. }
  74. },
  75. fail: (err) => {
  76. reject(new Error("网络异常"));
  77. }
  78. });
  79. });
  80. };
  81. const wxSilentLoginApi = (params) => {
  82. return request({
  83. url: "/v1/auth/wx/silent-login",
  84. method: "POST",
  85. header: {
  86. "content-type": "application/json"
  87. },
  88. data: params
  89. });
  90. };
  91. const wxPhoneLoginApi = (params) => {
  92. return request({
  93. url: "/v1/auth/wx/phone-verify",
  94. method: "POST",
  95. header: {
  96. "content-type": "application/json"
  97. },
  98. data: params
  99. });
  100. };
  101. const wxCompleteUserInfoApi = (params) => {
  102. return request({
  103. url: "/v1/auth/wx/register",
  104. method: "POST",
  105. header: {
  106. "content-type": "application/json"
  107. },
  108. data: params
  109. });
  110. };
  111. const getUserInfoApi = () => {
  112. return request({
  113. url: "/v1/user/info",
  114. method: "GET"
  115. });
  116. };
  117. const uploadFile = {
  118. url: `${BASE_URL}/v1/file/upload`
  119. };
  120. const updateUserProfile = (data) => {
  121. return request({
  122. url: "/v1/user/profile",
  123. method: "PUT",
  124. header: {
  125. "content-type": "application/json"
  126. },
  127. data
  128. });
  129. };
  130. const getSuggestions = (keyword) => {
  131. return request({
  132. url: "/v1/stock/suggestion",
  133. method: "GET",
  134. data: { keyword }
  135. });
  136. };
  137. const searchStocks = (keyword) => {
  138. return request({
  139. url: "/v1/stock/search",
  140. method: "POST",
  141. header: {
  142. "content-type": "application/json"
  143. },
  144. data: { keyword }
  145. });
  146. };
  147. const getStockQuotes = (codes) => {
  148. return request({
  149. url: "/api/stock/fetch",
  150. method: "GET",
  151. data: { codes }
  152. });
  153. };
  154. const getIndexQuote = (code) => {
  155. return request({
  156. url: "/api/stock/index",
  157. method: "GET",
  158. data: { code }
  159. });
  160. };
  161. const getUserStocks = () => {
  162. return request({
  163. url: "/v1/user/stock/list",
  164. method: "GET"
  165. });
  166. };
  167. const addUserStock = (data) => {
  168. return request({
  169. url: "/v1/user/stock/add",
  170. method: "POST",
  171. header: {
  172. "content-type": "application/json"
  173. },
  174. data
  175. });
  176. };
  177. const deleteUserStock = (stockCode) => {
  178. return request({
  179. url: `/v1/user/stock/delete?stockCode=${encodeURIComponent(stockCode)}`,
  180. method: "DELETE"
  181. });
  182. };
  183. const getStockPoolList = (poolType) => {
  184. return request({
  185. url: "/v1/stock/pool/list",
  186. method: "GET",
  187. data: { poolType }
  188. });
  189. };
  190. const getPaymentConfig = (poolType) => {
  191. return request({
  192. url: "/v1/order/config",
  193. method: "GET",
  194. data: { poolType }
  195. });
  196. };
  197. const createOrder = (data) => {
  198. return request({
  199. url: "/v1/order/create",
  200. method: "POST",
  201. header: {
  202. "content-type": "application/json"
  203. },
  204. data
  205. });
  206. };
  207. const queryOrder = (orderNo) => {
  208. return request({
  209. url: "/v1/order/query",
  210. method: "GET",
  211. data: { orderNo }
  212. });
  213. };
  214. const getUserOrders = () => {
  215. return request({
  216. url: "/v1/order/list",
  217. method: "GET"
  218. });
  219. };
  220. const repayOrder = (orderNo) => {
  221. return request({
  222. url: `/v1/order/repay?orderNo=${encodeURIComponent(orderNo)}`,
  223. method: "POST"
  224. });
  225. };
  226. const checkSubscription = (poolType) => {
  227. return request({
  228. url: "/v1/order/check-subscription",
  229. method: "GET",
  230. data: { poolType }
  231. });
  232. };
  233. const wxPay = (payParams) => {
  234. const totalFee = payParams.total_fee || payParams.totalFee;
  235. console.log("调起支付,参数:", JSON.stringify(payParams), "totalFee:", totalFee);
  236. if (!totalFee) {
  237. console.error("缺少 total_fee 参数,payParams:", payParams);
  238. }
  239. return new Promise((resolve, reject) => {
  240. common_vendor.index.requestPayment({
  241. provider: "wxpay",
  242. timeStamp: payParams.timeStamp,
  243. nonceStr: payParams.nonceStr,
  244. package: payParams.packageValue,
  245. signType: payParams.signType,
  246. paySign: payParams.paySign,
  247. totalFee,
  248. // 开发者工具模拟支付需要
  249. success: (res) => {
  250. resolve(res);
  251. },
  252. fail: (err) => {
  253. console.log("支付失败:", err);
  254. if (err.errMsg && err.errMsg.includes("cancel")) {
  255. reject(new Error("用户取消支付"));
  256. } else {
  257. reject(new Error("支付失败:" + (err.errMsg || JSON.stringify(err))));
  258. }
  259. }
  260. });
  261. });
  262. };
  263. const getStockHistory = (params) => {
  264. return request({
  265. url: "/v1/stock/history/list",
  266. method: "GET",
  267. data: params
  268. });
  269. };
  270. const getStockHistoryStats = (params) => {
  271. return request({
  272. url: "/v1/stock/history/stats",
  273. method: "GET",
  274. data: params
  275. });
  276. };
  277. const searchStockHistory = (keyword) => {
  278. return request({
  279. url: "/v1/stock/history/search",
  280. method: "GET",
  281. data: { keyword }
  282. });
  283. };
  284. exports.BASE_URL = BASE_URL;
  285. exports.addUserStock = addUserStock;
  286. exports.checkSubscription = checkSubscription;
  287. exports.createOrder = createOrder;
  288. exports.deleteUserStock = deleteUserStock;
  289. exports.getImageUrl = getImageUrl;
  290. exports.getIndexQuote = getIndexQuote;
  291. exports.getPaymentConfig = getPaymentConfig;
  292. exports.getStockHistory = getStockHistory;
  293. exports.getStockHistoryStats = getStockHistoryStats;
  294. exports.getStockPoolList = getStockPoolList;
  295. exports.getStockQuotes = getStockQuotes;
  296. exports.getSuggestions = getSuggestions;
  297. exports.getUserInfoApi = getUserInfoApi;
  298. exports.getUserOrders = getUserOrders;
  299. exports.getUserStocks = getUserStocks;
  300. exports.queryOrder = queryOrder;
  301. exports.repayOrder = repayOrder;
  302. exports.searchStockHistory = searchStockHistory;
  303. exports.searchStocks = searchStocks;
  304. exports.updateUserProfile = updateUserProfile;
  305. exports.uploadFile = uploadFile;
  306. exports.wxCompleteUserInfoApi = wxCompleteUserInfoApi;
  307. exports.wxPay = wxPay;
  308. exports.wxPhoneLoginApi = wxPhoneLoginApi;
  309. exports.wxSilentLoginApi = wxSilentLoginApi;