吉安感知网项目-前端
shuishen
2026-01-14 052d0e8bd9c1d8e0d243a166bedf761fc5a7a891
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<template>
    <view class="regulationsDetail" @click="handleRetry">
        <view class="title">
            {{ title }}
        </view>
        <!-- <u-parse 
            :content="htmlContent"
            :tagStyle="tagStyles"
            @preview="handlePreview"
            @navigate="handleNavigate"
            /> -->
        <!-- <view v-for="item in fileList" :key="item.id">
            <image :src="fjSvg" class="fj-icon" />
            <view class="fj-name">{{ item.fileName }}</view>
            <view class="fj-size">{{ item.createTime }}</view>
        </view> -->
    </view>
</template>
<script setup>
    import fjSvg from '@/components/fj.svg'
import { ref, onMounted, onUnmounted } from 'vue'
import { regulationsDetailApi } from '@/api/index'
const formParams = ref({
    id: '',
})
const htmlContent = ref('<p>暂无数据</p>')
const title = ref('')
const tagStyles = ref({})
onMounted(() => {
    console.log('onMounted 开始执行')
    try {
        // 获取当前页面(最后一个元素)而不是第一个页面
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        
        // 尝试获取ID参数
        const id = currentPage.options.id
        const type = currentPage.options.type
        
        if (id) {
            formParams.value.id = id
            // 设置默认标题
            title.value = '法律政策详情'
            
            // 使用setTimeout延迟执行API调用,避免页面跳转时的网络请求冲突
            getRegulationsDetail()
        } else {
            console.error('未获取到ID参数')
            title.value = '法律政策详情'
            htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">未获取到有效ID参数</div>'
        }
    } catch (error) {
        console.error('获取ID失败:', error)
        title.value = '法律政策详情'
        htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">页面加载失败,请稍后重试</div>'
    }
})
const fileList = ref([]) // 附件列表
// API调用超时处理函数
const fetchWithTimeout = async (promise, timeout = 10000) => {
    const timeoutPromise = new Promise((_, reject) => {
        setTimeout(() => reject(new Error('网络请求超时,请稍后重试')), timeout)
    })
    return Promise.race([promise, timeoutPromise])
}
 
const handleRetry = () => {
    // 只有在加载失败时才允许重试
    if (htmlContent.value.includes('超时') || htmlContent.value.includes('失败')) {
        console.log('用户点击重试')
        getRegulationsDetail()
    }
}
 
const getRegulationsDetail = async () => {
    if (!formParams.value.id) {
        console.error('ID为空,不执行API调用')
        htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">未获取到有效ID参数</div>'
        return
    }
    
    // 显示加载状态
    htmlContent.value = '<div style="padding: 40px; text-align: center; color: #999;">加载中...</div>'
    
    try {
        // 调用API并设置10秒超时
        const res = await fetchWithTimeout(regulationsDetailApi(formParams.value.id), 10000)
        
        // 检查返回数据结构
        if (res && res.data) {
            if (res.data.code === 200) {
                // 法律法规提供的是地址附件
                htmlContent.value = res.data.data.noticeContent || '<div style="padding: 20px; text-align: center; color: #999;">暂无数据</div>'
                title.value = res.data.data.noticeTitle || '法律政策详情'
                fileList.value = res.data.data.fileList || []
            } else {
                console.error('API返回错误:', res.data.msg || '未知错误')
                htmlContent.value = `<div style="padding: 20px; text-align: center; color: #999;">${res.data.msg || '获取数据失败,请点击屏幕重试'}</div>`
            }
        } else {
            console.error('API返回数据结构不正确:', res)
            htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">数据格式错误,请点击屏幕重试</div>'
        }
    } catch (error) {
        console.error('获取详情异常:', error)
        // 根据错误类型显示不同的提示
        if (error.message.includes('timeout')) {
            htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">网络连接超时,请点击屏幕重试</div>'
        } else {
            htmlContent.value = '<div style="padding: 20px; text-align: center; color: #999;">加载失败,请点击屏幕重试</div>'
        }
    }
}
</script>
<style scoped lang="scss">
.regulationsDetail {
    padding: 24rpx;
    .title {
        font-family: Source Han Sans CN, Source Han Sans CN;
        font-weight: 500;
        font-size: 18px;
        color: #1D2129;
        margin-bottom: 40rpx;
    }
}
</style>