zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    如何勾选 servlet如何获取?
    过滤器 如何实现获取不到用户名跳转回登录界面
    验证码
    cookie保存用户名及密码
    游标
    存储过程和自定义函数的区别
    瞎搞
    sql 试图索引
    sql 常用函数
    sql 简单的定义变量 声明 输出
  • 原文地址:https://www.cnblogs.com/yang-2018/p/12524195.html
Copyright © 2011-2022 走看看