diff --git a/api/index/index.js b/api/index/index.js index 0f2257b..ee46db5 100644 --- a/api/index/index.js +++ b/api/index/index.js @@ -65,8 +65,9 @@ export function loadCategoryMenus(_that, _data) { reslove(_data); }, fail: function(e) { + console.log(123123123,e) wx.showToast({ - title: "服务器异常了", + title: e, icon: 'none', duration: 2000 }) diff --git a/lib/java110/api/Java110SessionApi.js b/lib/java110/api/Java110SessionApi.js index b7a60e0..076a437 100644 --- a/lib/java110/api/Java110SessionApi.js +++ b/lib/java110/api/Java110SessionApi.js @@ -55,6 +55,7 @@ export function login(userName, passwd) { uni.setStorageSync(java110Config.USER_INFO, _userInfo); uni.setStorageSync(java110Config.TOKEN, data.token); let afterOneHourDate = date.addHour(new Date(), 1); + //let afterOneHourDate = date.addMinutes(new Date(), 1); wx.setStorageSync(java110Config.LOGIN_FLAG, { sessionKey: _tmpUserInfo.userName, expireTime: afterOneHourDate.getTime(), @@ -96,10 +97,7 @@ export function hasSession() { resolve(); return; } - //跳转至登录页面 - uni.reLaunch({ - url: '/pages/login/login' - }); + reject('回话已失效'); }) } diff --git a/lib/java110/api/SystemApi.js b/lib/java110/api/SystemApi.js index 042edfd..30ab6f9 100644 --- a/lib/java110/api/SystemApi.js +++ b/lib/java110/api/SystemApi.js @@ -15,6 +15,7 @@ export function getHeaders(){ 'req-time': getDateYYYYMMDDHHMISS(), 'sign': '1234567', 'user-id': '-1', + 'X-Requested-With':'XMLHttpRequest', // #ifndef H5 cookie: '_java110_token_=' + uni.getStorageSync('token') // #endif diff --git a/lib/java110/request.js b/lib/java110/request.js index da60677..6f98ee3 100644 --- a/lib/java110/request.js +++ b/lib/java110/request.js @@ -1,42 +1,83 @@ - -import {hasSession} from './api/Java110SessionApi.js' +import { + hasSession +} from './api/Java110SessionApi.js' import java110Config from './Java110Config.js' -import {wxuuid} from './utils/SeqUtil.js' +import { + wxuuid +} from './utils/SeqUtil.js' -import {getDateYYYYMMDDHHMISS} from './utils/DateUtil.js' +import { + getDateYYYYMMDDHHMISS +} from './utils/DateUtil.js' -export default{ +export default { /** * 请求 * @param {Object} _reqObj 请求内容 */ - request:function(_reqObj) { + request: function(_reqObj) { //检查是否登录成功 hasSession() - .then((_data)=>{ - //重写token - // #ifndef H5 - _reqObj.header.cookie = '_java110_token_=' + uni.getStorageSync('token'); - // #endif - //console.log(_reqObj); - uni.request(_reqObj); - }) - + .then((_data) => { + //重写token + // #ifndef H5 + _reqObj.header.cookie = '_java110_token_=' + uni.getStorageSync('token'); + // #endif + //console.log(_reqObj); + + let _success = _reqObj.success; + let _fail = _reqObj.fail; + //todo 拦截成功 + _reqObj.success = function(_res) { + if (_res.statusCode == 401) { + uni.reLaunch({ + url: '/pages/login/login' + }); + return; + } + if(!_success){ + return; + } + _success(_res); + } + //todo 拦截失败 + _reqObj.fail = function(_res) { + if (_res.statusCode == 401) { + uni.reLaunch({ + url: '/pages/login/login' + }); + return; + } + if(!_fail){ + return; + } + _fail(_res); + } + + uni.request(_reqObj); + }, err => { + //跳转至登录页面 + uni.reLaunch({ + url: '/pages/login/login' + }); + }) + } } /** * 获取 header */ -export function getHeaders(){ +export function getHeaders() { return { 'app-id': java110Config.AppId, 'transaction-id': wxuuid(), 'req-time': getDateYYYYMMDDHHMISS(), 'sign': '1234567', 'user-id': '-1', + 'X-Requested-With': 'XMLHttpRequest', // #ifndef H5 cookie: '_java110_token_=' + uni.getStorageSync('token') // #endif