Commit 93a3dbac by fengzhaoyu

MOD:线上版,解决刷新token后ios报错

parent 20563565
...@@ -7,14 +7,14 @@ const $http = (url, data, type, loadingType) => { ...@@ -7,14 +7,14 @@ const $http = (url, data, type, loadingType) => {
// content: '加载中...' // content: '加载中...'
// }); // });
// } // }
dd.showLoading({content: '加载中...'});
let hasToken = dd.getStorageSync({ key: 'token' }).data; // token let hasToken = dd.getStorageSync({ key: 'token' }).data; // token
let expires_in = hasToken.expires_in || ''; // 过期时效 let expires_in = hasToken.expires_in || ''; // 过期时效
let loginTime = hasToken.expires_in || ''; // 用户登陆时间 let loginTime = hasToken.loginTime || ''; // 用户登陆时间
let token = hasToken.access_token || ''; let token = hasToken.access_token || '';
let refresh_token = hasToken.refresh_token // 刷新token let refresh_token = hasToken.refresh_token // 刷新token
let mingUserId = dd.getStorageSync({ key: 'mingUserId' }).data let mingUserId = dd.getStorageSync({ key: 'mingUserId' }).data
// 时间戳未过期 // 时间戳未过期
// if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime < hasToken.expires_in * 1000)){
if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime)< (hasToken.expires_in-300) * 1000){ if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime)< (hasToken.expires_in-300) * 1000){
data.orgId = dd.corpId; data.orgId = dd.corpId;
let sendData = ''; let sendData = '';
...@@ -35,6 +35,14 @@ const $http = (url, data, type, loadingType) => { ...@@ -35,6 +35,14 @@ const $http = (url, data, type, loadingType) => {
if(res.data.resultCode === 0) { if(res.data.resultCode === 0) {
console.log(res.data.data) console.log(res.data.data)
return resolve(res.data.data) return resolve(res.data.data)
dd.hideLoading()
}
else if(res.data.code == 401) {
dd.removeStorageSync({key: 'token'})
dd.hideLoading()
dd.redirectTo({
url: '/pages/reTry/reTry'
})
} }
else { else {
// 修改系统繁忙时loading隐藏 // 修改系统繁忙时loading隐藏
...@@ -69,8 +77,8 @@ const $http = (url, data, type, loadingType) => { ...@@ -69,8 +77,8 @@ const $http = (url, data, type, loadingType) => {
refresh_token: res.data.refresh_token, refresh_token: res.data.refresh_token,
loginTime: (new Date()).getTime() loginTime: (new Date()).getTime()
} }
}) })
dd.hideLoading()
$http(url, data, type).then(res => { $http(url, data, type).then(res => {
return resolve(res) return resolve(res)
}) })
...@@ -78,12 +86,10 @@ const $http = (url, data, type, loadingType) => { ...@@ -78,12 +86,10 @@ const $http = (url, data, type, loadingType) => {
fail: function(err) { fail: function(err) {
dd.hideLoading() dd.hideLoading()
if(err.status == 426) { if(err.status == 426) {
dd.removeStorageSync({ dd.removeStorageSync({key: 'token'})
key:'token' dd.redirectTo({
}) url: '/pages/reTry/reTry'
dd.redirectTo({ })
url: '/pages/reTry/reTry'
})
} }
else { else {
dd.alert({ dd.alert({
...@@ -125,7 +131,7 @@ const $http = (url, data, type, loadingType) => { ...@@ -125,7 +131,7 @@ const $http = (url, data, type, loadingType) => {
url:`${app.globalData.globalUrl}/auth/oauth/token`, url:`${app.globalData.globalUrl}/auth/oauth/token`,
method: 'POST', method: 'POST',
success: function(res) { success: function(res) {
dd.removeStorage({ dd.removeStorageSync({
key:'token' key:'token'
}) })
dd.setStorageSync({ dd.setStorageSync({
...@@ -137,6 +143,7 @@ const $http = (url, data, type, loadingType) => { ...@@ -137,6 +143,7 @@ const $http = (url, data, type, loadingType) => {
loginTime: (new Date()).getTime() loginTime: (new Date()).getTime()
} }
}) })
dd.hideLoading()
$http(url, data, type).then(res => { $http(url, data, type).then(res => {
return resolve(res) return resolve(res)
}) })
......
...@@ -5,15 +5,16 @@ const login = () => { ...@@ -5,15 +5,16 @@ const login = () => {
if(dd.getStorageSync({ key: 'token' }).data && dd.getStorageSync({ key: 'token' }).data.access_token) { if(dd.getStorageSync({ key: 'token' }).data && dd.getStorageSync({ key: 'token' }).data.access_token) {
let hasToken = dd.getStorageSync({ key: 'token' }).data; // token let hasToken = dd.getStorageSync({ key: 'token' }).data; // token
let expires_in = hasToken.expires_in || ''; // 过期时效 let expires_in = hasToken.expires_in || ''; // 过期时效
let loginTime = hasToken.expires_in || ''; // 用户登陆时间 let loginTime = hasToken.loginTime || ''; // 用户登陆时间
let token = hasToken.access_token || ''; let token = hasToken.access_token || '';
let refresh_token = hasToken.refresh_token // 刷新token let refresh_token = hasToken.refresh_token // 刷新token
// 判断token没有过期 // 判断token没有过期
if(((new Date()).getTime() - hasToken.loginTime < hasToken.expires_in * 1000)) { if(new Date().getTime() - hasToken.loginTime < (hasToken.expires_in - 300) * 1000) {
// if(((new Date()).getTime() - hasToken.loginTime < 30 * 1000)) { // if(((new Date()).getTime() - hasToken.loginTime < 30 * 1000)) {
dd.redirectTo({ dd.hideLoading()
url:'/pages/index/index' dd.redirectTo({
}) url:'/pages/index/index'
})
} }
// 判断token过期 // 判断token过期
else { else {
...@@ -45,6 +46,7 @@ const login = () => { ...@@ -45,6 +46,7 @@ const login = () => {
loginTime: (new Date()).getTime() loginTime: (new Date()).getTime()
} }
}) })
dd.hideLoading()
login() login()
} }
}, },
...@@ -53,10 +55,12 @@ const login = () => { ...@@ -53,10 +55,12 @@ const login = () => {
dd.removeStorageSync({ dd.removeStorageSync({
key: 'token' key: 'token'
}) })
login() dd.hideLoading()
login()
} }
else { else {
dd.hideLoading() dd.hideLoading()
dd.removeStorageSync({key:'token'})
dd.redirectTo({ dd.redirectTo({
url: '/pages/reTry/reTry' url: '/pages/reTry/reTry'
}) })
...@@ -66,6 +70,7 @@ const login = () => { ...@@ -66,6 +70,7 @@ const login = () => {
} }
else { else {
dd.hideLoading() dd.hideLoading()
dd.removeStorageSync({key:'token'})
dd.redirectTo({ dd.redirectTo({
url: '/pages/reTry/reTry' url: '/pages/reTry/reTry'
}) })
...@@ -105,6 +110,7 @@ const login = () => { ...@@ -105,6 +110,7 @@ const login = () => {
loginTime: (new Date()).getTime() loginTime: (new Date()).getTime()
} }
}) })
dd.hideLoading()
dd.redirectTo({ dd.redirectTo({
url:'/pages/index/index' url:'/pages/index/index'
}) })
......
...@@ -32,10 +32,11 @@ Page({ ...@@ -32,10 +32,11 @@ Page({
}) })
} }
else { else {
dd.hideLoading()
dd.removeStorageSync({key:'token'})
dd.redirectTo({ dd.redirectTo({
url: '/pages/reTry/reTry' url: '/pages/reTry/reTry'
}) })
dd.hideLoading()
} }
} }
} }
...@@ -71,10 +72,11 @@ Page({ ...@@ -71,10 +72,11 @@ Page({
$login.login() $login.login()
} }
else { else {
dd.hideLoading()
dd.removeStorageSync({key:'token'})
dd.redirectTo({ dd.redirectTo({
url:'/pages/reTry/reTry' url:'/pages/reTry/reTry'
}) })
dd.hideLoading()
} }
}, },
fail: function(res) { fail: function(res) {
...@@ -87,6 +89,8 @@ Page({ ...@@ -87,6 +89,8 @@ Page({
}) })
} }
else { else {
dd.hideLoading()
dd.removeStorageSync({key:'token'})
dd.redirectTo({ dd.redirectTo({
url:'/pages/reTry/reTry' url:'/pages/reTry/reTry'
}) })
...@@ -95,48 +99,6 @@ Page({ ...@@ -95,48 +99,6 @@ Page({
}) })
}, },
// 获取token接口
getUserToken(mingUserId) {
dd.httpRequest({
headers: {
'Authorization': 'Basic bWluZ3BheS1waG9uZTptaW5ncGF5LXBob25l',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
username: mingUserId,
password: mingUserId,
scope: 'server',
grant_type: 'password'
},
url:`${app.globalData.globalUrl}/auth/oauth/token`,
// url:`${app.globalData.globalUrl}/auth/oauth/`,
method: 'POST',
dataType: 'json',
success: function(res) {
dd.setStorageSync({
key: 'token',
data: {
access_token: res.data.access_token,
expires_in: res.data.expires_in,
refresh_token: res.data.refresh_token,
loginTime: (new Date()).getTime()
}
})
dd.hideLoading()
dd.redirectTo({
url: '/pages/index/index'
})
},
fail: function(res) {
console.log('err',res)
dd.redirectTo({
url:'/pages/reTry/reTry'
})
dd.hideLoading()
}
})
},
onReady() { onReady() {
// 页面加载完成 // 页面加载完成
}, },
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<view class="{{type === '6' ? 'clickTabs' : ''}}" data-num="300" onTap="changeRechargeNum">300元</view> <view class="{{type === '6' ? 'clickTabs' : ''}}" data-num="300" onTap="changeRechargeNum">300元</view>
</view> </view>
<view class="rechargeInput"> <view class="rechargeInput">
¥<input Screenshot value="{{inputValue}}" type="number" style="font-size:48rpx;height: 60rpx" onInput="bindKeyInput" placeholder="请输入充值金额"/> ¥<input Screenshot value="{{inputValue}}" type="number" style="font-size:48rpx;height: 60rpx" onInput="bindKeyInput" placeholder="请输入充值金额"/>
</view> </view>
<text>{{messagedetail}}</text> <text>{{messagedetail}}</text>
</view> </view>
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</view> </view>
<view class="balanceBox"> <view class="balanceBox">
<view class="balancetitle">账户余额</view> <view class="balancetitle">账户余额</view>
<view>¥ <view class="balanceNum">{{realityBalance ? realityBalance.toFixed(2) : '--'}}</view></view> <view>¥ <view class="balanceNum">{{`${realityBalance}` ? realityBalance.toFixed(2) : '--'}}</view></view>
</view> </view>
<view class="indexItemList"> <view class="indexItemList">
<view touchStart="touchStyle" touchEnd="touchEnd" onTap="toPayment"><image mode="scaleToFill" src="./../../assets/QRcode.png"/>付款</view> <view touchStart="touchStyle" touchEnd="touchEnd" onTap="toPayment"><image mode="scaleToFill" src="./../../assets/QRcode.png"/>付款</view>
......
...@@ -16,7 +16,7 @@ Page({ ...@@ -16,7 +16,7 @@ Page({
// }, // },
onLoad(query) { onLoad(query) {
dd.hideLoading()
const _that = this; const _that = this;
dd.setNavigationBar({ dd.setNavigationBar({
title: '一卡通', title: '一卡通',
......
...@@ -13,7 +13,8 @@ Page({ ...@@ -13,7 +13,8 @@ Page({
size: 272, size: 272,
url: '', url: '',
ecc: 2, ecc: 2,
isTimeOut: true isTimeOut: true,
ma: ''
}, },
onLoad(query) { onLoad(query) {
// 页面加载 // 页面加载
...@@ -33,15 +34,33 @@ Page({ ...@@ -33,15 +34,33 @@ Page({
}, },
// 生成二维码 // 生成二维码
refreshQRcode(){ refreshQRcode(){
webSocket.closeSocket();
dd.getNetworkType({
success: (res) => {
// 有网络状态
if (res.networkAvailable) {
// 获取免登授权码
webSocket.connectSocket();
webSocket.onSocketMessageCallback = this.onSocketMessageCallback;
}
}
});
const _that = this; const _that = this;
dd.showLoading({
content: '加载中...'
});
let hasToken = dd.getStorageSync({ key: 'token' }).data; let hasToken = dd.getStorageSync({ key: 'token' }).data;
let token = dd.getStorageSync({ key: 'token' }).data.access_token; let token = dd.getStorageSync({ key: 'token' }).data.access_token;
let expires_in = dd.getStorageSync({ key: 'token' }).data.expires_in; let expires_in = dd.getStorageSync({ key: 'token' }).data.expires_in;
let refresh_token = dd.getStorageSync({ key: 'token' }).data.refresh_token; let refresh_token = dd.getStorageSync({ key: 'token' }).data.refresh_token;
let mingUserId = dd.getStorageSync({ key: 'mingUserId' }).data let mingUserId = dd.getStorageSync({ key: 'mingUserId' }).data
console.log(dd.getStorageSync({ key: 'token' }).data.refresh_token,1111111111)
if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime < (hasToken.expires_in - 300) * 1000)) {
// if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime < 1* 1000)) { if(!!hasToken && (new Date().getTime() - hasToken.loginTime < (hasToken.expires_in - 300) * 1000)) {
// if(!!hasToken && ((new Date()).getTime() - hasToken.loginTime < 30* 1000)) {
this.setData({
isTimeOut: true
})
let timestamp = (new Date()).getTime(); let timestamp = (new Date()).getTime();
let url = aes.getAES(JSON.stringify({ let url = aes.getAES(JSON.stringify({
"token": token, "token": token,
...@@ -52,15 +71,14 @@ Page({ ...@@ -52,15 +71,14 @@ Page({
let name = 'myQrcode' let name = 'myQrcode'
const ctx = dd.createCanvasContext(name); const ctx = dd.createCanvasContext(name);
QR.draw(url, ctx, size, Number(ecc)); QR.draw(url, ctx, size, Number(ecc));
dd.hideLoading()
} }
else { else {
dd.getNetworkType({ dd.getNetworkType({
success: (res => { success: (res => {
// 网络可用的情况下 刷新token // 网络可用的情况下 刷新token
if(res.networkAvailable) { if(res.networkAvailable) {
dd.showLoading({
content: '加载中...'
});
dd.httpRequest({ dd.httpRequest({
headers: { headers: {
'Authorization': 'Basic bWluZ3BheS1waG9uZTptaW5ncGF5LXBob25l', 'Authorization': 'Basic bWluZ3BheS1waG9uZTptaW5ncGF5LXBob25l',
...@@ -74,9 +92,9 @@ Page({ ...@@ -74,9 +92,9 @@ Page({
}, },
method: 'POST', method: 'POST',
success: function(res) { success: function(res) {
dd.removeStorage({ // dd.removeStorageSync({
key:'token' // key:'token'
}) // })
dd.setStorageSync({ dd.setStorageSync({
key: 'token', key: 'token',
data: { data: {
...@@ -90,14 +108,9 @@ Page({ ...@@ -90,14 +108,9 @@ Page({
_that.setData({ _that.setData({
isTimeOut: true isTimeOut: true
}) })
// hasToken = dd.getStorageSync({ key: 'token' }).data;
// token = dd.getStorageSync({ key: 'token' }).data.access_token;
// expires_in = dd.getStorageSync({ key: 'token' }).data.expires_in;
// refresh_token = dd.getStorageSync({ key: 'token' }).data.refresh_token;
let timestamp = (new Date()).getTime(); let timestamp = (new Date()).getTime();
let url = aes.getAES(JSON.stringify({ let url = aes.getAES(JSON.stringify({
"token": token, "token": res.data.access_token,
"timestamp": timestamp, "timestamp": timestamp,
})) }))
let size = 272 let size = 272
...@@ -109,15 +122,20 @@ Page({ ...@@ -109,15 +122,20 @@ Page({
}, },
fail: function(err) { fail: function(err) {
if(err.status == 426) { if(err.status == 426) {
dd.redirectTo({ dd.hideLoading()
url: '/pages/reTry/reTry' dd.removeStorageSync({key:'token'})
}) dd.redirectTo({
url: '/pages/reTry/reTry'
})
} }
else { else {
dd.alert({ dd.alert({
content: '请检查网络并下拉重试', content: '请检查网络并下拉重试',
buttonText: '确定' buttonText: '确定'
}) })
_that.setData({
isTimeOut: false
})
dd.hideLoading() dd.hideLoading()
} }
} }
...@@ -153,16 +171,7 @@ Page({ ...@@ -153,16 +171,7 @@ Page({
_that.refreshQRcode(); _that.refreshQRcode();
}, 55000); }, 55000);
},500) },500)
dd.getNetworkType({
success: (res) => {
// 有网络状态
if (res.networkAvailable) {
// 获取免登授权码
webSocket.connectSocket();
webSocket.onSocketMessageCallback = this.onSocketMessageCallback;
}
}
});
}, },
// socket收到的信息回调 // socket收到的信息回调
onSocketMessageCallback: function(res) { onSocketMessageCallback: function(res) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment