uni-app app和微信小程序获取微信授权

<template>
    <view>
        <!-- #ifdef MP-WEIXIN -->
        <button type="default" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true">小程序登录</button>
        <!-- #endif -->
        <image :src="userUrl" mode=""></image>
        <h3>{{userName}}</h3>
        <h3>{{openId}}</h3>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                userName: '',
                userUrl: '',
                openId: ''
            }
        },
        onLoad(options) {
            console.log(options);
            this.appLogin()
        },
        methods: {
            // app
            appLogin() {
                uni.getProvider({
                    service: 'oauth',
                    success: (res) => {
                        console.log(res.provider);
                        //支持微信、qq和微博等
                        if (~res.provider.indexOf('weixin')) {
                            uni.login({
                                provider: 'weixin',
                                success: (loginRes) => {
                                    // 获取用户信息
                                    uni.getUserInfo({
                                        provider: 'weixin',
                                        success: (infoRes) => {
                                            this.nickName = infoRes.userInfo.nickName
                                            this.avatarUrl = infoRes.userInfo.avatarUrl
                                            this.openId = infoRes.userInfo.openId
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            },
            // 微信
            getUserInfo(res) {
                uni.showModal({
                    content: '将获取你的个人信息',
                    success: (res) => {
                        //如果用户点击了确定按钮
                        if (res.confirm) {
                            wx.getUserProfile({
                                desc: '获取你的昵称、头像、地区及性别',
                                success: res => {
                                    console.log(res)
                                    this.userName = res.userInfo.nickName
                                    this.userUrl = res.userInfo.avatarUrl
                                    uni.login({
                                        success: (loginRes) => {
                                            let appid = 'xxxx'
                                            let secret ='xxxx'
                                            let url ='https://api.weixin.qq.com/sns/jscode2session?appid=' +appid + '&secret=' +secret + '&js_code=' + loginRes.code +'&grant_type=authorization_code';
                                            uni.request({
                                                url:url,
                                                success: (res) => {
                                                    this.openId = res.data.openid
                                                }
                                            })
                                        }
                                    })
                                },
                                fail: res => {
                                    //拒绝授权
                                    wx.showToast({
                                        title: '您拒绝了请求',
                                        icon: 'error',
                                        duration: 2000
                                    });
                                    return;
                                }
                            });
                        } else if (res.cancel) {
                            //如果用户点击了取消按钮
                            console.log(3);
                            wx.showToast({
                                title: '您拒绝了请求',
                                icon: 'error',
                                duration: 2000
                            });
                            return;
                        }
                    }
                });

            },
        }
    }
</script>

<style>

</style>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值