Uni-app 之APP和小程序微信授权登录

一、APP

<template>
    <view>
        <!-- #ifdef APP-PLUS -->
        <button class="" @click="appLogin">APP微信授权登录</button>
        <!-- #endif -->
    </view>
</template>

<script>
    export default {
        data() {
            return {

            }
        },
        onLoad(options) {
            console.log(options);
        },
        methods: {
            appLogin: function() {
                uni.getProvider({
                    service: 'oauth',
                    success: function(res) {
                        console.log(res.provider);
                        //支持微信、qq和微博等
                        if (~res.provider.indexOf('weixin')) {
                            uni.login({
                                provider: 'weixin',
                                success: function(loginRes) {
                                    console.log('-------获取openid(unionid)-----');
                                    console.log(JSON.stringify(loginRes));
                                    // 获取用户信息
                                    uni.getUserInfo({
                                        provider: 'weixin',
                                        success: function(infoRes) {
                                            console.log('-------获取微信用户所有-----');
                                            console.log(JSON.stringify(infoRes.userInfo));
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            },
        }
    }
</script>

<style>

</style>

 二、小程序

 

<template>
    <view>
        <!-- #ifdef MP-WEIXIN -->
        <button type="default" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true">小程序登录</button>
        <!-- #endif -->
    </view>
</template>

<script>
    export default {
        data() {
            return {

            }
        },
        onLoad(options) {
            console.log(options);
        },
        methods: {
            getUserInfo(res) {
                console.log(res);
                uni.login({
                    provider: 'weixin',
                    success: function(loginRes) {
                        console.log(loginRes);
                        // 获取用户信息
                        uni.getUserInfo({
                            provider: 'weixin',
                            success: function(infoRes) {
                                console.log('用户昵称为:' + infoRes.userInfo.nickName);
                            }
                        });
                    }
                });
            },
        }
    }
</script>

<style>

</style>

猜你喜欢

转载自www.cnblogs.com/yang-2018/p/12524195.html