uniapp使用云函数中的插件uni-id实现微信小程序的登录

2020-11-13  本文已影响0人  焚心123
image.png
image.png
这是微信小程序中的点击按钮进行登录,获取用户的信息,方便我们后面获取用户的token
<button class="my-nav-title wx-title"  open-type="getUserInfo"
            @getuserinfo = "wx_user">未登入,点击登入</button>

wx_user(e){//点击获取用户信息
            var that = this;
                uni.showLoading({
                    title:'登录中...'
                })
                uni.login({
                    success(res) {
                        uniCloud.callFunction({
                            name:'user-center',
                            data:{
                                action: 'loginByWeixin',//这是我们云函数文件中的微信登录的值
                                params:{
                                    code:res.code//这是登录的code值
                                }
                            },
                            success:ret=>{
                                if(ret.result.code == 0){
                                    that.is_token = true;
                                    uni.hideLoading();
                                    that.img_url = e.detail.userInfo.avatarUrl;
                                    that.userName = e.detail.userInfo.userName;
                                    console.log(that.img_url);
                                    console.log(that.userName)
                                    uni.setStorageSync('user',e.detail.userInfo);
                                    uni.setStorageSync('token',ret.result.token);
                                    uni.setStorageSync('uni_id_token_expired',ret.result.tokenExpired);
                                }
                            },
                            fail:msg1=>{
                                that.is_token = false;
                                uni.hideLoading();
                                console.error(msg1);
                                uni.showModal({
                                    showCancel: false,
                                    content: '微信登录失败,请稍后再试'
                                })
                            }
                        })
                    },fail:msg=>{
                        that.is_token = false;
                        uni.hideLoading();
                        console.error(msg);
                        uni.showModal({
                            showCancel: false,
                            content: '微信登录失败,请稍后再试'
                        })
                    }
                })
                
            },
上一篇 下一篇

猜你喜欢

热点阅读