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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM