<template>
    <view style="padding-top: 88rpx;">
        <view class="login-tip">时味苏州PHoneLogin 申请获得</view>
        <view class="login-tip2">以下权限</view>
        <view class="login-tip-box">
            <text>获得你的公开信息（昵称、头像、地区及性别）</text>
        </view>
        <view class="btn-box">
            <button bindtap="cancel" type="default" @click="redirectIndex">取消</button>
            <button type="primary" @click="getUserInfo">同意</button>
        </view>
        <view class="flex-center article-box">
            <radio-group @change="toggleAgreement">
                <radio value="1" :checked="isAgreed" style="transform:scale(0.7)"></radio>
            </radio-group>
            <view>同意<text @click="gotoInfo(9)">《用户服务协议》、</text><text @click="gotoInfo(12)">《隐私政策》</text></view>
        </view>
        <uni-popup ref="popup" type="bottom" background-color="#fff">
            <button type="default" open-type="getPhoneNumber" @getphonenumber="handlePhoneNumber" style="width: 100%;height: 12vh;line-height: 12vh;">点击授权手机号</button>
        </uni-popup>
    </view>
</template>

<script>
export default {
    name: "login",
    data() {
        return {
            isAgreed: false,
			openid: ''
        };
    },
    onLoad(options) {
        
    },
    methods: {
        // 处理手机号授权
        handlePhoneNumber(e) {
            if (e.detail.errMsg === "getPhoneNumber:ok") { // 成功
                this.$refs.popup.close();
                this.Post({
                    code: e.detail.code,
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    openid: this.openid
                }, '/api/uservice/user/bindPhoneNumber')
               .then(res => {
                    this.$store.commit('changeUserInfo', res.data);
                    this.navigateBasedOnPath();
                })
               .catch(error => {
                    console.error('绑定手机号失败:', error);
                    uni.showToast({
                        title: '绑定手机号失败，请稍后重试',
                        icon: 'none'
                    });
                });
            } else {
                console.error('获取手机号失败:', e.detail.errMsg);
                uni.showToast({
                    title: '获取手机号失败，请稍后重试',
                    icon: 'none'
                });
            }
        },
        // 跳转到协议页面
        gotoInfo(id) {
            uni.navigateTo({
                url: '/subPackages/user/privacyInfo?id=' + id
            });
        },
        // 切换协议同意状态
        toggleAgreement() {
            this.isAgreed = !this.isAgreed;
        },
        // 重定向到首页
        redirectIndex() {
            uni.switchTab({
                url: '/pages/index/index',
            });
        },
        // 获取用户信息
        getUserInfo() {
            if (!this.isAgreed) {
                uni.showToast({
                    title: '请先勾选同意《用户服务协议》、《隐私政策》',
                    icon: 'none'
                });
                return;
            }
			let data = {
				avatar: "https://jdsz.obs.cn-east-3.myhuaweicloud.com:443/backend/upload/202509281759047643d42d0a25de93f030757727b6a74a999c2f5f0426.png",
				birthday: "2020-02-05",
				createtime: 1760087570,
				email: "",
				expires_in: 2592000,
				expiretime: 1762679570,
				from_source: "WECHAT",
				fullname: "",
				gender: 1,
				id: 6069705,
				id_number: "",
				isBindMobile: 1,
				is_enterprise: 2,
				mobile: "19941952818",
				name_verification: "UNCERTFIED",
				nickname: "abc",
				score: 106,
				token: "c04a0d8e-b2dd-4a47-a2e8-0a7652520bad",
				user_id: 6069705,
				username: "",
				uuid: "3c6f7d59-5c27-42ad-902b-89043fc4b4f8",
			}
            this.$store.commit('changeUserInfo', data);
        },
        // 根据路径进行导航
        navigateBasedOnPath() {
            if (this.$store.state.user.toPath.includes('user/user')) {
                uni.switchTab({
                    url: this.$store.state.user.toPath
                });
            } else {
                uni.navigateBack({});
            }
        }
    },
};
</script>

<style>
.login-tip {
    font-size: 28rpx;
    margin: 0 60rpx;
    margin-top: 40rpx;
}

.login-tip2 {
    font-size: 44rpx;
    margin: 0rpx 60rpx;
    margin-top: 20rpx;
    font-weight: 400;
}

.login-tip-box {
    display: flex;
    align-items: flex-start;
    margin: 0 60rpx;
    font-size: 28rpx;
    margin-top: 40rpx;
    line-height: 40rpx;
}

.login-tip-box .icon-gou1 {
    line-height: 80rpx;
    margin-top: -20rpx;
    margin-right: 30rpx;
    color: #666;
}

.btn-box {
    display: flex;
    position: absolute;
    bottom: 100rpx;
    left: 0;
    right: 0;
}

.btn-box button {
    width: 400rpx;
}

.article-box {
    position: absolute;
    left: 0;
    right: 0;
    justify-content: center;
    font-size: 24rpx;
    color: #1aad19;
    bottom: 40rpx;
}

.article-box .iconfont {
    margin-right: 10rpx;
    font-size: 26rpx;
}

.article-box text {
    border-bottom: 1px solid;
}
</style>