<template>
    <!-- index.wxml -->
    <view class="wrap">
        <view class="wrap_nr">
            <image class="user_thumb" :src="userInfo.thumb"></image>
            <view class="user_name">您好，{{ userInfo.nickname }}</view>
            <view class="tips">为了您的账户安全，请绑定手机号</view>
            <button class="custom_btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
                <van-icon name="wechat" size="24" color="#ffffff" />
                微信手机号一键绑定
            </button>
            <view class="single" @tap="toPhoneLogin">使用其他手机号</view>
        </view>
    </view>
    <!-- loading -->
    <!-- <van-overlay show="{{ loading }}" z-index="100">
	<van-loading custom-class="custom_loading" />
</van-overlay> -->
</template>

<script>
// index.js
let myPro = require('../../../utils/wxRequest.js');

let util = require('../../../utils/util.js');

export default {
    data() {
        return {
            // 全局loading
            loading: false,

            userInfo: {
                thumb: '',
                nickname: ''
            },

            store: ''
        };
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
        let that = this;
        that.setData({
            userInfo: getApp().globalData.userInfo
        }); // console.log('用户信息',that.data.userInfo)
        // 店铺

        uni.getStorage({
            key: 'store',
            success: function (res) {
                that.setData({
                    store: JSON.parse(res.data)
                });
            }
        });
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
    },
    onResize: function () {
        // Do something when page resize
    },
    methods: {
        // 手机号授权
        getPhoneNumber(event) {
            let that = this;

            if (event.detail.errMsg == 'getPhoneNumber:ok') {
                console.log('手机号授权', event); // return;

                let params = {
                    openid: getApp().globalData.openid,
                    session_key: getApp().globalData.session_key,
                    encryptedData: event.detail.encryptedData,
                    iv: event.detail.iv,
                    nickname: getApp().globalData.userInfo.nickname,
                    sex: getApp().globalData.userInfo.sex,
                    thumb: getApp().globalData.userInfo.thumb,
                    store_id: that.store ? that.store.id : 1 // 门店id
                };
                myPro
                    .wxRequest('user/v2/wxlogin', 'POST', params)
                    .then((res) => {
                        // 存下token
                        getApp().globalData.token = res.result.token;
                        uni.setStorage({
                            key: 'token',
                            data: res.result.token
                        }); // 存在会员

                        getApp().globalData.is_member = res.result.user.is_member; // 会员 0否 1是
                        // 存下角色

                        if (res.result.user.store) {
                            getApp().globalData.role = 1; // 有店铺,即为门店
                        }

                        uni.reLaunch({
                            url: '/pages/index/index'
                        });
                    })
                    .catch((err) => {
                        console.log('报错信息', err);
                        uni.showToast({
                            title: err,
                            icon: 'none'
                        });
                    });
            }
        },

        // 去手机号登录
        toPhoneLogin() {
            let that = this;
            uni.navigateTo({
                url: '/pages/login/phone/index'
            });
        }
    }
};
</script>
<style>
/**index.wxss**/
.wrap_nr {
    padding-top: 200rpx;
    text-align: center;
}
.user_thumb {
    width: 148rpx;
    height: 148rpx;
    border-radius: 50%;
}
.user_name {
    padding-top: 50rpx;
    font-size: 40rpx;
    color: #333333;
}
.tips {
    padding-top: 20rpx;
    font-size: 28rpx;
    color: rgba(51, 51, 51, 0.54);
}
.custom_btn {
    width: 604rpx !important;
    height: 90rpx;
    padding: 0;
    margin: 300rpx auto 0 auto;
    line-height: unset;
    font-weight: normal;
    background: #295c56;
    border-radius: 45rpx;
    font-size: 34rpx;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.custom_btn .van-icon {
    /* width: 37rpx;
	height: 30rpx;
	background: #FFFFFF; */
    margin-right: 10rpx;
}
.single {
    margin-top: 50rpx;
    font-size: 26rpx;
    color: rgba(0, 0, 0, 0.54);
}
</style>
