gree_leran/utils/http.js

68 lines
1.2 KiB
JavaScript

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