uniapp 微信小程序 授权获取个人信息

主要用 uni.login 和  uni.getUserProfile  两个方法。

特别注意 uni.getUserProfile 里会重置code

 let code = "";
      uni.login({
        provider: "weixin",
        success: function (loginRes) {
          console.log("getUserProfile", loginRes);
          code = loginRes.code;
        },
      });
      uni.getUserProfile({
        lang: "zh_CN",
        desc: "登录",
        success: (res) => {
          console.log("user", res);
          this.userInfo = res.userInfo;
          let data = {
            channelId: this.channelId,
            token: code,
            signature: res.encryptedData,
            iv: res.iv,
          };
          uni.request({
            url: baseUrl + "/login", //仅为示例,并非真实接口地址。
            data: {
              ...data,
            },
            method: "POST",
            success: (res) => {
             
              }
            },
          });
        },
      });

这样就能拿到用户信息了。

原文地址:https://www.cnblogs.com/lq2333/p/15573814.html