| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- "use strict";
- const common_vendor = require("../common/vendor.js");
- const utils_auth = require("./auth.js");
- const BASE_URL = "http://localhost:8080";
- const request = (options) => {
- return new Promise((resolve, reject) => {
- const token = utils_auth.getToken();
- const header = options.header || {};
- if (token) {
- header["Authorization"] = `Bearer ${token}`;
- }
- common_vendor.index.request({
- url: `${BASE_URL}${options.url}`,
- method: options.method || "GET",
- data: options.data || {},
- header,
- success: (res) => {
- if (res.statusCode === 200) {
- resolve(res.data);
- } else if (res.statusCode === 401) {
- common_vendor.index.showToast({
- title: "登录已过期,请重新登录",
- icon: "none"
- });
- common_vendor.index.removeStorageSync("user_token");
- common_vendor.index.navigateTo({
- url: "/pages/login/login"
- });
- reject(new Error("未授权"));
- } else {
- reject(new Error(res.data.message || "服务暂不可用"));
- }
- },
- fail: (err) => {
- reject(new Error("网络异常"));
- }
- });
- });
- };
- const wxLogin = (params) => {
- return request({
- url: "/v1/auth/wxLogin",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: params
- });
- };
- const phoneLogin = (phone, verifyCode) => {
- console.log("phoneLogin调用参数:", { phone, verifyCode });
- return request({
- url: "/v1/auth/phoneLogin",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: { phone, verifyCode }
- });
- };
- const sendSmsCode = (phone) => {
- return request({
- url: "/v1/auth/sendCode",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: { phone }
- });
- };
- const updateUserProfile = (data) => {
- return request({
- url: "/v1/user/profile",
- method: "PUT",
- header: {
- "content-type": "application/json"
- },
- data
- });
- };
- const getSuggestions = (keyword) => {
- return request({
- url: "/v1/stock/suggestion",
- method: "GET",
- data: { keyword }
- });
- };
- const searchStocks = (keyword) => {
- return request({
- url: "/v1/stock/search",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: { keyword }
- });
- };
- const getUserPortfolio = () => {
- return request({
- url: "/v1/user/portfolio",
- method: "GET"
- });
- };
- const getLeaderboard = () => {
- return request({
- url: "/v1/rank/leaderboard",
- method: "GET"
- });
- };
- exports.getLeaderboard = getLeaderboard;
- exports.getSuggestions = getSuggestions;
- exports.getUserPortfolio = getUserPortfolio;
- exports.phoneLogin = phoneLogin;
- exports.searchStocks = searchStocks;
- exports.sendSmsCode = sendSmsCode;
- exports.updateUserProfile = updateUserProfile;
- exports.wxLogin = wxLogin;
|