make page

This commit is contained in:
zzc
2026-01-15 08:43:10 +08:00
parent f34899eb81
commit 3d2d20dd5f
21 changed files with 4315 additions and 737 deletions

View File

@@ -1,55 +1,138 @@
// const BASE_URL = 'https://apis.lihailezzc.com'
const BASE_URL = 'http://127.0.0.1:3999'
export const request = (config = {}) => {
const {
url,
method="GET",
header={},
data={}
} = config
// 默认 header
const defaultHeader = {
'x-app-id': '68774dc2d7a1efe42086078a',
};
// const BASE_URL = 'http://127.0.0.1:3999'
const BASE_URL = "http://192.168.1.3:3999";
import { useUserStore } from "@/stores/user";
// 合并 header用户传入的覆盖默认的
const finalHeader = {
...defaultHeader,
...header
};
return new Promise((reslove, reject) => {
uni.request({
url: BASE_URL + url,
method,
header: finalHeader,
data,
success: res => {
if(res.statusCode === 200 || res.statusCode === 201) {
if(res?.data?.code === 200) {
reslove(res.data.data)
} else {
uni.showModal({
title: "错误提示",
content: res.data.msg || '',
showCancel: false
})
reject(res.data)
}
} else {
uni.showModal({
title: "错误提示",
content: res.data.errMsg,
showCancel: false
})
reject(res.data)
}
},
fail: err => {
reject(err)
}
})
})
}
// 环境区分
// 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();
}