进入游客模式,不发起任何请求。
1. 在需要进入游客模式的时候存一个变量挂载到wx。
这里当状态码返回4001时进入游客模式,存的是 wx.loginMode。
wx.http.POST({
url: "/authentication/generateCode",
noCheck: true,
thirdPartyAPI: true,
params: {
mobile: $data.phone
},
success(res) {
if (res.status == 200) {
// 登录
} else if(res.status == 4001) {
// 游客模式
wx.showModal({
title: "提示",
content: "该手机号未登记,是否继续以游客模式登录",
success(res) {
if (res.confirm) {
wx.setStorageSync('phone', $data.phone)
if(wx.getStorageSync('bindMpUsers') && wx.getStorageSync('bindMpUsers') === 'isBind') {
wx.loginMode = 'tourist'
wx.switchTab({
url: "/pages/patientEnd/home/index"
});
} else {
that.bindMpUsers().then(() => {
wx.loginMode = 'tourist'
wx.switchTab({
url: "/pages/patientEnd/home/index"
});
})
}
} else if (res.cancel) {
// 点击取消
that.setData({
codeText: "获取验证码"
});
}
}
});
} else {
wx.showToast({
title: res.message,
icon: "none",
duration: 2000
});
that.setData({
codeText: "获取验证码"
});
}
}
});
2. 然后在 http.js 文件中通过刚刚存的变量判断,决定是否发请求。
在 wx.request 之前做判断,如果是游客模式,就 return。
if(wx.loginMode && wx.loginMode === 'tourist') {
return
}
注意
由于进入游客模式后不发起任何请求,所以在需要调用接口比如解绑手机号/退出登录等按钮的时候,需要先将 wx.loginMode 置为 null ,接口调用成功后再决定是否置为 'tourist'(比如退出登录的时候接口调用成功后也不需要置为 'tourist' )。
以解绑手机号为例:
// 解绑手机号
unBindMpUsers() {
let tel = wx.getStorageSync('phone')
wx.showModal({
title: "提示",
content: "解绑后此微信将无法收到 " + tel + " 手机号的检查和报告的消息推送,是否解绑?",
success(res) {
if (res.confirm) {
// 为 null
wx.loginMode = null
wx.login({
success(res) {
if(res.code) {
wx.showLoading({
title: '正在解绑手机号'
});
wx.http.POST({
url: '/authentication/unbindMpUsers',
noCheck: true,
params: {
tel,
jsCode: res.code,
},
success(res) {
wx.hideLoading()
if(res.status == 200) {
// 为 'tourist'
wx.loginMode = 'tourist'
wx.setStorageSync('bindMpUsers', 'isUnBind')
wx.showToast({
title: "解绑成功",
icon: "success",
duration: 2000
});
}
},
complete() {
wx.hideLoading()
}
})
}
}
})
} else if (res.cancel) {
// 点击取消
}
}
});
}