<template>
    <view class="init">
        <view class="window">
            <view class="getUserInfo">用户服务隐私协议</view>
            <view class="content">
                <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="btn" @click="getUserInfo">授权</button>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            nickName:'',
            avatarUrl:'',
            OpenId:''
        }
    },
    methods: {
        getUserInfo() {
            var that = this
            wx.login({
                success(res) {
                    console.log(res)
                    if (res.code) {
                        // 将 res.code 发送至后台服务器进行处理
                        uni.request({
                            url: 'https://admin.mindepoch.com:8089/wechat/AppletWeChatLogin',
                            method: 'POST',
                            header: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            },
                            data: { code: res.code },
                            success: function (response) {
                                console.log('登录成功', response);
                                that.OpenId = response.data.OpenId
                                // TODO: 根据后台返回的结果保存 openid、sessionKey 等信息

                                uni.getUserInfo({
                                    withCredentials: true,
                                    lang: 'zh_CN',
                                    success: function (userRes) {
                                        var userInfo = userRes.userInfo;
                                        console.log("用户昵称为：" + userInfo.nickName);
                                        console.log("用户头像地址为：" + userInfo.avatarUrl);
                                        that.nickName = userInfo.nickName
                                        that.avatarUrl = userInfo.avatarUrl
                                        that.$globalData.userPhoto = userInfo.avatarUrl
                                        // TODO: 将用户昵称和头像地址保存或上传至后台服务器

                                    },
                                    fail: function () {
                                        console.error('获取用户信息失败');
                                    }
                                });
                            }
                        })
                    } else {
                        console.error('登录失败！' + res.errMsg);
                    }
                }
            });
        },

        // 获取手机号
        getPhoneNumber(e) {
            console.log('e',e.detail.code)
            if(e.detail.code){
                this.sendPhoneNumberToServer(e.detail.code,'no');
            }else{
                this.sendPhoneNumberToServer(e.detail.code,'yes');
            }
        },
        sendPhoneNumberToServer(code,bool) {
            // 将code、encryptedData和iv发送到后端API
            uni.request({
                url: 'https://admin.mindepoch.com:8089/wechat/RegisterAndLoginWXUserPhone',
                method: 'POST',
                data:{
                    username:this.nickName,
                    openid:this.OpenId,
                    phone_code:bool=="no"?code:'',
                    reject_binding:bool
                },
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                success: (res) => {
                    console.log('success',res)
                    if (res.statusCode === 200) {
                        console.log('手机号码传递成功', res);

                        if(res.data.code===1){
                            this.$globalData.wxUid = res.data.wxUid
                            uni.setStorageSync('session_token', res.cookies[0].split(';')[0]); //保存cookie值
                            uni.navigateTo({
                                url: `/pages/homeCards/index`
                            });
                        }
                    } else {
                        console.log('服务器返回状态码异常', res.statusCode);
                    }
                },
                fail: (err) => {
                    console.log('发送失败', err);
                }
            });
        }
    }
}
</script>


<style scoped>
.init {
    width: 100vw;
    height: 100vh;
    background-color: rgb(146, 108, 126);
}

.window {
    width: 80vw;
    height: 300rpx;
    background-color: aliceblue;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10rpx;
    font-size: 40rpx;
    box-shadow: 0 0 20rpx #fff;
}

.getUserInfo {
    text-align: center;
    padding: 20px;
}

.content {
    display: flex;
    margin-top: 50rpx;
}

.btn {
    width: 200rpx;
    height: 80rpx;
    line-height: 80rpx;
    font-size: 34rpx;
}
</style>