<template>
    <view class="container">
        <!-- 加载H5认证页面 -->
        <web-view
            :src="faceUrl"
            @message="handleMessage"
            @error="onWebviewError"
        ></web-view>

        <!-- 加载状态 -->
        <view v-if="loading" class="loading-mask">
            <text>正在加载认证页面...</text>
        </view>
    </view>
</template>

<script>
import {authApi} from "@/api/auth";

export default {
    data() {
        return {
            faceUrl: '',
            bizId: '',
            serialNo: '',
            loading: true
        }
    },
    onLoad(options) {
        // 接收传入的认证URL
        if (!options.bizId || options.bizId === '') {
            uni.showToast({ title: '认证链接错误', icon: 'none' })
            uni.navigateBack()
            return;
        }

        if (!options.serialNo || options.serialNo === '') {
            uni.showToast({ title: '认证链接错误', icon: 'none' })
            uni.navigateBack()
            return;
        }

        this.bizId = options.bizId
        this.serialNo = options.serialNo
        if (options.faceUrl) {
            this.faceUrl = decodeURIComponent(options.faceUrl)
            this.loading = false
        } else {
            uni.showToast({ title: '认证链接错误', icon: 'none' })
            uni.navigateBack()
        }
    },
    methods: {
        // 接收H5页面消息
        async handleMessage(e) {
            let vm = this
            const [message] = e.detail.data

            console.log('收到H5消息:', JSON.parse(JSON.stringify(message)))

            try {
                if (message.code === 100000) {
                    message['bizId'] = this.bizId;
                    message['serialNo'] = this.serialNo;

                    //活体成功
                    //三要素
                    authApi.threeElementApi(message).then(res=> {
                        if (res.code === 0) {
                            uni.navigateBack({ delta: 1 }); // 返回两级页面
                        }
                    })

                }else{
                    uni.showModal({
                        title: '认证失败',
                        content: message.msg,
                        showCancel: false
                    })
                }



             } catch (error) {
                this.$emit('child-event', {
                    aaa:"eeee",
                    bizId:vm.bizId,
                    serialNo:vm.serialNo
                });

                console.log('异常：',error)
                uni.showToast({ title: '结果验证失败', icon: 'none' })
            }
        },

        // 处理加载失败
        onWebviewError() {
            this.loading = false
            uni.showModal({
                title: '提示',
                content: '认证页面加载失败，请检查网络后重试',
                showCancel: false,
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }
}
</script>

<style scoped>
.loading-mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255,255,255,0.9);
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
