yzxy-mobile/util/request.js

86 lines
2.2 KiB
JavaScript

// http.js页面
//#ifdef APP-PLUS
export const baseUrl = "http://139.9.58.213"
//#endif
// 判断是H5
//#ifdef H5
// const BASE_URL = "/api";
const BASE_URL = "http://cdxy.qyff.86698.cn"
export const baseUrl = BASE_URL
//#endif
const request = (url, method, data = {}, showLoading = false) => {
return new Promise((resolve, reject) => {
// const token = uni.getStorageSync('token')
const token = uni.getStorageSync("token")
if (showLoading) {
uni.showLoading({
title: ""
})
}
let api = baseUrl + url
let tempData = data
uni.request({
url: api,
data: tempData,
method: method,
header: { //自定义请求头
'content-type': 'application/json',
'token': token
},
success: res => {
console.log(res)
uni.hideLoading()
if (res.data.code == 401) {
uni.reLaunch({
url: '/pages/login'
})
} else {
resolve(res);
}
// let code = res.data.code
// if (code == 200) {
// console.log('接口正常api', api)
// console.log('接口正常入参', tempData)
// console.log('接口正常返参', res)
// let data = res.data.data;
// resolve(data);
// } else if (code == 2) {
// console.log('接口code==2api', api)
// console.log('接口code==2入参', tempData)
// console.log('接口code==2返参', res)
// reject(res);
// uni.removeStorageSync('token')
// uni.reLaunch({
// url: '/pages/login/login'
// })
// } else if (res.data.msg == "暂无数据!") {
// console.log('接口暂无数据api', api)
// console.log('接口暂无数据入参', tempData)
// console.log('接口暂无数据返参', res)
// resolve([]);
// } else {
// console.log('接口错误api', api)
// console.log('接口错误入参', tempData)
// console.log('接口错误返参', res)
// resolve(res);
// reject(res);
// }
},
fail: (error) => {
uni.hideLoading()
console.log('接口异常api', api)
console.log('接口异常入参', tempData)
console.log('接口异常返参', error)
reject(error);
},
complete() {
uni.hideLoading()
}
})
})
}
export default {
request
}