const DEFAULT_TIMEOUT = 30000;
|
const DEVICE_IP_KEY = 'device_ip';
|
const DEVICE_PORT_KEY = 'device_port';
|
const DEVICE_ID_KEY = 'device_id';
|
const DEVICE_FINGERPRINT_KEY = 'device_fingerprint';
|
|
const normalizeIp = (ip = '') => {
|
return String(ip).trim().replace(/^https?:\/\//, '');
|
};
|
|
const normalizePort = (port = '') => {
|
return String(port).trim();
|
};
|
|
export const buildBaseUrl = ({ ip, port }) => {
|
// return 'http://deab5d36.natappfree.cc'
|
const normalizedIp = normalizeIp(ip);
|
const normalizedPort = normalizePort(port);
|
|
if (!normalizedIp || !normalizedPort) {
|
return '';
|
}
|
|
return `http://${normalizedIp}:${normalizedPort}`;
|
};
|
|
export const getRequestServerConfig = () => {
|
return {
|
ip: normalizeIp(uni.getStorageSync(DEVICE_IP_KEY) || ''),
|
port: normalizePort(uni.getStorageSync(DEVICE_PORT_KEY) || ''),
|
};
|
};
|
|
export const setRequestServerConfig = ({ ip, port }) => {
|
uni.setStorageSync(DEVICE_IP_KEY, normalizeIp(ip));
|
uni.setStorageSync(DEVICE_PORT_KEY, normalizePort(port));
|
};
|
|
export const clearRequestServerConfig = () => {
|
uni.removeStorageSync(DEVICE_IP_KEY);
|
uni.removeStorageSync(DEVICE_PORT_KEY);
|
};
|
|
export const hasRequestServerConfig = () => {
|
const { ip, port } = getRequestServerConfig();
|
return Boolean(ip && port);
|
};
|
|
export const getDeviceActivationStorage = () => {
|
return {
|
deviceId: String(uni.getStorageSync(DEVICE_ID_KEY) || '').trim(),
|
deviceFingerprint: String(uni.getStorageSync(DEVICE_FINGERPRINT_KEY) || '').trim(),
|
};
|
};
|
|
export const hasActivatedDevice = () => {
|
const { deviceId, deviceFingerprint } = getDeviceActivationStorage();
|
return Boolean(hasRequestServerConfig() && deviceId && deviceFingerprint);
|
};
|
|
const resolveRequestUrl = (url, baseURL) => {
|
if (!url) {
|
throw new Error('请求的 URL 不能为空');
|
}
|
|
if (/^https?:\/\//.test(url)) {
|
return url;
|
}
|
|
const resolvedBaseUrl = baseURL || buildBaseUrl(getRequestServerConfig());
|
if (!resolvedBaseUrl) {
|
throw new Error('未配置服务地址,请先完成设备激活');
|
}
|
|
const safeUrl = String(url);
|
return `${resolvedBaseUrl}${safeUrl.startsWith('/') ? safeUrl : `/${safeUrl}`}`;
|
};
|
|
const isBusinessSuccess = (responseData) => {
|
if (!responseData || typeof responseData !== 'object' || Array.isArray(responseData)) {
|
return true;
|
}
|
|
if (!Object.prototype.hasOwnProperty.call(responseData, 'code')) {
|
return true;
|
}
|
|
return Number(responseData.code) === 200;
|
};
|
|
export const request = ({
|
url,
|
method = 'GET',
|
data = {},
|
header = {},
|
timeout = DEFAULT_TIMEOUT,
|
baseURL = '',
|
responseType = 'text',
|
}) => {
|
return new Promise((resolve, reject) => {
|
let resolvedUrl = '';
|
|
try {
|
resolvedUrl = resolveRequestUrl(url, baseURL);
|
} catch (error) {
|
reject({
|
message: error?.message || '请求地址解析失败',
|
});
|
return;
|
}
|
|
uni.request({
|
url: resolvedUrl,
|
method,
|
data,
|
header,
|
timeout,
|
responseType,
|
success: (response) => {
|
const { statusCode, data: responseData } = response;
|
console.log('请求成功:', response);
|
// 统一按 HTTP 状态码判断请求是否成功
|
if (statusCode >= 200 && statusCode < 300) {
|
// 接口存在业务 code 时,统一按业务成功态判断
|
if (!isBusinessSuccess(responseData)) {
|
reject(responseData);
|
return;
|
}
|
|
resolve(responseData);
|
return;
|
}
|
|
reject({
|
...response,
|
message: responseData?.message || `请求失败,状态码:${statusCode}`,
|
});
|
},
|
fail: (error) => {
|
reject({
|
...error,
|
message: error?.errMsg || '网络请求失败',
|
});
|
},
|
});
|
});
|
};
|