页面修改
This commit is contained in:
parent
4408ac20dd
commit
c4f9c1f7d8
File diff suppressed because it is too large
Load Diff
210
utils/oahttp.js
210
utils/oahttp.js
@ -1,127 +1,127 @@
|
|||||||
import {
|
import {
|
||||||
HTTP_REQUEST_URL_THREE,
|
HTTP_REQUEST_URL_THREE,
|
||||||
HEADER,
|
HEADER,
|
||||||
TOKENNAME,
|
TOKENNAME,
|
||||||
} from '@/config/app';
|
} from '@/config/app';
|
||||||
import {
|
import {
|
||||||
Toast
|
Toast
|
||||||
} from '../libs/uniApi';
|
} from '../libs/uniApi';
|
||||||
// import { checkLogin } from '../libs/login';
|
// import { checkLogin } from '../libs/login';
|
||||||
import store from '../store';
|
import store from '../store';
|
||||||
|
|
||||||
function toLogin() {
|
function toLogin() {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '请先登录',
|
title: '请先登录',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
duration: 1000
|
duration: 1000
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function baseRequestTwo(url, method, data, {
|
function baseRequestTwo(url, method, data, {
|
||||||
noAuth = false,
|
noAuth = false,
|
||||||
noVerify = false,
|
noVerify = false,
|
||||||
onReLogin = false
|
onReLogin = false
|
||||||
}) {
|
}) {
|
||||||
let Url = HTTP_REQUEST_URL_THREE,
|
let Url = HTTP_REQUEST_URL_THREE,
|
||||||
header = HEADER;
|
header = HEADER;
|
||||||
if (!noAuth) {
|
if (!noAuth) {
|
||||||
// 已经未登录了,禁止请求
|
// 已经未登录了,禁止请求
|
||||||
if (!store.state.config.request) return Promise.reject({
|
if (!store.state.config.request) return Promise.reject({
|
||||||
msg: '未登录'
|
msg: '未登录'
|
||||||
});
|
});
|
||||||
//登录过期自动登录
|
//登录过期自动登录
|
||||||
if (!store.state.app.token) {
|
if (!store.state.app.token) {
|
||||||
toLogin();
|
toLogin();
|
||||||
store.commit("SET_REQUEST", false);
|
store.commit("SET_REQUEST", false);
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
msg: '未登录'
|
msg: '未登录'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
|
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
|
||||||
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
|
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
|
||||||
|
|
||||||
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
|
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
|
||||||
return new Promise((reslove, reject) => {
|
return new Promise((reslove, reject) => {
|
||||||
// uni.showLoading({
|
// uni.showLoading({
|
||||||
// title: '加载中'
|
// title: '加载中'
|
||||||
// })
|
// })
|
||||||
uni.request({
|
uni.request({
|
||||||
// url: Url + '/api/v1' + url,
|
// url: Url + '/api/v1' + url,
|
||||||
url: Url + '/api' + url,
|
url: Url + '/api' + url,
|
||||||
method: method || 'GET',
|
method: method || 'GET',
|
||||||
header: {
|
header: {
|
||||||
...header
|
...header
|
||||||
},
|
},
|
||||||
data: method != 'GET' ? data || {} : {},
|
data: method != 'GET' ? data || {} : {},
|
||||||
params: method == 'GET' ? data : {},
|
params: method == 'GET' ? data : {},
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
if (noVerify)
|
if (noVerify)
|
||||||
reslove(res.data);
|
reslove(res.data);
|
||||||
else if (res.data.code == -1) {
|
else if (res.data.code == -1) {
|
||||||
if(onReLogin) {
|
if (onReLogin) {
|
||||||
store.commit('LOGOUT');
|
store.commit('LOGOUT');
|
||||||
return reject();
|
return reject();
|
||||||
}
|
}
|
||||||
// 如果登录超时,自动重新登录并且继续发送请求
|
// 如果登录超时,自动重新登录并且继续发送请求
|
||||||
store.dispatch("RE_LOGIN", {
|
store.dispatch("RE_LOGIN", {
|
||||||
url: url,
|
url: url,
|
||||||
method: method,
|
method: method,
|
||||||
data: data,
|
data: data,
|
||||||
opt: {
|
opt: {
|
||||||
noAuth,
|
noAuth,
|
||||||
noVerify
|
noVerify
|
||||||
}
|
}
|
||||||
}).then((e)=>{
|
}).then((e) => {
|
||||||
reslove(e);
|
reslove(e);
|
||||||
}).catch((err)=>{
|
}).catch((err) => {
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
})
|
})
|
||||||
// store.commit("SET_REQUEST", false);
|
// store.commit("SET_REQUEST", false);
|
||||||
} else if (res.data.code == 0) {
|
} else if (res.data.code == 0) {
|
||||||
if (res.data.msg != '无登录信息') {
|
if (res.data.msg != '无登录信息') {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: res.data.msg || '请检查网络',
|
title: res.data.msg || '请检查网络',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
} else if (res.data.code == 1) {
|
} else if (res.data.code == 1) {
|
||||||
store.commit("SET_REQUEST");
|
store.commit("SET_REQUEST");
|
||||||
reslove(res.data);
|
reslove(res.data);
|
||||||
} else if (res.data.code == 200) {
|
} else if (res.data.code == 200) {
|
||||||
store.commit("SET_REQUEST");
|
store.commit("SET_REQUEST");
|
||||||
reslove(res.data.data);
|
reslove(res.data.data);
|
||||||
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
|
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
|
||||||
toLogin();
|
toLogin();
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
} else if (res.data.code == 501) {
|
} else if (res.data.code == 501) {
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: res.data.msg || '请检查网络',
|
title: res.data.msg || '请检查网络',
|
||||||
icon: 'none'
|
icon: 'none'
|
||||||
})
|
})
|
||||||
reject(res.data.msg || '请检查网络');
|
reject(res.data.msg || '请检查网络');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (message) => {
|
fail: (message) => {
|
||||||
// uni.hideLoading()
|
// uni.hideLoading()
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '网络错误',
|
title: '网络错误',
|
||||||
icon: 'none'
|
icon: 'none'
|
||||||
})
|
})
|
||||||
reject('请求失败');
|
reject('请求失败');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const oahttp = {};
|
const oahttp = {};
|
||||||
|
|
||||||
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
|
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
|
||||||
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
|
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
|
||||||
});
|
});
|
||||||
|
|
||||||
export default oahttp;
|
export default oahttp;
|
Loading…
x
Reference in New Issue
Block a user