小程序登陸並受權

HTML代碼: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>

  方法:ide

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
                    });
                })
            }
        
        
        
        }
    
相關文章
相關標籤/搜索