140 lines
3.5 KiB
JavaScript
140 lines
3.5 KiB
JavaScript
const BASE_URL = "https://api.ai-meng.com";
|
||
// const BASE_URL = 'http://127.0.0.1:3999'
|
||
// const BASE_URL = "http://192.168.1.3:3999";
|
||
// const BASE_URL = "http://192.168.31.253:3999";
|
||
import { useUserStore } from "@/stores/user";
|
||
|
||
// 环境区分
|
||
// const BASE_URL =
|
||
// process.env.NODE_ENV === 'production'
|
||
// ? 'https://apis.lihailezzc.com'
|
||
// : 'http://127.0.0.1:3999'
|
||
// 192.168.2.186
|
||
// 192.168.31.253
|
||
// const BASE_URL = 'https://apis.lihailezzc.com'
|
||
|
||
let isLoadingCount = 0; // 计数多次请求loading
|
||
|
||
function showLoadingFlag(loadingText) {
|
||
if (isLoadingCount === 0) {
|
||
uni.showLoading({ title: loadingText });
|
||
}
|
||
isLoadingCount++;
|
||
}
|
||
|
||
function hideLoading() {
|
||
isLoadingCount--;
|
||
if (isLoadingCount <= 0) {
|
||
isLoadingCount = 0;
|
||
uni.hideLoading();
|
||
}
|
||
}
|
||
|
||
function getHeaders() {
|
||
const headers = {
|
||
"x-app-id": "69665538a49b8ae3be50fe5d",
|
||
};
|
||
const userStore = useUserStore();
|
||
if (userStore.token) {
|
||
headers["Authorization"] = userStore.token;
|
||
}
|
||
return headers;
|
||
}
|
||
|
||
function handleError(err, showError = true) {
|
||
if (!showError) return;
|
||
let msg = "网络异常,请稍后重试";
|
||
if (typeof err === "string") {
|
||
msg = err;
|
||
} else if (err?.msg) {
|
||
msg = err.msg;
|
||
} else if (err?.message) {
|
||
msg = err.message;
|
||
}
|
||
uni.showModal({
|
||
title: "错误提示",
|
||
content: msg,
|
||
showCancel: false,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 请求函数,支持自动加载状态、token、错误弹窗、重试等
|
||
* @param {Object} config 请求配置
|
||
* @param {string} config.url 请求路径(必填)
|
||
* @param {string} [config.method='GET'] 请求方法
|
||
* @param {Object} [config.data={}] 请求参数
|
||
* @param {Object} [config.header={}] 额外 headers
|
||
* @param {boolean} [config.showLoading=true] 是否自动显示加载提示
|
||
* @param {boolean} [config.showError=true] 是否自动弹错误提示
|
||
* @param {number} [config.timeout=10000] 请求超时,单位毫秒
|
||
* @param {number} [config.retry=1] 失败后自动重试次数
|
||
* @returns {Promise<any>} 返回接口 data
|
||
*/
|
||
export function request(config) {
|
||
if (!config || !config.url) {
|
||
return Promise.reject(new Error("请求缺少 url 参数"));
|
||
}
|
||
|
||
const {
|
||
url,
|
||
method = "GET",
|
||
data = {},
|
||
header = {},
|
||
showLoading = false,
|
||
showError = true,
|
||
retry = 1,
|
||
loadingText = "加载中",
|
||
} = config;
|
||
|
||
const finalHeader = { ...getHeaders(), ...header };
|
||
const upperMethod = method.toUpperCase();
|
||
|
||
let attempt = 0;
|
||
|
||
const doRequest = () =>
|
||
new Promise((resolve, reject) => {
|
||
if (showLoading) showLoadingFlag(loadingText);
|
||
|
||
uni.request({
|
||
url: BASE_URL + url,
|
||
method: upperMethod,
|
||
data,
|
||
header: finalHeader,
|
||
success: (res) => {
|
||
if (showLoading) hideLoading();
|
||
const { statusCode, data: body } = res;
|
||
if (
|
||
(statusCode === 200 || statusCode === 201) &&
|
||
body?.code === 200
|
||
) {
|
||
resolve(body.data);
|
||
} else {
|
||
reject({
|
||
type: "business",
|
||
msg: body?.msg || "服务器错误",
|
||
data: body,
|
||
});
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
if (showLoading) hideLoading();
|
||
reject({ type: "network", msg: "网络请求失败", error: err });
|
||
},
|
||
});
|
||
});
|
||
|
||
// 自动重试
|
||
const tryRequest = () =>
|
||
doRequest().catch((err) => {
|
||
if (attempt < retry) {
|
||
attempt++;
|
||
return tryRequest();
|
||
}
|
||
if (showError) handleError(err.msg || err);
|
||
return Promise.reject(err);
|
||
});
|
||
|
||
return tryRequest();
|
||
}
|