118 lines
3.9 KiB
JavaScript
118 lines
3.9 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const BASE_URL = "https://mini-chat.1024tool.vip";
|
|
let authModalShown = false;
|
|
function handleAuthExpired() {
|
|
if (authModalShown)
|
|
return;
|
|
authModalShown = true;
|
|
common_vendor.index.removeStorageSync("token");
|
|
common_vendor.index.showModal({
|
|
title: "提示",
|
|
content: "账号登录已过期,请重新登录",
|
|
showCancel: false,
|
|
success: () => {
|
|
authModalShown = false;
|
|
common_vendor.index.navigateTo({ url: "/pages/login/index" });
|
|
}
|
|
});
|
|
}
|
|
function request({ url, method = "GET", data = {}, header = {} }) {
|
|
return new Promise((resolve, reject) => {
|
|
const finalHeader = { ...buildDefaultHeaders(), ...header };
|
|
try {
|
|
common_vendor.index.__f__("log", "at utils/request.js:25", "HTTP request", method, url, "data", data, "headers", finalHeader);
|
|
} catch (e) {
|
|
}
|
|
common_vendor.index.request({
|
|
url: BASE_URL + url,
|
|
method,
|
|
data,
|
|
header: finalHeader,
|
|
timeout: 15e3,
|
|
success: (res) => {
|
|
const code = res.statusCode;
|
|
try {
|
|
common_vendor.index.__f__("log", "at utils/request.js:34", "HTTP response", method, url, "status", code, "body", res.data);
|
|
} catch (e) {
|
|
}
|
|
if (code >= 200 && code < 300) {
|
|
const body = res.data;
|
|
resolve(body && body.data !== void 0 ? body.data : body);
|
|
} else {
|
|
if (code === 401) {
|
|
const suppress = finalHeader && (finalHeader["X-Suppress-Auth-Modal"] || finalHeader["x-suppress-auth-modal"]);
|
|
if (!suppress) {
|
|
handleAuthExpired();
|
|
}
|
|
}
|
|
const msg = res.data && (res.data.message || res.data.msg) || "请求错误";
|
|
reject({ message: msg, statusCode: code, data: res.data });
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
try {
|
|
common_vendor.index.__f__("error", "at utils/request.js:50", "HTTP fail", method, url, "err", err);
|
|
} catch (e) {
|
|
}
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
function authRequest(options) {
|
|
const token = common_vendor.index.getStorageSync("token");
|
|
const base = buildDefaultHeaders();
|
|
const header = { ...base, ...options.header || {} };
|
|
if (token) {
|
|
header.Authorization = token;
|
|
header.authorization = token;
|
|
}
|
|
return request({ ...options, header });
|
|
}
|
|
function getDeviceId() {
|
|
try {
|
|
let id = common_vendor.index.getStorageSync("device_id");
|
|
if (!id) {
|
|
id = "dev-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 8);
|
|
common_vendor.index.setStorageSync("device_id", id);
|
|
}
|
|
return id;
|
|
} catch (_) {
|
|
return "dev-unknown";
|
|
}
|
|
}
|
|
function buildDefaultHeaders() {
|
|
const info = common_vendor.index.getSystemInfoSync();
|
|
const lang = info.language || "zh-CN";
|
|
const platform = info.platform || "unknown";
|
|
const brand = info.brand || "";
|
|
const model = info.model || "";
|
|
const osName = info.system || "";
|
|
const osVersion = info.system ? info.system.split(" ")[1] || "" : "";
|
|
const screen = `${info.screenWidth}x${info.screenHeight}`;
|
|
const pixelRatio = info.pixelRatio || 1;
|
|
const windowSize = `${info.windowWidth}x${info.windowHeight}`;
|
|
return {
|
|
"Accept": "application/json",
|
|
"content-type": "application/json",
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
"Accept-Language": lang,
|
|
"X-App-Client": "uni-app",
|
|
"X-App-Platform": platform,
|
|
"X-Device-Id": getDeviceId(),
|
|
"X-Device-Brand": brand,
|
|
"X-Device-Model": model,
|
|
"X-OS-Name": osName,
|
|
"X-OS-Version": osVersion,
|
|
"X-Screen": screen,
|
|
"X-Pixel-Ratio": pixelRatio,
|
|
"X-Window": windowSize,
|
|
"Cache-Control": "no-cache",
|
|
"User-Agent": `UniApp/${platform} (${brand} ${model}; ${osName} ${osVersion}) MiniProgram`
|
|
};
|
|
}
|
|
exports.authRequest = authRequest;
|
|
exports.request = request;
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
|