检查接入状态
引入sdk 后,使用 <button open-type="getUserInfo"/>
进行用户信息授权的小程序,可以通过检查登录状态来判断引入sdk是否成功。
在小程序总入口获取用户信息
开发者在小程序总入口获取用户信息,并把信息存放在 globalData
。
注意:必须在程序开始时进行 login
,确保用户后续操作都在登录完成后开始。
import XH_MINIPRO_SDK from './utils/xh_minipro_sdk_1.5.0.min.js'
App({
onLaunch: function (options) {
XH_MINIPRO_SDK.login().then(res => {
this.globalData.platUserInfo = res
})
},
...
})
检查login的返回值
检查 login
返回值中是否存在微信相关的数据,比如头像、昵称等。
//app.js
import XH_MINIPRO_SDK from './utils/xh_minipro_sdk_1.5.0.min.js'
App({
onLaunch: function (options) {
XH_MINIPRO_SDK.login().then(res => {
this.globalData.platUserInfo = res
this.globalData.shouldUpdateUserInfo = !!res.nick_name
})
},
...
})
调用updateUserInfo
若 login
返回值中不存在头像、昵称等信息,则需要调用 updateUserInfo
。
<!-- index.wxml -->
<button v-if="{{shouldUpdateUserInfo}}" open-type="getUserInfo" bindgetuserinfo="bindUserLogin">授权</button>
//index.js
import XH_MINIPRO_SDK from './utils/xh_minipro_sdk_1.5.0.min.js'
Page({
data: {
shouldUpdateUserInfo: getApp().globalData.shouldUpdateUserInfo
},
bindUserLogin: function(e) {
if( e.detail.encryptedData ){
XH_MINIPRO_SDK.updateUserInfo({
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
signature: e.detail.signature
})
.then(res=>{
getApp().globalData.platUserInfo = res
})
}
},
...
})