54 lines
1.7 KiB
JavaScript
54 lines
1.7 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 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)
|
|
});
|
|
});
|
|
}
|
|
function post(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;
|
|
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)
|
|
});
|
|
});
|
|
}
|
|
exports.get = get;
|
|
exports.post = post;
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/http.js.map
|