初始化
This commit is contained in:
94
utils/service/http.js
Normal file
94
utils/service/http.js
Normal file
@@ -0,0 +1,94 @@
|
||||
import * as tools from '../tools';
|
||||
import * as config from './config';
|
||||
import {Storage_Token} from '@/config/const.js';
|
||||
|
||||
let isShowToast = false;
|
||||
|
||||
// #ifdef H5
|
||||
const baseUrl = ''
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
const baseUrl = config.host
|
||||
// #endif
|
||||
|
||||
class Request {
|
||||
_request(url, data = {}, method = 'GET') {
|
||||
let loading;
|
||||
if (data.loading) {
|
||||
loading = data.loading
|
||||
console.log('loading===>', loading)
|
||||
delete data.loading
|
||||
}
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (loading && !isShowToast) tools.showLoading()
|
||||
uni.request({
|
||||
url: baseUrl + url,
|
||||
data: data,
|
||||
method: method,
|
||||
header: {
|
||||
Authorization: 'Bearer '+ uni.getStorageSync(Storage_Token) || ''
|
||||
},
|
||||
success: res => {
|
||||
if (!isShowToast) tools.hideLoading()
|
||||
// tools.toast(String(new Date().getTime() - time))
|
||||
if (res.statusCode === 200) {
|
||||
if (res.data.code == '501') {
|
||||
tools.toast('数据中存在敏感词汇!')
|
||||
isShowToast = true
|
||||
setTimeout(() => {
|
||||
isShowToast = false
|
||||
}, 1600)
|
||||
reject('请求失败' + res.data.code)
|
||||
} else if (res.data.status == 400) {
|
||||
isShowToast = true
|
||||
setTimeout(() => {
|
||||
isShowToast = false
|
||||
}, 1600)
|
||||
tools.toast(res.data.message || '请求失败!')
|
||||
reject('请求失败' + res.data.code)
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else if (res.statusCode === 401) {
|
||||
isShowToast = true
|
||||
setTimeout(() => {
|
||||
isShowToast = false
|
||||
}, 1600)
|
||||
tools.toast(res.data.message)
|
||||
// 登录失效
|
||||
reject('登录失效')
|
||||
} else {
|
||||
tools.toast(res.data.message || '接口请求异常')
|
||||
isShowToast = true
|
||||
setTimeout(() => {
|
||||
isShowToast = false
|
||||
}, 1600)
|
||||
// 请求失败
|
||||
reject('请求失败' + res.statusCode)
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
tools.toast('服务器连接异常')
|
||||
//服务器连接异常
|
||||
// prettier-ignore
|
||||
console.log('===============================================================================================')
|
||||
console.log(baseUrl + url)
|
||||
console.log('== 接口地址:' + baseUrl + url)
|
||||
console.log('== 接口参数:' + JSON.stringify(data))
|
||||
console.log('== 请求类型:' + method)
|
||||
console.log('== 服务器连接异常')
|
||||
// prettier-ignore
|
||||
console.log('===============================================================================================')
|
||||
reject('服务器连接异常,请检查网络再试')
|
||||
},
|
||||
complete: () => {
|
||||
if (loading) console.log('hide loading...')
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export { Request }
|
||||
Reference in New Issue
Block a user