gree_leran/unpackage/dist/dev/mp-weixin/utils/http.js

65 lines
1.5 KiB
JavaScript

"use strict";
const common_vendor = require("../common/vendor.js");
const utils_Auth = require("./Auth.js");
const baseUrl = "https://9miao.fun/prod-api";
const http = (option) => {
if (!option instanceof Object) {
throw "参数非法";
}
const {
url,
data
} = option;
const token = utils_Auth.getToken();
return new Promise((resolve, reject) => {
common_vendor.index.request({
header: {
Authorization: token,
...option.header
},
url: baseUrl + url,
data,
method: option.method,
success(res) {
console.log("结果1", res);
if (res.data.code == 401) {
common_vendor.index.showToast({
icon: "none",
title: "请登录后使用该功能"
});
common_vendor.index.removeStorageSync("token");
}
resolve(res.data);
},
fail(err) {
console.log(err);
reject(err);
}
});
});
};
function uploadFile(file) {
const token = common_vendor.index.getStorageSync("token");
return new Promise((resolve, reject) => {
common_vendor.index.uploadFile({
url: `${baseUrl}/api/upload`,
filePath: file,
name: "file",
header: {
Authorization: token
},
success: (res) => {
setTimeout(() => {
resolve(res.data);
}, 1e3);
},
fail: (err) => {
console.log(err);
reject(err);
}
});
});
}
exports.http = http;
exports.uploadFile = uploadFile;