Files
2026-07-24 11:56:12 +08:00

91 lines
2.7 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(213);
uni.login({
provider: 'weixin',
success: (s) => {
uni.request({
url: apiUrl.apiUrl + '/api/user/getOpenid', //仅为示例,并非真实接口地址。
data: {
code: s.code,
ad_code: uni.getStorageSync('get_ad_code'),
invite: uni.getStorageSync("invite_number")
},
success: (res) => {
if (res.data.code == 1) {
uni.setStorageSync("userinfo", res.data.userinfo);
uni.setStorageSync("token", res.data.userinfo.token);
uni.setStorageSync("config", res.data.config);
}
}
});
}
})
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
}