微信小程序 游客模式

进入游客模式,不发起任何请求。

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) {
        // 点击取消
      }
    }
  });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值