This commit is contained in:
浅念
2025-12-13 17:30:18 +08:00
parent 5112803313
commit c60dda98a4
8 changed files with 356 additions and 182 deletions
+71 -4
View File
@@ -40,9 +40,7 @@
<input class="uni-input" focus v-model="form.price" placeholder="请输入折扣价格" />
</view>
<view class="detils">
<view v-html="productcontents">
</view>
<rich-text :nodes="productcontents"></rich-text>
</view>
</view>
@@ -182,10 +180,35 @@ export default {
}
})
},
// 处理富文本HTML,让图片自适应
processRichText(html) {
if (!html) return ''
// 使用正则表达式给所有 img 标签添加样式
return html.replace(/<img([^>]*)>/gi, (match, attrs) => {
// 检查是否已有 style 属性
if (attrs.includes('style=')) {
// 如果已有 style,添加 max-width
return match.replace(/style=["']([^"']*)["']/i, (styleMatch, styleContent) => {
// 移除已有的 max-width 和 width
let newStyle = styleContent.replace(/max-width\s*:\s*[^;]+;?/gi, '').replace(/width\s*:\s*[^;]+;?/gi, '')
newStyle = newStyle.trim()
if (newStyle && !newStyle.endsWith(';')) {
newStyle += ';'
}
newStyle += 'max-width: 100%; height: auto;'
return `style="${newStyle}"`
})
} else {
// 如果没有 style,添加 style 属性
return `<img${attrs} style="max-width: 100%; height: auto;">`
}
})
},
selectCategory(item){
this.form.category = item.model || ''
this.form.sku_id = item.id || ''
this.productcontents = item.desc || ''
// 处理富文本,让图片自适应
this.productcontents = this.processRichText(item.desc || '')
console.log(this.productcontents)
this.showSelect1 = false
},
@@ -337,6 +360,42 @@ export default {
this.show = false
},
submitForm(){
// 必填校验
if(!this.form.category || !this.form.sku_id){
uni.showToast({
title: '请选择型号',
icon: 'none'
});
return
}
if(!this.form.content || !this.form.content.trim()){
uni.showToast({
title: '请输入文字内容',
icon: 'none'
});
return
}
if(!this.form.agename || !this.form.age){
uni.showToast({
title: '请选择样机新旧程度',
icon: 'none'
});
return
}
if(!this.form.price || !this.form.price.trim()){
uni.showToast({
title: '请输入折扣价格',
icon: 'none'
});
return
}
if(!this.form.media || this.form.media.length === 0){
uni.showToast({
title: '请至少上传一张图片',
icon: 'none'
});
return
}
if(!this.agree){
uni.showToast({
title: '请先同意发布协议!',
@@ -373,6 +432,14 @@ export default {
<style lang="scss">
.detils{
margin-top: 40rpx;
overflow: hidden;
word-wrap: break-word;
word-break: break-all;
}
// 确保富文本中的图片自适应
.rich-wrap{
width: 100%;
overflow: hidden;
}
.cent{
display: flex;