api.js 2.9 KB

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