make page
This commit is contained in:
189
utils/request.js
189
utils/request.js
@@ -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();
|
||||
}
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
const SYSTEM = uni.getSystemInfoSync()
|
||||
const SYSTEM = uni.getSystemInfoSync();
|
||||
|
||||
|
||||
export const getStatusBarHeight = () => SYSTEM.statusBarHeight || 15
|
||||
export const getStatusBarHeight = () => SYSTEM.statusBarHeight || 15;
|
||||
export const getTitleBarHeight = () => {
|
||||
if(uni.getMenuButtonBoundingClientRect) {
|
||||
const { top, height } = uni.getMenuButtonBoundingClientRect()
|
||||
return height + (top - getStatusBarHeight()) * 2
|
||||
} else {
|
||||
return 40
|
||||
}
|
||||
}
|
||||
if (uni.getMenuButtonBoundingClientRect) {
|
||||
const { top, height } = uni.getMenuButtonBoundingClientRect();
|
||||
return height + (top - getStatusBarHeight()) * 2;
|
||||
} else {
|
||||
return 40;
|
||||
}
|
||||
};
|
||||
|
||||
export const getBavBarHeight = () => getStatusBarHeight() + getTitleBarHeight()
|
||||
export const getBavBarHeight = () => getStatusBarHeight() + getTitleBarHeight();
|
||||
|
||||
export const getLeftIconLeft = () => {
|
||||
// if(tt?.getMenuButtonBoundingClientRect) {
|
||||
// const { leftIcon: {left, width}} = tt.getMenuButtonBoundingClientRect
|
||||
// return left + parseInt(width) + 5
|
||||
// } else {
|
||||
// return 0
|
||||
// }
|
||||
// #ifdef MP-TOUTIAO
|
||||
const { leftIcon: {left, width}} = tt.getCustomButtonBoundingClientRect()
|
||||
return left + parseInt(width)
|
||||
// #endif
|
||||
// #ifndef MP-TOUTIAO
|
||||
return 0
|
||||
// #endif
|
||||
}
|
||||
|
||||
// if(tt?.getMenuButtonBoundingClientRect) {
|
||||
// const { leftIcon: {left, width}} = tt.getMenuButtonBoundingClientRect
|
||||
// return left + parseInt(width) + 5
|
||||
// } else {
|
||||
// return 0
|
||||
// }
|
||||
// #ifdef MP-TOUTIAO
|
||||
const {
|
||||
leftIcon: { left, width },
|
||||
} = tt.getCustomButtonBoundingClientRect();
|
||||
return left + parseInt(width);
|
||||
// #endif
|
||||
// #ifndef MP-TOUTIAO
|
||||
return 0;
|
||||
// #endif
|
||||
};
|
||||
|
||||
export function getPlatformProvider() {
|
||||
const platform = process.env.UNI_PLATFORM
|
||||
return platform ?? 'mp-weixin'
|
||||
const platform = process.env.UNI_PLATFORM;
|
||||
return platform ?? "mp-weixin";
|
||||
// const platform = uni.getSystemInfoSync().platform;
|
||||
// console.log(1111111, platform)
|
||||
// // H5 模拟器调试时使用 __wxConfig 环境变量判断
|
||||
@@ -45,3 +45,19 @@ export function getPlatformProvider() {
|
||||
|
||||
// return typeof tt !== 'undefined' ? 'toutiao' : 'weixin';
|
||||
}
|
||||
|
||||
export function getDeviceInfo() {
|
||||
const info = uni.getSystemInfoSync();
|
||||
return {
|
||||
platform: info.platform,
|
||||
brand: info.brand,
|
||||
model: info.model,
|
||||
system: info.system,
|
||||
screenWidth: info.screenWidth,
|
||||
screenHeight: info.screenHeight,
|
||||
pixelRatio: info.pixelRatio,
|
||||
language: info.language,
|
||||
version: info.version,
|
||||
SDKVersion: info.SDKVersion,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user