228 lines
6.3 KiB
JavaScript
228 lines
6.3 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const utils_config = require("./config.js");
|
|
const utils_login = require("./login.js");
|
|
const PLATFORM_CONFIG = {
|
|
"mp-weixin": {
|
|
storageKey: "wx_token",
|
|
authHeader: "Authorization"
|
|
},
|
|
"mp-kuaishou": {
|
|
storageKey: "ks_token",
|
|
authHeader: "Authorization"
|
|
}
|
|
};
|
|
function getPlatformConfig() {
|
|
const defaultConfig = {
|
|
storageKey: "token",
|
|
authHeader: "Authorizationn",
|
|
baseURL: utils_config.BASE_URL
|
|
};
|
|
return {
|
|
...defaultConfig,
|
|
...PLATFORM_CONFIG[utils_config.PLATFORM] || {}
|
|
};
|
|
}
|
|
const requestInterceptor = (config, encrypted) => {
|
|
if (encrypted) {
|
|
if (config.data) {
|
|
try {
|
|
const data = JSON.stringify(config.data);
|
|
const key = common_vendor.dayjs().format("YYYYMMDDHHmmss");
|
|
config.data = encode(data, key);
|
|
config.header = {
|
|
"x-encode-key": key,
|
|
"Content-Type": "text/plain; charset=utf-8"
|
|
};
|
|
} catch (error) {
|
|
console.error("请求加密失败:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
const platformConfig = getPlatformConfig();
|
|
try {
|
|
console.log("getStorageSync:", common_vendor.index.getStorageSync(platformConfig.storageKey), platformConfig.storageKey);
|
|
const token = common_vendor.index.getStorageSync(platformConfig.storageKey);
|
|
if (token) {
|
|
config.header = config.header || {};
|
|
config.header[platformConfig.authHeader] = `Bearer ${token}`;
|
|
config.header[platformConfig.authHeader] = token;
|
|
}
|
|
} catch (error) {
|
|
console.warn("获取存储token失败:", error);
|
|
}
|
|
if (!encrypted) {
|
|
config.header = {
|
|
...config.header || {},
|
|
"Content-Type": "application/json",
|
|
"X-Platform": utils_config.PLATFORM
|
|
};
|
|
}
|
|
if (!config.url.startsWith("http")) {
|
|
config.url = `${platformConfig.baseURL}${config.url}`;
|
|
}
|
|
console.log("[Request]", config.method, config.url, config);
|
|
return config;
|
|
};
|
|
const responseInterceptor = (response) => {
|
|
console.log("[Response]", response.statusCode, response);
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
if (response.data.code === 200) {
|
|
if (response.data.code === 201 && response.data.data && response.data.timestamp) {
|
|
try {
|
|
const result = decode(response.data.data, response.data.timestamp);
|
|
response.data = JSON.parse(result);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("响应解密失败:", error);
|
|
throw new Error("响应解密失败");
|
|
}
|
|
}
|
|
return response.data;
|
|
} else {
|
|
return Promise.reject({
|
|
code: response.data.code,
|
|
message: response.data.msg || "业务逻辑错误"
|
|
});
|
|
}
|
|
}
|
|
const errorMap = {
|
|
400: "请求参数错误",
|
|
401: "登录已过期,请重新登录",
|
|
403: "拒绝访问",
|
|
404: "请求地址错误",
|
|
500: "服务器内部错误"
|
|
};
|
|
const errorMessage = errorMap[response.statusCode] || `服务器错误: ${response.statusCode}`;
|
|
if (response.statusCode === 401) {
|
|
try {
|
|
const platformConfig = getPlatformConfig();
|
|
common_vendor.index.removeStorageSync(platformConfig.storageKey);
|
|
common_vendor.index.showToast({
|
|
title: "登录已过期,请重新登录",
|
|
icon: "none",
|
|
complete: () => {
|
|
setTimeout(() => {
|
|
utils_login.initLogin();
|
|
}, 1500);
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error("401处理失败:", error);
|
|
}
|
|
}
|
|
return Promise.reject({
|
|
code: response.statusCode,
|
|
message: errorMessage
|
|
});
|
|
};
|
|
const errorHandler = (error) => {
|
|
console.error("[Error]", error);
|
|
let errorMessage = error.message || "未知错误";
|
|
if (error.errMsg && error.errMsg.indexOf("request:fail") !== -1) {
|
|
errorMessage = "网络连接失败,请检查网络设置";
|
|
}
|
|
if (error.code !== 401) {
|
|
common_vendor.index.showToast({
|
|
title: errorMessage,
|
|
icon: "none",
|
|
duration: 3e3
|
|
});
|
|
}
|
|
return Promise.reject(error);
|
|
};
|
|
const createRequest = () => {
|
|
return async (options) => {
|
|
const config = {
|
|
url: options.url,
|
|
method: options.method || "GET",
|
|
header: options.header || {},
|
|
data: options.data || {},
|
|
timeout: options.timeout || 15e3,
|
|
encrypted: options.encrypted
|
|
};
|
|
try {
|
|
const processedConfig = requestInterceptor(config, options.encrypted);
|
|
console.log("createRequest after requestInterceptor:", processedConfig);
|
|
const response = await new Promise((resolve, reject) => {
|
|
common_vendor.index.request({
|
|
...processedConfig,
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
});
|
|
return responseInterceptor(response);
|
|
} catch (error) {
|
|
return errorHandler(error);
|
|
}
|
|
};
|
|
};
|
|
const http = {
|
|
get: (url, params, config = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "GET",
|
|
data: params,
|
|
...config
|
|
});
|
|
},
|
|
post: (url, data, config = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "POST",
|
|
data,
|
|
...config
|
|
});
|
|
},
|
|
put: (url, data, config = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "PUT",
|
|
data,
|
|
...config
|
|
});
|
|
},
|
|
delete: (url, config = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "DELETE",
|
|
...config
|
|
});
|
|
},
|
|
// 下载文件方法
|
|
download: (url, config = {}) => {
|
|
return new Promise((resolve, reject) => {
|
|
const platformConfig = getPlatformConfig();
|
|
let token;
|
|
try {
|
|
token = common_vendor.index.getStorageSync(platformConfig.storageKey);
|
|
} catch (error) {
|
|
console.warn("获取存储token失败:", error);
|
|
}
|
|
const header = {};
|
|
if (token) {
|
|
header[platformConfig.authHeader] = `Bearer ${token}`;
|
|
}
|
|
let reqUrl = url;
|
|
if (!reqUrl.startsWith("http")) {
|
|
reqUrl = `${platformConfig.baseURL}${url}`;
|
|
}
|
|
common_vendor.index.downloadFile({
|
|
url: reqUrl,
|
|
header,
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
resolve(res);
|
|
} else {
|
|
reject(new Error(`下载失败,状态码: ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: reject
|
|
});
|
|
});
|
|
}
|
|
};
|
|
common_vendor.index.$http = http;
|
|
exports.http = http;
|