uniapp 微信小程式 授權獲取個人資訊
阿新 • • 發佈:2021-11-18
主要用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) => { } }, }); }, });
這樣就能拿到使用者資訊了。