133 lines
5.2 KiB
JavaScript
133 lines
5.2 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("./vendor.js");
|
|
const common_config = require("./config.js");
|
|
function buildUrl(path) {
|
|
if (!path)
|
|
return common_config.API_BASE_URL;
|
|
if (path.startsWith("http"))
|
|
return path;
|
|
return common_config.API_BASE_URL + (path.startsWith("/") ? path : "/" + path);
|
|
}
|
|
function parseJwtClaims(token) {
|
|
try {
|
|
const parts = String(token || "").split(".");
|
|
if (parts.length < 2)
|
|
return {};
|
|
const payload = JSON.parse(decodeURIComponent(escape(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")))));
|
|
return payload || {};
|
|
} catch (_) {
|
|
return {};
|
|
}
|
|
}
|
|
function buildAuthHeaders(base = {}) {
|
|
const headers = { ...base };
|
|
try {
|
|
const token = typeof common_vendor.index !== "undefined" ? common_vendor.index.getStorageSync("TOKEN") || "" : "";
|
|
if (token) {
|
|
headers["Authorization"] = `Bearer ${token}`;
|
|
const claims = parseJwtClaims(token);
|
|
if (claims && claims.shopId)
|
|
headers["X-Shop-Id"] = claims.shopId;
|
|
if (claims && claims.userId)
|
|
headers["X-User-Id"] = claims.userId;
|
|
}
|
|
} catch (_) {
|
|
}
|
|
return headers;
|
|
}
|
|
function requestWithFallback(options, candidates, idx, resolve, reject) {
|
|
const base = candidates[idx] || common_config.API_BASE_URL;
|
|
let url = options.url;
|
|
if (!/^https?:\/\//.test(url)) {
|
|
url = base + (url.startsWith("/") ? "" : "/") + url;
|
|
} else {
|
|
url = options.url.replace(/^https?:\/\/[^/]+/, base);
|
|
}
|
|
common_vendor.index.request({ ...options, url, dataType: "json", success: (res) => {
|
|
const { statusCode, data } = res;
|
|
if (statusCode >= 200 && statusCode < 300)
|
|
return resolve(data);
|
|
const msg = data && (data.message || data.error || data.msg) || "HTTP " + statusCode;
|
|
common_vendor.index.showToast({ title: msg, icon: "none" });
|
|
if (!options.__noRetry && statusCode >= 500 && idx + 1 < candidates.length) {
|
|
return requestWithFallback(options, candidates, idx + 1, resolve, reject);
|
|
}
|
|
reject(new Error(msg));
|
|
}, fail: (err) => {
|
|
if (!options.__noRetry && idx + 1 < candidates.length)
|
|
return requestWithFallback(options, candidates, idx + 1, resolve, reject);
|
|
reject(err);
|
|
} });
|
|
}
|
|
function get(path, params = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = buildAuthHeaders({});
|
|
const options = { url: buildUrl(path), method: "GET", data: params, header: headers };
|
|
requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
|
|
});
|
|
}
|
|
function post(path, body = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = buildAuthHeaders({ "Content-Type": "application/json" });
|
|
const options = { url: buildUrl(path), method: "POST", data: body, header: headers };
|
|
const p = String(path || "");
|
|
if (p.includes("/api/auth/wxmp/login") || p.includes("/api/auth/sms/login") || p.includes("/api/auth/sms/send") || p.includes("/api/auth/password/login") || p.includes("/api/auth/register"))
|
|
options.__noRetry = true;
|
|
requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
|
|
});
|
|
}
|
|
function put(path, body = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = buildAuthHeaders({ "Content-Type": "application/json" });
|
|
const options = { url: buildUrl(path), method: "PUT", data: body, header: headers };
|
|
requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
|
|
});
|
|
}
|
|
function del(path, body = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = buildAuthHeaders({ "Content-Type": "application/json" });
|
|
const options = { url: buildUrl(path), method: "DELETE", data: body, header: headers };
|
|
requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
|
|
});
|
|
}
|
|
function uploadWithFallback(options, candidates, idx, resolve, reject) {
|
|
const base = candidates[idx] || common_config.API_BASE_URL;
|
|
const url = options.url.replace(/^https?:\/\/[^/]+/, base);
|
|
const uploadOptions = { ...options, url };
|
|
common_vendor.index.uploadFile({
|
|
...uploadOptions,
|
|
success: (res) => {
|
|
const statusCode = res.statusCode || 0;
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|
try {
|
|
const data = typeof res.data === "string" ? JSON.parse(res.data) : res.data;
|
|
return resolve(data);
|
|
} catch (e) {
|
|
return resolve(res.data);
|
|
}
|
|
}
|
|
if (idx + 1 < candidates.length)
|
|
return uploadWithFallback(options, candidates, idx + 1, resolve, reject);
|
|
reject(new Error("HTTP " + statusCode));
|
|
},
|
|
fail: (err) => {
|
|
if (idx + 1 < candidates.length)
|
|
return uploadWithFallback(options, candidates, idx + 1, resolve, reject);
|
|
reject(err);
|
|
}
|
|
});
|
|
}
|
|
function upload(path, filePath, formData = {}, name = "file") {
|
|
return new Promise((resolve, reject) => {
|
|
const header = buildAuthHeaders({});
|
|
const options = { url: buildUrl(path), filePath, name, formData, header };
|
|
uploadWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
|
|
});
|
|
}
|
|
exports.del = del;
|
|
exports.get = get;
|
|
exports.post = post;
|
|
exports.put = put;
|
|
exports.upload = upload;
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/http.js.map
|