迁移版本
This commit is contained in:
114
api/interceptor.js
Normal file
114
api/interceptor.js
Normal file
@@ -0,0 +1,114 @@
|
||||
// 定义一个全局请求配置(可被多个拦截器修改)
|
||||
import {setCache} from "@/utils/cache";
|
||||
|
||||
export let globalConfig = {
|
||||
baseURL: 'https://wx.api.borman.top/api/',
|
||||
// baseURL: 'http://brm.wx.api.borman.top/api/',
|
||||
// baseURL: 'https://wx.boerman.top/api/',
|
||||
// baseURL: 'http://127.0.0.1:18004/api/', // 本地测试地址
|
||||
// baseURL: 'http://127.0.0.1:18006/api/', // 本地测试地址
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
// 请求拦截器队列
|
||||
const requestInterceptors = [];
|
||||
|
||||
// 添加请求拦截器
|
||||
export function addRequestInterceptor(interceptor) {
|
||||
requestInterceptors.push(interceptor);
|
||||
}
|
||||
|
||||
// 执行请求拦截器
|
||||
export async function runRequestInterceptors(config) {
|
||||
let newConfig = { ...config };
|
||||
for (const interceptor of requestInterceptors) {
|
||||
newConfig = await interceptor(newConfig);
|
||||
}
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
// 响应拦截器队列
|
||||
const responseInterceptors = [];
|
||||
|
||||
// 添加响应拦截器
|
||||
export function addResponseInterceptor(interceptor) {
|
||||
responseInterceptors.push(interceptor);
|
||||
}
|
||||
|
||||
// 执行响应拦截器
|
||||
export async function runResponseInterceptors(response) {
|
||||
let newResponse = { ...response };
|
||||
for (const interceptor of responseInterceptors) {
|
||||
newResponse = await interceptor(newResponse);
|
||||
}
|
||||
return newResponse;
|
||||
}
|
||||
|
||||
// 初始化默认拦截器(可选)
|
||||
addRequestInterceptor(config => {
|
||||
// 自动携带token示例
|
||||
const token = uni.getStorageSync('token');
|
||||
// 临时token(测试用)
|
||||
// const token = 'JG7hKhTQoANh/hyL67BzIg==';
|
||||
if (token) {
|
||||
config.header.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
addResponseInterceptor(response => {
|
||||
// 统一处理错误状态码
|
||||
if (response.statusCode !== 200) {
|
||||
uni.showToast({ title: `请求错误: ${response.data.message}`, icon: 'none' });
|
||||
return Promise.reject(response.data.message);
|
||||
}
|
||||
|
||||
if (response.data.code === 500) {
|
||||
uni.showToast({ title: response.data.message, icon: 'none' });
|
||||
return Promise.reject(response.data.message);
|
||||
}
|
||||
|
||||
if (response.data.code === 401) {
|
||||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' });
|
||||
setCache('token', null);
|
||||
setTimeout(() => {
|
||||
// 储存当前访问的路由
|
||||
// 获取当前页面栈
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length === 0) {
|
||||
console.error('未找到当前页面栈');
|
||||
return;
|
||||
}
|
||||
// 获取当前页面实例
|
||||
const currentPage = pages[pages.length - 1];
|
||||
if (!currentPage) {
|
||||
console.error('未找到当前页面实例');
|
||||
return;
|
||||
}
|
||||
// 获取当前页面的路由地址
|
||||
const currentRoute = currentPage.route;
|
||||
if (!currentRoute) {
|
||||
console.error('未找到当前页面的路由地址');
|
||||
return;
|
||||
}
|
||||
// 获取传参
|
||||
const options = currentPage.options;
|
||||
// 构建包含参数的查询字符串
|
||||
let queryString = '';
|
||||
if (Object.keys(options).length > 0) {
|
||||
queryString = '?' + Object.entries(options)
|
||||
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
||||
.join('&');
|
||||
}
|
||||
// 拼接完整的路由地址
|
||||
const fullRoute = `/${currentRoute}${queryString}`;
|
||||
// 设置缓存
|
||||
setCache('currentPage', fullRoute);
|
||||
uni.reLaunch({ url: '/pages/login/index' });
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
return response.data.result; // 根据实际接口返回结构调整
|
||||
});
|
||||
11
api/page/auth.js
Normal file
11
api/page/auth.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import {get, post} from '../request';
|
||||
|
||||
const prefix = 'auth/'
|
||||
export const loginApi = async (code, phoneCode, avatarUrl, nickName) => {
|
||||
return await post(`${prefix}login`, {
|
||||
code,
|
||||
phone_code: phoneCode,
|
||||
avatar: avatarUrl,
|
||||
nick_name: nickName
|
||||
})
|
||||
}
|
||||
30
api/page/cart.js
Normal file
30
api/page/cart.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import {get, post} from '../request';
|
||||
|
||||
const prefix = 'list/'
|
||||
|
||||
export const getCartListApi = async () => {
|
||||
return await get(`${prefix}list`)
|
||||
}
|
||||
export const getCartItemApi = async (id) => {
|
||||
return await get(`${prefix}detail`, {id})
|
||||
}
|
||||
export const createCartApi = async (params) => {
|
||||
return await post(`${prefix}create`, params)
|
||||
}
|
||||
export const deleteCartApi = async (id) => {
|
||||
return await post(`${prefix}delete`, {
|
||||
ids: [id]
|
||||
})
|
||||
}
|
||||
export const deleteCartItemApi = async (id) => {
|
||||
return await post(`${prefix}delete-item`, {
|
||||
ids: [id]
|
||||
})
|
||||
}
|
||||
export const toCartApi = async (listId, productId) => {
|
||||
return await post(`${prefix}to-cart`, {
|
||||
list_id: listId,
|
||||
product_id: productId
|
||||
})
|
||||
}
|
||||
|
||||
10
api/page/home.js
Normal file
10
api/page/home.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import {get} from '../request';
|
||||
|
||||
const prefix = 'home/'
|
||||
|
||||
export const getCarouselApi = async () => {
|
||||
return await get(`${prefix}carousel`)
|
||||
}
|
||||
export const getCategoryListApi = async () => {
|
||||
return await get(`${prefix}category-list`)
|
||||
}
|
||||
15
api/page/image.js
Normal file
15
api/page/image.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import {get, post} from '../request';
|
||||
|
||||
const prefix = 'w-oss/'
|
||||
|
||||
export const getImageApi = async (hash = '') => {
|
||||
return await get(`${prefix}getImage`, {
|
||||
hash
|
||||
})
|
||||
}
|
||||
export const uploadImageWatermarkApi = async (imageUrl = '', watermark = 'BRM') => {
|
||||
return await post(`${prefix}watermark`, {
|
||||
image_url: imageUrl,
|
||||
watermark
|
||||
})
|
||||
}
|
||||
14
api/page/product.js
Normal file
14
api/page/product.js
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
import {get} from '../request';
|
||||
|
||||
const prefix = 'product/'
|
||||
|
||||
export const getProductListApi = async (param) => {
|
||||
return await get(`${prefix}list`, param)
|
||||
}
|
||||
export const getCategoryListByPidApi = async (pid = 0) => {
|
||||
return await get(`${prefix}category-list-by-pid`, {pid})
|
||||
}
|
||||
export const getProductDetailApi = async (id = 0, p_user_id = 0) => {
|
||||
return await get(`${prefix}detail`, {id, p_user_id})
|
||||
}
|
||||
17
api/page/user.js
Normal file
17
api/page/user.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import {get, post} from '../request';
|
||||
|
||||
const prefix = 'user/'
|
||||
|
||||
export const getUserInfoApi = async () => {
|
||||
return await get(`${prefix}my-info`)
|
||||
}
|
||||
export const bandPhoneApi = async (phone) => {
|
||||
return await post(`${prefix}band-phone`, {
|
||||
phone: phone
|
||||
})
|
||||
}
|
||||
export const updateNickNameApi = async (nick_name) => {
|
||||
return await post(`${prefix}update-nick-name`, {
|
||||
nick_name
|
||||
})
|
||||
}
|
||||
70
api/request.js
Normal file
70
api/request.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
runRequestInterceptors,
|
||||
runResponseInterceptors,
|
||||
globalConfig
|
||||
} from './interceptor.js';
|
||||
// import * as uni from "@/api/request"; // 原错误导入保留供参考
|
||||
|
||||
// 基础请求方法
|
||||
export function request(options) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
// 合并全局配置
|
||||
const mergedConfig = {
|
||||
...globalConfig,
|
||||
...options,
|
||||
header: { ...globalConfig.header, ...(options.header || {}) }
|
||||
};
|
||||
|
||||
// 执行请求拦截器
|
||||
let finalConfig;
|
||||
try {
|
||||
finalConfig = await runRequestInterceptors(mergedConfig);
|
||||
} catch (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (!finalConfig.url) {
|
||||
return reject(new Error('URL is required'));
|
||||
}
|
||||
|
||||
// 发起请求(修复点:移除多余的await)
|
||||
uni.request({
|
||||
...finalConfig,
|
||||
url: finalConfig.baseURL + finalConfig.url,
|
||||
success: async (res) => {
|
||||
try {
|
||||
const handledRes = await runResponseInterceptors(res);
|
||||
// console.log(handledRes, 'ssssssssssssssss'); // 保留调试日志
|
||||
resolve(handledRes);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
});
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 封装GET方法(保持async/await结构)
|
||||
export async function get(url, params = {}, options = {}) {
|
||||
return await request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
// 封装POST方法(保持async/await结构)
|
||||
export async function post(url, data = {}, options = {}) {
|
||||
return await request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user