小程序登录并授权

HTML代码:

<view>
		<view class="content">
			<image src="../../static/jwj.png"></image>
			<view>金万家申请获得以下授权:</view>
		</view>
		<view class="text">获得你的公开信息(昵称、头像等)</view>
		<button open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGotUserInfo">立即进入</button>
	</view>

  方法:

onLoad() {
            this.baseURL = this.$req.base_url;
            try {
                let value = uni.getStorageSync('token') || '';
                if (!value) {
                    this.handle_login();
                    
                } else {
                    // this.onGotUserInfo();
                    // this.login_success = false;
                    // this.get_info(1);
                    uni.switchTab({
                        url:"../index/index"
                    })
                }
            } catch (e) {
                console.log(e)
            }
        },
        methods: {
            handle_login() {
                //授权登陆
                uni.login({
                    provider: 'weixin',
                    scopes: 'auth_user',
                    success: (loginRes) => {
                        console.log(loginRes);
                        
                        this.code = loginRes.code;
                    },
                    fail: (err) => {
                        uni.showToast({
                            icon: "none",
                            title: '请稍后重试',
                            duration: 1500
                        });
                    }
                });
            },
            onGotUserInfo: function(e) {
                // console.log(e.detail.userInfo)
                console.log(e.detail)
                if (e.detail.errMsg == "getUserInfo:fail auth deny") {
                    return false;
                }
                let infoRes = e.detail;
                console.log(infoRes);
                let nickname = infoRes.userInfo.nickName, //昵称
                    gender = infoRes.userInfo.gender, //性别
                    code = this.code, //换取openid
                    avatarUrl = infoRes.userInfo.avatarUrl, //头像
                    url = "/wechet/login/login";
                uni.setStorage({
                    key: 'user_info',
                    data: infoRes.rawData,
                    success: function() {
                        console.log('success');
                    }
                });
                this.$req.post(url,{
                    code,
                    nickname,
                    gender,
                    code,
                    avatarUrl
                }).then(res => {
                    console.log(res)
                    uni.setStorage({
                        key: 'token',
                        data: res.data,
                        success: (res) => {
                            console.log(res);
                            // this.get_info();
                            uni.showToast({
                                icon: "none",
                                title: '登录成功',
                                duration: 1500
                            });
                        }
                    });
                    uni.switchTab({
                        url:"../index/index"
                    })
                }).catch(err => {
                    console.log(err)
                    uni.showToast({
                        icon: "none",
                        title: '请稍后重试',
                        duration: 1500
                    });
                })
            }
        
        
        
        }
    

 

 
posted @ 2019-07-30 14:25  爱学习的土豆  阅读(329)  评论(0编辑  收藏  举报