acdr-ui/dist/dev/mp-weixin/utils/http.js

153 lines
4.5 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
const common_vendor = require("../common/vendor.js");
const store_user = require("../store/user.js");
const http = (options) => {
const userStore = store_user.useUserStore();
return new Promise((resolve, reject) => {
common_vendor.index.request(__spreadProps(__spreadValues({}, options), {
url: options.url,
dataType: "json",
// 响应成功
success(res) {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data);
} else if (res.statusCode === 401) {
userStore.clearUserInfo();
common_vendor.index.navigateTo({ url: "/pages/login/index" });
reject(res);
} else {
!options.hideErrorToast && common_vendor.index.showToast({
icon: "none",
title: res.data.message || "请求错误"
});
reject(res);
}
},
// 响应失败
fail(err) {
common_vendor.index.showToast({
icon: "none",
title: "网络错误,换个网络试试"
});
reject(err);
}
}));
});
};
const httpGet = (url, query) => {
return http({
url,
query,
method: "GET"
});
};
const httpPost = (url, data, query) => {
return http({
url,
query,
data,
method: "POST"
});
};
const httpUploadFile = (url, filePath, name = "file", formData = {}) => {
const userStore = store_user.useUserStore();
return new Promise((resolve, reject) => {
common_vendor.index.uploadFile({
url,
filePath,
name,
formData,
header: {
satoken: `Bearer ${userStore.token}`
// 添加认证头部
},
timeout: 1e4,
success(res) {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(JSON.parse(res.data));
} else if (res.statusCode === 401) {
userStore.clearUserInfo();
common_vendor.index.navigateTo({ url: "/pages/login/index" });
reject(res);
} else {
common_vendor.index.showToast({
icon: "none",
title: "上传失败," + (JSON.parse(res.data).message || "请稍后重试")
});
reject(res);
}
},
fail(err) {
common_vendor.index.showToast({
icon: "none",
title: "网络错误,换个网络试试"
});
reject(err);
}
});
});
};
const httpUploadMultipleFiles = (_0, _1, ..._2) => __async(exports, [_0, _1, ..._2], function* (url, filePaths, name = "file", formData = {}) {
const uploadResults = [];
for (const filePath of filePaths) {
try {
const result = yield httpUploadFile(url, filePath, name, formData);
if (result.code == 200) {
uploadResults.push(result.data.url);
}
} catch (err) {
console.error("上传文件失败:", err);
}
}
return uploadResults;
});
http.uploadFile = httpUploadFile;
http.uploadMultipleFiles = httpUploadMultipleFiles;
http.get = httpGet;
http.post = httpPost;
exports.http = http;
exports.httpGet = httpGet;
exports.httpPost = httpPost;
exports.httpUploadFile = httpUploadFile;
exports.httpUploadMultipleFiles = httpUploadMultipleFiles;
//# sourceMappingURL=http.js.map