uniapp开发微信小程序获取用户基本信息以及手机号 uni.getUserProfile getPhoneNumber

本文介绍了在uniapp中如何通过button组件获取微信小程序用户的个人信息和手机号。详细步骤包括设置button属性,调用uni.getUserProfile和getPhoneNumber接口,以及处理解密用户信息的过程,涉及到的权限授权和后端交互也进行了简要说明。

首先获取用户信息以及手机号必须要通过button按钮点击实现

小程序登录、用户信息相关接口调整说明官方

一、获取用户信息(友情链接

依据微信小程序开发文档描述,先写一个button按钮,open-type 设为 getUserProfile,在uniapp页面中创建按钮

          <button
            style="border: none; height: 120rpx; width: 120rpx"
            class="login obtain"
            open-type="getUserProfile"
            @click="getUserProfile"
            withCredentials="true"
            v-if="theUser == 1"
          >获取用户信息</button>

在methods中:

    getUserProfile() {
      let that = this;
      if (uni.getUserProfile) {
        uni.getUserProfile({
          desc: "获取用户信息仅用于展示",
          success(res) {
            console.log(res);
            console.log("用户点击允许");
            console.log("用户信息", that.userInfo);
            const userInfo = res.userInfo;
            //性别 0:未知、1:男、2:女
            const {
              nickName,
              avatarUrl,
              gender,
              province,
              city,
              country,
            } = userInfo;
            uni.setStorageSync("userInfo", userInfo);
            that.userInfo.nickName = userInfo.nickName;
            that.userInfo.avatarUrl = userInfo.avatarUrl;
            that.userInfo.gender = userInfo.gender;
            that.theUser = 0;//获取成功之后用来判断按钮的显示与隐藏
          },
          fail(error) {
            console.log("用户未授权", error);
            that.theUser = 1;
            // uni.showToast({
            //   title: "登录失败",
            //   duration: 3000,
            //   icon: "none",
            // });
          },
        });
      }
    }

二、获取用户的手机号   参考文档

获取用户手机号跟获取用户信息差不多,先写一个button按钮,open-type 设为 getPhoneNumber,在uniapp页面中创建按钮:

 <button style="color: #999; font-size: 36rpx; border: none"  class="login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" withCredentials="true">点击登录</button>

首先微信小程序,点击微信第三方登录,获取到当前用户绑定的手机号与openId(或unionid)

在页面生命周期 onLoad() 中,调用 wx.login 接口,获取code;用 code 换取 session 和 openId(前端获取)

怎么查看自己的secret

onLoad(){  
    wx.login({  
        success: (res) => {  
            if (res.code) {         //微信登录成功 已拿到code  
                this.jsCode=res.code        //保存获取到的code  
                uni.request({  
                    url: 'https://api.weixin.qq.com/sns/jscode2session',  
                    method:'GET',  
                    data: {  
               
在使用uniapp开发微信小程序获取用户信息登录时,需要用到小程序APPID和AppSecret,以下是具体方法: - **获取接口调用凭据**:需要用到小程序的唯一凭证AppID和唯一凭证密钥AppSecret,AppID和AppSecret可在「微信公众平台 - 设置 - 开发设置」页中获得(需要已经成为开发者,且帐号没有异常状态) [^1]。 - **获取用户基本信息以及手机号**:在获取手机号时,使用`uni.getUserProfile`和`getPhoneNumber`方法。示例代码如下 [^2]: ```javascript // 手机号 getPhoneNumber(e) { console.log(e); if (e.detail.errMsg == "getPhoneNumber:ok") { const accountInfo = uni.getAccountInfoSync(); //获取appid console.log(accountInfo.miniProgram.appId); console.log(this.session_key); var encryptedData = e.detail.encryptedData; var iv = e.detail.iv; var WXBizDataCrypt = require("@/common/WXBizDataCrypt");//解密文件地址 var pc = new WXBizDataCrypt( accountInfo.miniProgram.appId, this.session_key ); var data = pc.decryptData(encryptedData, iv); console.log(data.purePhoneNumber); this.userNumber = data.purePhoneNumber; this.theLogin = 0; } else { this.theLogin = 1; uni.showToast({ title: "登录失败", duration: 3000, icon: "none", }); } } ``` - **保存用户信息**:可使用`uni.getStorageSync`获取本地存储的用户信息,使用`uni.setStorageSync`更新本地存储的用户信息。示例代码如下 [^4]: ```javascript // 保存信息 saveUser(){ let dataInfo = uni.getStorageSync('userInfo'); let _this = this; changeInfo({ nickname: _this.nickname, avatar: _this.avatar, gender:_this.gender }, 'json').then(res => { if (res.code == 1) { dataInfo.avatar = _this.avatar dataInfo.nickname = _this.nickname dataInfo.gender = _this.gender uni.setStorageSync('userInfo', dataInfo) _this.goLogin = false _this.isLogin = true } }) } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值