| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- "use strict";
- const common_vendor = require("../common/vendor.js");
- const DEV_BASE_URL = "http://192.168.1.3:8081";
- const BASE_URL = DEV_BASE_URL;
- const getToken = () => {
- return common_vendor.index.getStorageSync("user_token") || null;
- };
- const request = (options) => {
- return new Promise((resolve, reject) => {
- const token = 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) => {
- const newToken = res.header["New-Token"] || res.header["new-token"];
- if (newToken) {
- console.log("检测到新token,自动续期");
- common_vendor.index.setStorageSync("user_token", newToken);
- }
- if (res.statusCode === 200) {
- resolve(res.data);
- } else if (res.statusCode === 401) {
- common_vendor.index.showToast({
- title: "登录已过期,请重新登录",
- icon: "none",
- duration: 2e3
- });
- common_vendor.index.removeStorageSync("user_token");
- common_vendor.index.removeStorageSync("user_info");
- setTimeout(() => {
- common_vendor.index.showModal({
- title: "登录提示",
- content: '登录已过期,请前往"模拟排名"或"强势池"页面重新登录',
- confirmText: "去登录",
- cancelText: "取消",
- success: (modalRes) => {
- if (modalRes.confirm) {
- common_vendor.index.switchTab({
- url: "/pages/rank/rank"
- });
- }
- }
- });
- }, 2e3);
- reject(new Error("未授权"));
- } else {
- reject(new Error(res.data.message || "服务暂不可用"));
- }
- },
- fail: (err) => {
- reject(new Error("网络异常"));
- }
- });
- });
- };
- const wxSilentLoginApi = (params) => {
- return request({
- url: "/v1/auth/wx/silent-login",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: params
- });
- };
- const wxPhoneLoginApi = (params) => {
- return request({
- url: "/v1/auth/wx/phone-verify",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: params
- });
- };
- const wxCompleteUserInfoApi = (params) => {
- return request({
- url: "/v1/auth/wx/register",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data: params
- });
- };
- const getUserInfoApi = () => {
- return request({
- url: "/v1/user/info",
- method: "GET"
- });
- };
- const uploadFile = {
- url: `${BASE_URL}/v1/file/upload`
- };
- 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 getStockQuotes = (codes) => {
- return request({
- url: "/api/stock/fetch",
- method: "GET",
- data: { codes }
- });
- };
- const getIndexQuote = (code) => {
- return request({
- url: "/api/stock/index",
- method: "GET",
- data: { code }
- });
- };
- const getUserStocks = () => {
- return request({
- url: "/v1/user/stock/list",
- method: "GET"
- });
- };
- const addUserStock = (data) => {
- return request({
- url: "/v1/user/stock/add",
- method: "POST",
- header: {
- "content-type": "application/json"
- },
- data
- });
- };
- const deleteUserStock = (stockCode) => {
- return request({
- url: `/v1/user/stock/delete?stockCode=${encodeURIComponent(stockCode)}`,
- method: "DELETE"
- });
- };
- exports.addUserStock = addUserStock;
- exports.deleteUserStock = deleteUserStock;
- exports.getIndexQuote = getIndexQuote;
- exports.getStockQuotes = getStockQuotes;
- exports.getSuggestions = getSuggestions;
- exports.getUserInfoApi = getUserInfoApi;
- exports.getUserStocks = getUserStocks;
- exports.searchStocks = searchStocks;
- exports.updateUserProfile = updateUserProfile;
- exports.uploadFile = uploadFile;
- exports.wxCompleteUserInfoApi = wxCompleteUserInfoApi;
- exports.wxPhoneLoginApi = wxPhoneLoginApi;
- exports.wxSilentLoginApi = wxSilentLoginApi;
|