179 lines
4.8 KiB
JavaScript
179 lines
4.8 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const PLATFORM_CONFIG = {
|
|
"mp-weixin": {
|
|
// 微信小程序
|
|
storageKey: "wx_token",
|
|
authHeader: "Authorization",
|
|
baseURL: "https://api.weixin.example.com"
|
|
},
|
|
"mp-kuaishou": {
|
|
// 快手小程序
|
|
storageKey: "ks_token",
|
|
authHeader: "X-KS-Token",
|
|
baseURL: "https://api.kuaishou.example.com"
|
|
}
|
|
};
|
|
function getPlatformConfig() {
|
|
const platform = "mp-weixin";
|
|
return PLATFORM_CONFIG[platform] || {
|
|
storageKey: "token",
|
|
authHeader: "Authorization",
|
|
baseURL: "https://api.example.com"
|
|
};
|
|
}
|
|
const requestInterceptor = (config2) => {
|
|
const platformConfig = getPlatformConfig();
|
|
const token = common_vendor.index.getStorageSync(platformConfig.storageKey);
|
|
if (token) {
|
|
config2.header[platformConfig.authHeader] = `Bearer ${token}`;
|
|
}
|
|
config2.header["Content-Type"] = "application/json";
|
|
config2.header["X-Platform"] = "mp-weixin";
|
|
if (!config2.url.startsWith("http")) {
|
|
config2.url = `${platformConfig.baseURL}${config2.url}`;
|
|
}
|
|
console.log("[Request]", config2.method, config2.url);
|
|
return config2;
|
|
};
|
|
const responseInterceptor = (response) => {
|
|
console.log("[Response]", response.statusCode, response.data);
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
if (response.data.code === 200) {
|
|
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) {
|
|
common_vendor.index.removeStorageSync(getPlatformConfig().storageKey);
|
|
common_vendor.index.showToast({
|
|
title: "登录已过期,请重新登录",
|
|
icon: "none",
|
|
complete: () => {
|
|
setTimeout(() => {
|
|
common_vendor.index.redirectTo({
|
|
url: "/pages/login/login"
|
|
});
|
|
}, 1500);
|
|
}
|
|
});
|
|
}
|
|
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 = "网络连接失败,请检查网络设置";
|
|
}
|
|
common_vendor.index.showToast({
|
|
title: errorMessage,
|
|
icon: "none",
|
|
duration: 3e3
|
|
});
|
|
return Promise.reject(error);
|
|
};
|
|
const createRequest = () => {
|
|
const platform = "mp-weixin";
|
|
if (!Object.keys(PLATFORM_CONFIG).includes(platform)) {
|
|
console.warn("Unsupported platform:", platform);
|
|
}
|
|
return async (options) => {
|
|
const config2 = {
|
|
url: options.url,
|
|
method: options.method || "GET",
|
|
header: options.header || {},
|
|
data: options.data || {},
|
|
timeout: options.timeout || 15e3
|
|
};
|
|
try {
|
|
const processedConfig = requestInterceptor(config2);
|
|
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, config2 = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "GET",
|
|
data: params,
|
|
...config2
|
|
});
|
|
},
|
|
post: (url, data, config2 = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "POST",
|
|
data,
|
|
...config2
|
|
});
|
|
},
|
|
put: (url, data, config2 = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "PUT",
|
|
data,
|
|
...config2
|
|
});
|
|
},
|
|
delete: (url, config2 = {}) => {
|
|
return createRequest()({
|
|
url,
|
|
method: "DELETE",
|
|
...config2
|
|
});
|
|
},
|
|
// 下载文件方法
|
|
download: async (url) => {
|
|
const platformConfig = getPlatformConfig();
|
|
const token = common_vendor.index.getStorageSync(platformConfig.storageKey);
|
|
let header = {};
|
|
header[platformConfig.authHeader] = `Bearer ${token}`;
|
|
const reqUrl = url;
|
|
if (!reqUrl.startsWith("http")) {
|
|
reqUrl = `${platformConfig.baseURL}${config.url}`;
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
common_vendor.index.downloadFile({
|
|
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;
|