2024-07-31 09:00:33 +08:00
|
|
|
|
// import qs from 'qs' // 处理data
|
|
|
|
|
|
import store from '@/vuex/store'
|
|
|
|
|
|
import i18n from '../locale/index.js'
|
|
|
|
|
|
const request = (params) => {
|
|
|
|
|
|
let _self = this;
|
|
|
|
|
|
let url = params.url;
|
|
|
|
|
|
let method = params.method || 'POST';
|
|
|
|
|
|
let data = params.data || {};
|
2024-08-01 12:21:21 +08:00
|
|
|
|
let acsurl = params.acsurl
|
2024-07-31 09:00:33 +08:00
|
|
|
|
let token = ''
|
|
|
|
|
|
if (store.getters.saveToken !== '') {
|
|
|
|
|
|
token = store.getters.saveToken
|
|
|
|
|
|
}
|
|
|
|
|
|
let defaultOpot = {
|
|
|
|
|
|
// 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
|
|
'Terminal-Type': 'innerH5',
|
|
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
|
|
|
|
}
|
|
|
|
|
|
let header = {}
|
|
|
|
|
|
method = method.toUpperCase()
|
|
|
|
|
|
if (method == 'POST') {
|
|
|
|
|
|
header = {
|
|
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
2024-08-01 12:21:21 +08:00
|
|
|
|
'Authorization': token,
|
|
|
|
|
|
'Accept-Language': uni.getLocale()
|
2024-07-31 09:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
// data = qs.stringify(data)
|
|
|
|
|
|
}
|
2024-08-01 12:21:21 +08:00
|
|
|
|
let requestUrl = `${store.getters.baseUrl}/` + url;
|
|
|
|
|
|
if (acsurl) {
|
|
|
|
|
|
requestUrl = `${store.getters.acsUrl}/` + url;
|
|
|
|
|
|
}
|
2024-07-31 09:00:33 +08:00
|
|
|
|
uni.showLoading({
|
|
|
|
|
|
// title: '加载中...'
|
|
|
|
|
|
title: i18n.tc('utils.loading')
|
|
|
|
|
|
});
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
uni.request({
|
|
|
|
|
|
url: requestUrl,
|
|
|
|
|
|
method: method,
|
|
|
|
|
|
header: Object.assign({}, defaultOpot, header),
|
|
|
|
|
|
data: data,
|
|
|
|
|
|
dataType: 'json'
|
|
|
|
|
|
// networkTimeout: {
|
|
|
|
|
|
// 'request': 5000
|
|
|
|
|
|
// }
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(res => { // 成功
|
2024-08-01 12:21:21 +08:00
|
|
|
|
uni.hideLoading()
|
2024-07-31 09:00:33 +08:00
|
|
|
|
if (res.length === 1) {
|
|
|
|
|
|
uni.showModal({
|
|
|
|
|
|
content: 'request:fail',
|
|
|
|
|
|
showCancel: false
|
|
|
|
|
|
})
|
|
|
|
|
|
reject('request:fail')
|
|
|
|
|
|
} else if (res[1] && res[1].statusCode === 400) {
|
|
|
|
|
|
uni.showModal({
|
|
|
|
|
|
content: res[1].data.message,
|
|
|
|
|
|
showCancel: false
|
|
|
|
|
|
})
|
|
|
|
|
|
reject(res[1].data.message)
|
|
|
|
|
|
} else if (res[1] && res[1].statusCode === 401) {
|
|
|
|
|
|
uni.showModal({
|
|
|
|
|
|
content: res[1].data.message,
|
|
|
|
|
|
showCancel: false
|
|
|
|
|
|
})
|
|
|
|
|
|
store.dispatch('delUserInfo', '')
|
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
|
url: '/pages/login/login'
|
|
|
|
|
|
})
|
|
|
|
|
|
} else if (res[1] && res[1].statusCode === 200) {
|
|
|
|
|
|
let {
|
|
|
|
|
|
data: dataType
|
|
|
|
|
|
} = res[1]
|
|
|
|
|
|
resolve(dataType)
|
|
|
|
|
|
// switch (dataType.code * 1) { // 拦截返回参数
|
|
|
|
|
|
// case 0:
|
|
|
|
|
|
// resolve(dataType)
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case 1003:
|
|
|
|
|
|
// uni.showModal({
|
|
|
|
|
|
// title: '登录已过期',
|
|
|
|
|
|
// content: '很抱歉,登录已过期,请重新登录',
|
|
|
|
|
|
// confirmText: '重新登录',
|
|
|
|
|
|
// success: function(res) {
|
|
|
|
|
|
// if (res.confirm) {
|
|
|
|
|
|
// uni.navigateTo({
|
|
|
|
|
|
// // 切记这儿需要哈pages.json保持一致;不能有.vue后缀
|
|
|
|
|
|
// url: '/pages/login/login'
|
|
|
|
|
|
// });
|
|
|
|
|
|
// } else if (res.cancel) {
|
|
|
|
|
|
// console.log('用户点击取消');
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// })
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case -1:
|
|
|
|
|
|
// uni.showModal({
|
|
|
|
|
|
// title: '请求数据失败',
|
|
|
|
|
|
// content: '获取数据失败!',
|
|
|
|
|
|
// confirmText: '确定',
|
|
|
|
|
|
// showCancel: false,
|
|
|
|
|
|
// success: function(res) {
|
|
|
|
|
|
// if (res.confirm) {} else if (res.cancel) {
|
|
|
|
|
|
// console.log('用户点击取消');
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// })
|
|
|
|
|
|
// break
|
|
|
|
|
|
// }
|
|
|
|
|
|
}else {
|
|
|
|
|
|
uni.showModal({
|
|
|
|
|
|
content: res[1].data.message,
|
|
|
|
|
|
showCancel: false
|
|
|
|
|
|
})
|
|
|
|
|
|
reject(res[1].data.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(err => { // 错误
|
2024-08-01 12:21:21 +08:00
|
|
|
|
uni.hideLoading()
|
2024-07-31 09:00:33 +08:00
|
|
|
|
reject(err)
|
|
|
|
|
|
})
|
|
|
|
|
|
.finally(() => {
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
export default request
|