Skip to content

Commit

Permalink
fix: H5页面刷新导致账号退出的问题,添加重连机制
Browse files Browse the repository at this point in the history
退出登录时使用reLaunch
  • Loading branch information
liuxy committed Oct 9, 2019
1 parent e95dd8e commit 85b68c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
33 changes: 29 additions & 4 deletions App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let disp = require("utils/broadcast");
let logout = false;
let is_reconnect = false;
export default {
globalData: {
unReadMessageNum: 0,
Expand All @@ -15,7 +16,9 @@
isIPX: false //是否为iphone X
},
onLaunch: function() {
console.log('App Launch')
var me = this;
var logs = uni.getStorageSync("logs") || [];
logs.unshift(Date.now());
Expand Down Expand Up @@ -48,15 +51,22 @@
onOpened: (message)=>{
this.$im.conn.setPresence();
console.log('登录成功登录成功登录成功登录成功登录成功登录成功登录成功');
message.accessToken && uni.setStorageSync("myToken", message.accessToken);
if (is_reconnect) {
uni.hideToast();
this.$helper.toast('success', '登陆成功', 2000)
is_reconnect = false;
}
if(this.getCurrentRoute() == "pages/login/login" || getCurrentRoute() == "pages/login_token/login_token"){
this.onLoginSuccess();
}
},
onReconnect: ()=>{
this.$helper.toast('loading', '重连中...', 2000)
},
onSocketConnected: ()=>{
this.$helper.toast('success', 'socket连接成功', 2000)
this.$helper.toast('success', '登陆成功', 2000)
},
onClosed: ()=>{
this.$helper.toast('none', '网络已断开', 2000)
Expand Down Expand Up @@ -308,8 +318,23 @@
},
});
// #ifdef H5
if(!this.$im.conn.isOpened() && uni.getStorageSync("myToken") && uni.getStorageSync("myUsername")) {
is_reconnect = true;
this.$helper.toast('loading', '自动登陆中...', 10000, true);
//尝试使用token重新登陆
this.$conn.open({
apiUrl: this.$im.config.apiURL,
user: uni.getStorageSync("myUsername"),
accessToken: uni.getStorageSync("myToken"),
appKey: this.$im.config.appkey
});
}
// #endif
},
onShow: function() {
onShow() {
console.log('App Show')
},
onHide: function() {
Expand All @@ -318,7 +343,7 @@
methods: {
onLoginSuccess(){
uni.hideToast()
uni.hideToast();
uni.switchTab({
url: "../chat/chat"
});
Expand Down
7 changes: 6 additions & 1 deletion comps/chat/inputbar/suit/main/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@
if(this.chatType == msgType.chatType.CHAT_ROOM){
msg.setGroup("groupchat");
}
this.$im.conn.send(msg.body);
try{
this.$im.conn.send(msg.body);
}catch(e){
console.log(e);
}
uni.$emit('saveSendMsg', {
msg: msg,
type: msgType.TEXT,
Expand Down
5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@
},
"mp-toutiao" : {
"usingComponents" : true
},
"h5" : {
"router" : {
"mode" : "history"
}
}
}
3 changes: 1 addition & 2 deletions pages/setting/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
success: (res)=>{
if(res.confirm){
this.$im.conn.close();
// wx.closeSocket()
uni.redirectTo({
uni.reLaunch({
url: "../login/login"
});
}
Expand Down

0 comments on commit 85b68c4

Please sign in to comment.