uniapp 微信公众号网页获取微信头像昵称(获取无反应)

操作步骤

1. 判断是否授权过头像昵称

2. 若未授权,判断是否是微信浏览器打开

// 判断是否微信浏览器
isWXBrowser() {
  return (
    String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) = "micromessenger");
  },

3. 若是微信浏览器打开,判断地址栏是否返回code

// 截取地址栏code
getUrlCode(name) {
  return (
    decodeURIComponent(
      (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(location.href) || [, ""])[1].replace(/\+/g, "%20")
    ) || null
  );
},

4. 若有code,根据code获取相关信息,无code,需跳转授权页面获取code

//访问微信地址,用来获取code
getWeChatCode() {
  //处理域名
  let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
  let appid = 'wx77b2************'
  window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + local + "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect"
},

5. 判断使用

if(!this.userInfo.avatar){ // 未授权头像昵称
    this.isWXBrowser = this.isWXBrowser();
    if (this.isWXBrowser) {
      let code = this.getUrlCode("code");
      if (code) {
        this.wxLogin(code);
      } else {
        this.getWeChatCode(); //微信授权
      }
    }
  }

后续出现点击获取头像昵称无反应的情况并报错[Component] <button>: chooseAvatar:fail api scope is not declared in the privacy agreement

是由于小程序的隐私协议更新小程序隐私协议开发指南 | 微信开放文档

1. 需在小程序后台设置用户隐私保护指引添加收集头像昵称的信息类型

2. 在manifest.json中添加"__usePrivacyCheck__":true,

"mp-weixin" : {
    "__usePrivacyCheck__":true,
}

3. 将基础库设为2.30.4

4. 待后台隐私协议审核通过后 将代码重新提交并发布到线上

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值