gree_leran/utils/http.js

68 lines
1.2 KiB
JavaScript
Raw Normal View History

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