处理附件渲染失败问题

This commit is contained in:
2026-06-24 16:15:44 +08:00
parent ba0585c8aa
commit abe464066e

View File

@@ -7,9 +7,19 @@
:show-with-animation="true" :show-with-animation="true"
:selectable="true"></u-parse> :selectable="true"></u-parse>
</view> </view>
<u-swiper :list="imgList" @change="change"></u-swiper> <u-swiper :list="imgList" v-if="imgList.length"></u-swiper>
<view v-for="(item,index) in attachments" :key="index"> <view class="attachment-section" v-if="attachmentList.length">
<text @click="download(item)">{{ item.fileName }}</text> <view class="section-title">
<text class="title-icon">📎</text>
<text>附件下载</text>
</view>
<view class="attachment-item" v-for="(item,index) in attachmentList" :key="index" @tap="download(item)">
<view class="file-info">
<text class="file-icon">📄</text>
<text class="file-name">{{ item.fileName || '附件' + (index + 1) }}</text>
</view>
<text class="download-btn">下载</text>
</view>
</view> </view>
</view> </view>
</template> </template>
@@ -32,27 +42,30 @@
onShow(){ onShow(){
this.recordView(this.id) this.recordView(this.id)
}, },
computed: {
attachmentList() {
return this.attachments || []
}
},
methods:{ methods:{
recordView(assetId) { recordView(assetId) {
let token = this.$getToken() let token = this.$getToken()
let userInfo = uni.getStorageSync('userInfo') let userInfo = uni.getStorageSync('userInfo')
if(token){ if (!token) {
return return
} }
if (token) {
let url = "/potential/add"; let url = "/potential/add";
this.$u.get(url, { this.$u.get(url, {
assetId: this.id, assetId: this.id,
assetsName: '查看公告' + this.title, assetsName: '查看公告' + this.title,
userType: userInfo.userType userType: userInfo.userType
}, { }, {
'WT': this.$getToken() 'WT': token
}).then(obj => { }).then(() => {
}).catch(err => { }).catch(err => {
console.log("记录客户浏览记录失败", err) console.log("记录客户浏览记录失败", err)
}) })
}
}, },
getDetail(){ getDetail(){
this.$u.get(`/notice/detail?id=${this.id}`,{},{}).then(res=>{ this.$u.get(`/notice/detail?id=${this.id}`,{},{}).then(res=>{
@@ -65,19 +78,57 @@
_this.imgList.push(_this.$config.staticUrl + img) _this.imgList.push(_this.$config.staticUrl + img)
}) })
} }
if(res.data.attachments) {
this.attachments = res.data.attachments; this.attachments = res.data.attachments;
}
this.recordView(this.id) this.recordView(this.id)
} }
}) })
}, },
download(item){ download(item){
const url = this.$config.staticUrl + item.fileUrl
console.log('下载地址:', url)
uni.showLoading({ title: '下载中...' })
uni.downloadFile({ uni.downloadFile({
url: this.$config.staticUrl + item.fileUrl, url: url,
success(res) { success(res) {
console.log('下载结果:', res)
uni.hideLoading()
if (res.statusCode === 200) {
// 保存文件到本地
uni.saveFile({
tempFilePath: res.tempFilePath,
success(saveRes) {
uni.showModal({
title: '下载成功',
content: '文件已保存,是否打开?',
success(modalRes) {
if (modalRes.confirm) {
uni.openDocument({ uni.openDocument({
filePath: res.tempFilePath filePath: saveRes.savedFilePath,
fail(err) {
console.log('打开文件失败:', err)
uni.showToast({ title: '无法打开此类型文件', icon: 'none' })
}
}) })
} }
}
})
},
fail(err) {
console.log('保存文件失败:', err)
uni.showToast({ title: '保存失败', icon: 'none' })
}
})
} else {
uni.showToast({ title: '下载失败', icon: 'none' })
}
},
fail(err) {
console.log('下载失败:', err)
uni.hideLoading()
uni.showToast({ title: '下载失败', icon: 'none' })
}
}) })
} }
} }
@@ -98,5 +149,72 @@
line-height: 1.8; line-height: 1.8;
white-space: pre-wrap !important; white-space: pre-wrap !important;
} }
.attachment-section {
margin: 20rpx 24rpx;
padding: 24rpx;
background-color: #f8f9fa;
border-radius: 16rpx;
}
.section-title {
display: flex;
align-items: center;
margin-bottom: 20rpx;
font-size: 30rpx;
font-weight: 600;
color: #333;
}
.title-icon {
margin-right: 10rpx;
}
.attachment-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20rpx 24rpx;
margin-bottom: 16rpx;
background-color: #fff;
border-radius: 12rpx;
box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
&:last-child {
margin-bottom: 0;
}
&:active {
background-color: #f0f0f0;
}
}
.file-info {
display: flex;
align-items: center;
flex: 1;
}
.file-icon {
margin-right: 16rpx;
font-size: 36rpx;
}
.file-name {
font-size: 28rpx;
color: #333;
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.download-btn {
padding: 10rpx 24rpx;
font-size: 24rpx;
color: #fff;
background-color: #e74c3c;
border-radius: 8rpx;
}
</style> </style>