api.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const DEV_BASE_URL = "http://192.168.1.3:8080";
  4. const BASE_URL = DEV_BASE_URL;
  5. const getToken = () => {
  6. return common_vendor.index.getStorageSync("user_token") || null;
  7. };
  8. const request = (options) => {
  9. return new Promise((resolve, reject) => {
  10. const token = getToken();
  11. const header = options.header || {};
  12. if (token) {
  13. header["Authorization"] = `Bearer ${token}`;
  14. }
  15. common_vendor.index.request({
  16. url: `${BASE_URL}${options.url}`,
  17. method: options.method || "GET",
  18. data: options.data || {},
  19. header,
  20. success: (res) => {
  21. if (res.statusCode === 200) {
  22. resolve(res.data);
  23. } else if (res.statusCode === 401) {
  24. common_vendor.index.showToast({
  25. title: "登录已过期,请重新登录",
  26. icon: "none",
  27. duration: 2e3
  28. });
  29. common_vendor.index.removeStorageSync("user_token");
  30. common_vendor.index.removeStorageSync("user_info");
  31. setTimeout(() => {
  32. common_vendor.index.showModal({
  33. title: "登录提示",
  34. content: '登录已过期,请前往"模拟排名"或"强势池"页面重新登录',
  35. confirmText: "去登录",
  36. cancelText: "取消",
  37. success: (modalRes) => {
  38. if (modalRes.confirm) {
  39. common_vendor.index.switchTab({
  40. url: "/pages/rank/rank"
  41. });
  42. }
  43. }
  44. });
  45. }, 2e3);
  46. reject(new Error("未授权"));
  47. } else {
  48. reject(new Error(res.data.message || "服务暂不可用"));
  49. }
  50. },
  51. fail: (err) => {
  52. reject(new Error("网络异常"));
  53. }
  54. });
  55. });
  56. };
  57. const wxLogin = (params) => {
  58. return request({
  59. url: "/v1/auth/wxLogin",
  60. method: "POST",
  61. header: {
  62. "content-type": "application/json"
  63. },
  64. data: params
  65. });
  66. };
  67. const updateUserProfile = (data) => {
  68. return request({
  69. url: "/v1/user/profile",
  70. method: "PUT",
  71. header: {
  72. "content-type": "application/json"
  73. },
  74. data
  75. });
  76. };
  77. const getSuggestions = (keyword) => {
  78. return request({
  79. url: "/v1/stock/suggestion",
  80. method: "GET",
  81. data: { keyword }
  82. });
  83. };
  84. const searchStocks = (keyword) => {
  85. return request({
  86. url: "/v1/stock/search",
  87. method: "POST",
  88. header: {
  89. "content-type": "application/json"
  90. },
  91. data: { keyword }
  92. });
  93. };
  94. const getUserPortfolio = () => {
  95. return request({
  96. url: "/v1/user/portfolio",
  97. method: "GET"
  98. });
  99. };
  100. const getLeaderboard = () => {
  101. return request({
  102. url: "/v1/rank/leaderboard",
  103. method: "GET"
  104. });
  105. };
  106. exports.getLeaderboard = getLeaderboard;
  107. exports.getSuggestions = getSuggestions;
  108. exports.getUserPortfolio = getUserPortfolio;
  109. exports.searchStocks = searchStocks;
  110. exports.updateUserProfile = updateUserProfile;
  111. exports.wxLogin = wxLogin;