first commit
This commit is contained in:
127
com/http.js
Normal file
127
com/http.js
Normal file
@@ -0,0 +1,127 @@
|
||||
import store from '../store'
|
||||
let authStatus = 0;
|
||||
|
||||
import {
|
||||
API_URL,
|
||||
STATIC_URL
|
||||
} from '@/env'
|
||||
|
||||
const request = (options = {}) => {
|
||||
const token = store.state.auth.token;
|
||||
const location = store.state.location.current;
|
||||
// 在这里可以对请求头进行一些设置
|
||||
// 例如:
|
||||
options.header = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Accept": "application/json",
|
||||
"Authorization": token.token_type + ' ' + token.access_token,
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (authStatus == 401) {
|
||||
authStatus = 0
|
||||
}
|
||||
// uni.showLoading({
|
||||
// title: '',
|
||||
// mask: true
|
||||
// });
|
||||
let url = options.url;
|
||||
|
||||
if (options.url.indexOf('https://') == -1) {
|
||||
url = API_URL + options.url;
|
||||
}
|
||||
|
||||
uni.request({
|
||||
url: url || '',
|
||||
method: options.type || 'GET',
|
||||
data: {
|
||||
...options.data
|
||||
} || {},
|
||||
timeout: 50000,
|
||||
header: options.header || {},
|
||||
success: (res) => {
|
||||
|
||||
if (res != undefined && res.statusCode == 401) {
|
||||
if (authStatus == 0) {
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/me/login'
|
||||
// })
|
||||
|
||||
store.commit('auth/resetUser');
|
||||
store.commit('auth/resetToken');
|
||||
uni.clearStorageSync();
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '为提供更好的服务,请登录后继续',
|
||||
//showCancel:false,
|
||||
confirmText: '立即登录',
|
||||
cancelText: '随便逛逛',
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
|
||||
uni.navigateTo({
|
||||
url: '/pages/me/login'
|
||||
});
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
authStatus = 401
|
||||
}
|
||||
if (res != undefined && res.statusCode == 429) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: res.data.msg,
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
if (res != undefined && res.statusCode == 200 && !res.data.success) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: res.data.msg,
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
if (res.statusCode == 200 && res.data.success && res.data.show == 1) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: res.data.msg,
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
//uni.hideLoading();
|
||||
resolve(res.data);
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showToast({
|
||||
title: err.errMsg,
|
||||
icon: 'error'
|
||||
})
|
||||
//uni.hideLoading();
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
const get = (url, data, options = {}) => {
|
||||
options.type = 'GET';
|
||||
options.data = data;
|
||||
options.url = url;
|
||||
return request(options)
|
||||
}
|
||||
|
||||
const post = (url, data, options = {}) => {
|
||||
options.type = 'POST';
|
||||
options.data = data;
|
||||
options.url = url;
|
||||
return request(options)
|
||||
}
|
||||
|
||||
export default {
|
||||
request,
|
||||
get,
|
||||
post
|
||||
}
|
||||
Reference in New Issue
Block a user