Files
video_mini/utils/request.js
T
2025-08-25 11:20:35 +08:00

87 lines
2.5 KiB
JavaScript

import config from "./config.js";
// 获取当前运行环境
// 同步获取系统信息
const systemInfo = uni.getSystemInfoSync();
const os = systemInfo.osName;
const os_type = getType();
function getType() {
// #ifdef APP
return 1
// #endif
// #ifdef H5
return 1
// #endif
// #ifdef MP-WEIXIN
return 3
// #endif
// #ifdef MP-TOUTIAO
return 4
// #endif
}
const options = {
url: String,
method: String,
data: Object,
header: Object,
isLoading: Boolean,
isAllLink: Boolean
}
function httpRequest(options = options) {
let token = uni.getStorageSync('token');
token = token?token:uni.getStorageSync('userinfo').token
const OS = uni.getSystemInfoSync();
// let token = '0fea3d3d-9c6b-446c-a5df-57779c70a273';
return new Promise((resolve, reject) => {
if (options.isLoading) {
uni.showLoading()
}
uni.request({
url: options.isAllLink ? options.url : config.webUrl + options.url,
method: options.method || "GET",
data: options.data || {},
header: {
'token': token || "",
'os':os,
'os_type':os_type,
},
success: (res) => {
if (res.data.code == 401) {
console.log('no login')
uni.showModal({
title: '提示',
content: "请登录后再进行操作",
confirmText: "确定",
cancelText: '取消',
success: (res) => {
if (res.confirm) {
let nowPage = getCurrentPages();
let pageNum = nowPage.length - 1;
console.log(nowPage)
uni.setStorageSync('loginPage', nowPage[pageNum].route)
uni.navigateTo({url: "/pagesA/login/login"})
}
}
})
reject(res.data)
} else if (res.data.code == 0) {
resolve(res.data)
} else {
resolve(res.data);
}
},
fail: (error) => {
reject(error);
},
complete: (req) => {
if (options.isLoading) {
uni.hideLoading()
}
}
});
});
}
module.exports = {
httpRequest
}