getUser(e) {
                wx.getUserProfile({
                    desc: '用户完善会员资料',
                    success: res => {
                        let userInfo = res.userInfo;
                        uni.showLoading({
                            title: '登录中'
                        });
                        wx.login({
                            //成功放回
                            success: (res) => {
                                let code = res.code
                                console.log(code);
                                wx.request({
                                    url: `https://api.weixin.qq.com/sns/jscode2session?appid=123123123&
                                    secret=123123123213&js_code=${code}&grant_type=authorization_code`,
                                    success: (res) => {
                                        userInfo.openid = res.data.openid
                                        //存储用户信息
                                        wx.setStorage({
                                            key: "userinfo",
                                            data: JSON.stringify(userInfo)
                                        })
                                        //页面跳转, 回到首页
                                        wx.reLaunch({
                                            url: "/pages/index/index"
                                        })
                                        uni.hideLoading();
                                    }
                                })
                            }
                        })
                    },
                    fail: res => {
                        uni.$showMsg("您取消了登录授权!")
                    }
                })
            },

 

页面:

<button class="btn_log" open-type="getUserInfo" @click="getUser">
微信授权登录
</button>

!!!uni.$showMsg是uni-app的弹窗方法,这里我封装了一下,appId,secret这两个可以去小程序后端获取

 

posted on 2022-09-20 09:51  最帅爸爸  阅读(118)  评论(0)    收藏  举报