1. 程式人生 > 其它 >uniapp 微信小程式 授權獲取個人資訊

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) => { } }, }); }, });

這樣就能拿到使用者資訊了。