This commit is contained in:
2025-09-17 14:40:16 +08:00
parent 46c5682960
commit a3bbc0098a
94 changed files with 3549 additions and 105 deletions

View File

@@ -8,24 +8,29 @@ function buildUrl(path) {
return path;
return common_config.API_BASE_URL + (path.startsWith("/") ? path : "/" + path);
}
function requestWithFallback(options, candidates, idx, resolve, reject) {
const base = candidates[idx] || common_config.API_BASE_URL;
const url = options.url.replace(/^https?:\/\/[^/]+/, base);
common_vendor.index.request({ ...options, url, success: (res) => {
const { statusCode, data } = res;
if (statusCode >= 200 && statusCode < 300)
return resolve(data);
if (idx + 1 < candidates.length)
return requestWithFallback(options, candidates, idx + 1, resolve, reject);
reject(new Error("HTTP " + statusCode));
}, fail: (err) => {
if (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 = { "X-Shop-Id": common_config.SHOP_ID };
if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
common_vendor.index.request({
url: buildUrl(path),
method: "GET",
data: params,
header: headers,
success: (res) => {
const { statusCode, data } = res;
if (statusCode >= 200 && statusCode < 300)
return resolve(data);
reject(new Error("HTTP " + statusCode));
},
fail: (err) => reject(err)
});
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 = {}) {
@@ -33,21 +38,67 @@ function post(path, body = {}) {
const headers = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
common_vendor.index.request({
url: buildUrl(path),
method: "POST",
data: body,
header: headers,
success: (res) => {
const { statusCode, data } = res;
if (statusCode >= 200 && statusCode < 300)
return resolve(data);
reject(new Error("HTTP " + statusCode));
},
fail: (err) => reject(err)
});
const options = { url: buildUrl(path), method: "POST", data: body, header: headers };
requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
});
}
function put(path, body = {}) {
return new Promise((resolve, reject) => {
const headers = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
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 = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
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 = { "X-Shop-Id": common_config.SHOP_ID };
if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
header["X-User-Id"] = common_config.DEFAULT_USER_ID;
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