四时宝库

程序员的知识宝库

Taro微信小程序使用getUserProfile获取微信用户头像昵称等信息

1、将 Taro CLI 版本及相关依赖更新到3.2.1版本

cnpm install -g @tarojs/cli@3.2.1


2、根据用户授权获取微信头像昵称等信息

<AtButton circle type='secondary' onClick={() => this.handleGetUserProfile()}>微信快捷注册</AtButton>
// 通过getUserProfile获取微信用户信息
  handleGetUserProfile = () => {
    Taro.getUserProfile({
      lang: 'zh_CN',
      desc: "获取你的昵称、头像、地区及性别",
      success: response => {
        const wxUserInfo = response.userInfo;
        const { openId } = this.state;
        console.log('getUserProfile',  wxUserInfo);
        const from_url = Taro.getCurrentInstance().router?.params["from_url"];
        Taro.navigateTo({url: `${ROUTER_ENUM.PAGE_USER_REGISTER}?userInfo=${JSON.stringify(wxUserInfo)}&openId=${openId || ''}&from_url=${from_url}`})
      },
      fail: () => {
        //拒绝授权
        console.error("您拒绝了请求");
        return;
      }
    })
  };

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接