uniapp uniCloud登陆 使用uni-id-co登陆

EN
EN
2023-06-03 / 0 评论 / 238 阅读 / 正在检测是否收录...

可以在onload中调用,也可以事件调用,不跳转到uni-id-pages提供的页面,使用uniCloud uni-Id登陆。

//微信小程序
const uniIdCo = uniCloud.importObject('uni-id-co')
uni.login({
    provider: 'weixin',
    success: function(loginRes) {
        if (loginRes.code) {
            uniIdCo.loginByWeixin({
                code:loginRes.code
            }).then(res=>{
                if(res.errCode == 0){
                    mutations.updateUserInfo()
                }
            })
        }
    },
    fail(err) {
        uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
        })
    }
});

token过期判断

let token_expired = uni.getStorageSync('uni_id_token_expired')||0
let currTime = new Date().getTime();

let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{}
// token是否过期
if(token_expired < currTime) hostUserInfo = {}
const data = {
    userInfo: hostUserInfo,
    hasLogin: Object.keys(hostUserInfo).length != 0
}
0

评论 (0)

取消