<template>
    <view class="content">
        <view class="headerImg">
            <image src="../../static/login_hd.png" mode="" />
        </view>
        <view class="login-btn-wrap">
            <button
                block
                style="border-radius: 46rpx"
                size="large"
                custom-class="login-btn"
                open-type="getPhoneNumber"
                @getphonenumber="onGetPhoneNumber"
            >
                同意协议并手机号快捷登录
            </button>
        </view>
        <view class="skip-login-btn" @click="noLogin()">暂不登录</view>
        <view class="funItem">
            <label class="simple-radio" @click="changeCheck()"
                ><radio
                    :value="serviceNumberFlag"
                    color="#000000"
                    icon-size="12px"
                    :checked="checked == true"
                    class="hide-original"
                    style="transform: scale(0.7)"
                />
            </label>
            <text style="font-size: 26rpx; color: #fff"
                >我已阅读并同意
                <text class="green" @click="dkwd('yhfwxy')">《用户协议》</text
                >和<text class="green" @click="dkwd('yszc')"
                    >《隐私协议》</text
                ></text
            >
        </view>
    </view>
</template>
<script>
export default {
    data() {
        return {
            checked: false,
            phone: false,
        };
    },
    computed: {},
    methods: {
        changeCheck(e) {
            this.checked = !this.checked;
        },
        dkwd(val) {
            let typeval = val;
            if (typeval == "yhfwxy") {
                uni.downloadFile({
                    url: "https://hflnxx.oss-cn-shanghai.aliyuncs.com/USERPROTOCOL/20250918/yhfwxy.pdf",
                    success: function (res) {
                        const filePath = res.tempFilePath;
                        wx.openDocument({
                            filePath: filePath,
                            success: function (res) {},
                        });
                    },
                });
            }
            if (typeval == "yszc") {
                uni.downloadFile({
                    url: "https://hflnxx.oss-cn-shanghai.aliyuncs.com/USERPROTOCOL/20250918/yszc.pdf",
                    success: function (res) {
                        const filePath = res.tempFilePath;
                        wx.openDocument({
                            filePath: filePath,
                            success: function (res) {},
                        });
                    },
                });
            }
        },
        onGetPhoneNumber(e) {
            if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
                uni.exitMiniProgram({
                    success(res) {},
                    fail(err) {},
                });
                return;
            }

            let userId = uni.getStorageSync("userId");
            if (!userId) {
                let code = "";
                // console.log(e.detail.code, 9999);
                if (e.detail) {
                    code = e.detail.code;
                    this.checked = true;
                } else {
                    code = e.code;
                    this.checked = true;
                }
                this.$http
                    .post(
                        "wap/user/wx/loginByPhone",
                        JSON.stringify({
                            openId: uni.getStorageSync("openid"),
                            unionId: uni.getStorageSync("unionid"),
                            code: code,
                        }),
                        {
                            header: {
                                "Content-Type":
                                    "application/json;charset=UTF-8",
                            },
                        }
                    )
                    .then((res) => {
                        if (res.data.data) {
                            uni.setStorageSync("phone", res.data.data.phone);
                            uni.setStorageSync(
                                "tokenValue",
                                res.data.data.tokenValue
                            );
                            uni.setStorageSync("userId", res.data.data.userId);

                            this.$initMqtt();
                            console.log("✅ 登录成功后 MQTT 开始登录连接");

                            uni.reLaunch({
                                url: "/pages/home/home",
                            });
                        }
                    });
                this.checked = false;
            } else {
                let code = "";
                // console.log(e.detail.code, 9999);
                if (e.detail) {
                    code = e.detail.code;
                    this.checked = true;
                } else {
                    code = e.code;
                    this.checked = true;
                }
                this.$http
                    .post(
                        "wap/user/wx/loginByPhone",
                        JSON.stringify({
                            openId: uni.getStorageSync("openid"),
                            unionId: uni.getStorageSync("unionid"),
                            code: code,
                        }),
                        {
                            header: {
                                "Content-Type":
                                    "application/json;charset=UTF-8",
                            },
                        }
                    )
                    .then((res) => {
                        if (res.data.data) {
                            uni.setStorageSync("phone", res.data.data.phone);
                            uni.setStorageSync(
                                "tokenValue",
                                res.data.data.tokenValue
                            );
                            uni.setStorageSync("userId", res.data.data.userId);
                            uni.reLaunch({
                                url: "/pages/home/home",
                            });
                        }
                    });
                this.checked = false;
            }
        },
        noLogin() {
            uni.clearStorageSync();
            uni.reLaunch({
                url: "/pages/home/home",
            });
        },
    },
    onLoad() {},
    onShow() {
        const updateManager = uni.getUpdateManager();
        updateManager.onCheckForUpdate(function (res) {
            if (res.hasUpdate) {
                updateManager.onUpdateReady(function () {
                    uni.showModal({
                        content: "小程序已更新",
                        showCancel: false,
                        success: function (res) {
                            updateManager.applyUpdate();
                        },
                    });
                });
            }
        });

        uni.login({
            success: (res) => {
                if (res.code) {
                    this.$http
                        .get("wap/user/loginWx", { code: res.code })
                        .then((res) => {
                            if (res.data.data) {
                                console.log(res.data.data, 9999);
                                uni.setStorageSync(
                                    "openid",
                                    res.data.data.openId
                                );
                                uni.setStorageSync(
                                    "unionid",
                                    res.data.data.unionId
                                );
                            }
                        });
                }
            },
        });
    },
    onShareAppMessage() {},
};
</script>
<style scoped>
.content {
    width: 100vw;
    height: 100vh;
    background-image: url("https://hflnxx.oss-cn-shanghai.aliyuncs.com/CAROUSEL/20250624/74f9d281.jpg");
    background-size: cover;
    /* 背景图等比例铺满 */
    background-repeat: no-repeat;
    background-position: center center;
}

.headerImg {
    width: 700rpx;
    margin: 0 auto;
    padding-top: 180rpx;
}

.headerImg image {
    width: 700rpx;
    height: 240rpx;
}

.login-btn-wrap {
    border-radius: 48rpx;
    margin: 850rpx 32rpx 0 32rpx;
}
.login-btn {
    font-size: 32rpx;
    height: 96rpx;
    border-radius: 48rpx;
    background: linear-gradient(
        107.53deg,
        #ffffff 0%,
        rgba(255, 255, 255, 0.65) 52.37%
    );
    color: #333;
    border: 2rpx solid #ccc;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
}

.input-check {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 40rpx;
    bottom: 100rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 26rpx;
    color: #999999;
}
.green {
    color: #000;
}
.funItem {
    display: flex;
    justify-content: center;
    align-items: center;
}
.funItem .simple-radio {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
}
.skip-login-btn {
    width: 200rpx;
    margin: 20rpx auto;
    color: #ccc;
    font-size: 15px;
    text-align: center;
}
</style>
