Merge branch 'master' of https://gitea.zianxin.com/zax/biz-mobile-react-cloud
This commit is contained in:
commit
0ff5f79b8d
4 files changed
+1353
-3
No files matched your search
@@ -74,7 +74,11 @@
|
||||
</tm-form-item>
|
||||
<view class="sub-text-20 c-red! mb-2">注:图片大小不超过5M</view>
|
||||
<tm-form-item name="description" label="详情描述" direction="vertical" required="">
|
||||
<tm-input v-model="form.description" type="textarea" :showClear="false" placeholder="请输入详情描述"></tm-input>
|
||||
<rich-text-input
|
||||
v-model="form.description"
|
||||
placeholder="点击编辑详情描述..."
|
||||
label="详情描述"
|
||||
/>
|
||||
</tm-form-item>
|
||||
<tm-form-item name="priceType" label="定价方式">
|
||||
<tm-radio-group v-model="form.priceType" align="flex-end">
|
||||
@@ -168,6 +172,7 @@ import { reactive, ref } from "vue"
|
||||
import { hooks } from "@@/index"
|
||||
import { prosperityShopApi } from "@/sheep/api"
|
||||
import ShopCategaryPicker from "@/sheep/components/shop-categary-picker.vue"
|
||||
import RichTextInput from "@/sheep/components/rich-text-input/rich-text-input.vue"
|
||||
import { onLoad } from "@dcloudio/uni-app"
|
||||
import { filePath } from "@/sheep/utils/file"
|
||||
|
||||
@@ -233,6 +238,12 @@ const getGoodsDetail = async (id: string) => {
|
||||
const saleEndTime = data.saleEndTime
|
||||
_saleStartTimeStr.value = saleStartTime
|
||||
_saleEndTimeStr.value = saleEndTime
|
||||
|
||||
// 转换HTML为Markdown格式
|
||||
const description = convertHtmlToMarkdown(data.description || '')
|
||||
console.log('原始HTML:', data.description)
|
||||
console.log('转换后Markdown:', description)
|
||||
|
||||
Object.assign(form, {
|
||||
...data,
|
||||
status: data.status.toString(),
|
||||
@@ -242,10 +253,89 @@ const getGoodsDetail = async (id: string) => {
|
||||
saleStartTime,
|
||||
saleEndTime,
|
||||
skuList,
|
||||
description,
|
||||
})
|
||||
uni.hideLoading()
|
||||
}
|
||||
|
||||
// HTML转Markdown函数
|
||||
const convertHtmlToMarkdown = (html: string): string => {
|
||||
if (!html) return ''
|
||||
|
||||
// 移除HTML标签,保留文本内容
|
||||
let markdown = html
|
||||
// 处理标题
|
||||
.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n')
|
||||
.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n')
|
||||
.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n')
|
||||
.replace(/<h4[^>]*>(.*?)<\/h4>/gi, '#### $1\n')
|
||||
.replace(/<h5[^>]*>(.*?)<\/h5>/gi, '##### $1\n')
|
||||
.replace(/<h6[^>]*>(.*?)<\/h6>/gi, '###### $1\n')
|
||||
// 处理段落
|
||||
.replace(/<p[^>]*>(.*?)<\/p>/gi, '$1\n\n')
|
||||
// 处理换行
|
||||
.replace(/<br\s*\/?>/gi, '\n')
|
||||
// 处理粗体
|
||||
.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**')
|
||||
.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**')
|
||||
// 处理斜体
|
||||
.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*')
|
||||
.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*')
|
||||
// 处理下划线
|
||||
.replace(/<u[^>]*>(.*?)<\/u>/gi, '__$1__')
|
||||
// 处理链接
|
||||
.replace(/<a[^>]*href=["']([^"']*)["'][^>]*>(.*?)<\/a>/gi, '[$2]($1)')
|
||||
// 处理图片 - 更全面的图片处理
|
||||
.replace(/<img[^>]*src=["']([^"']*)["'][^>]*alt=["']([^"']*)["'][^>]*>/gi, (match, src, alt) => {
|
||||
// 直接使用原始URL,不添加额外前缀
|
||||
console.log('HTML图片转换 - 原始:', match)
|
||||
console.log('HTML图片转换 - src:', src)
|
||||
console.log('HTML图片转换 - alt:', alt)
|
||||
return ``
|
||||
})
|
||||
.replace(/<img[^>]*src=["']([^"']*)["'][^>]*>/gi, (match, src) => {
|
||||
// 直接使用原始URL,不添加额外前缀
|
||||
console.log('HTML图片转换 - 原始:', match)
|
||||
console.log('HTML图片转换 - src:', src)
|
||||
return ``
|
||||
})
|
||||
// 处理列表
|
||||
.replace(/<ul[^>]*>(.*?)<\/ul>/gis, (match, content) => {
|
||||
return content.replace(/<li[^>]*>(.*?)<\/li>/gi, '- $1\n')
|
||||
})
|
||||
.replace(/<ol[^>]*>(.*?)<\/ol>/gis, (match, content) => {
|
||||
let index = 1
|
||||
return content.replace(/<li[^>]*>(.*?)<\/li>/gi, () => `${index++}. $1\n`)
|
||||
})
|
||||
// 处理引用
|
||||
.replace(/<blockquote[^>]*>(.*?)<\/blockquote>/gis, (match, content) => {
|
||||
return content.replace(/\n/g, '\n> ') + '\n'
|
||||
})
|
||||
// 处理代码
|
||||
.replace(/<code[^>]*>(.*?)<\/code>/gi, '`$1`')
|
||||
.replace(/<pre[^>]*>(.*?)<\/pre>/gis, '```\n$1\n```')
|
||||
// 处理分割线
|
||||
.replace(/<hr[^>]*>/gi, '---\n')
|
||||
// 处理表格(简单处理)
|
||||
.replace(/<table[^>]*>(.*?)<\/table>/gis, (match, content) => {
|
||||
// 这里可以添加更复杂的表格处理逻辑
|
||||
return content.replace(/<tr[^>]*>(.*?)<\/tr>/gi, (trMatch, trContent) => {
|
||||
const cells = trContent.match(/<t[hd][^>]*>(.*?)<\/t[hd]>/gi)
|
||||
if (cells) {
|
||||
return '| ' + cells.map(cell => cell.replace(/<[^>]*>/g, '')).join(' | ') + ' |\n'
|
||||
}
|
||||
return ''
|
||||
})
|
||||
})
|
||||
// 移除剩余的HTML标签
|
||||
.replace(/<[^>]*>/g, '')
|
||||
// 清理多余的换行
|
||||
.replace(/\n{3,}/g, '\n\n')
|
||||
.trim()
|
||||
|
||||
return markdown
|
||||
}
|
||||
|
||||
const getGoodsCategory = async () => {
|
||||
const { code, data, msg } = await prosperityShopApi.getGoodsCategory()
|
||||
if (code != 0) return uni.showToast({ title: msg || "商品分类获取失败", icon: "none" })
|
||||
@@ -287,10 +377,50 @@ const addSkuListItem = () => {
|
||||
})
|
||||
}
|
||||
|
||||
// Markdown转HTML函数
|
||||
const convertMarkdownToHtml = (markdown: string): string => {
|
||||
if (!markdown) return ''
|
||||
|
||||
let html = markdown
|
||||
// 处理标题
|
||||
.replace(/^# (.*$)/gim, '<h1>$1</h1>')
|
||||
.replace(/^## (.*$)/gim, '<h2>$1</h2>')
|
||||
.replace(/^### (.*$)/gim, '<h3>$1</h3>')
|
||||
.replace(/^#### (.*$)/gim, '<h4>$1</h4>')
|
||||
.replace(/^##### (.*$)/gim, '<h5>$1</h5>')
|
||||
.replace(/^###### (.*$)/gim, '<h6>$1</h6>')
|
||||
// 处理粗体
|
||||
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
|
||||
// 处理斜体
|
||||
.replace(/\*(.*?)\*/g, '<em>$1</em>')
|
||||
// 处理链接
|
||||
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>')
|
||||
// 处理图片
|
||||
.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1" style="max-width: 100%; height: auto;" />')
|
||||
// 处理段落
|
||||
.replace(/^(?!<[h1-6]|<img|<ul|<ol|<li)(.+)$/gim, '<p>$1</p>')
|
||||
// 处理换行
|
||||
.replace(/\n/g, '<br>')
|
||||
|
||||
return html
|
||||
}
|
||||
|
||||
// 保存
|
||||
const onsubmit = async (evt: TM.FORM_SUBMIT_RESULT) => {
|
||||
// console.log("校验结果:", evt)
|
||||
console.log("提交的数据:", { ...form, coverImage: JSON.parse(form.coverImage[0].response).data.url })
|
||||
|
||||
// 将Markdown格式的description转换为HTML
|
||||
const htmlDescription = convertMarkdownToHtml(form.description)
|
||||
console.log("原始Markdown:", form.description)
|
||||
console.log("转换后HTML:", htmlDescription)
|
||||
|
||||
const submitData = {
|
||||
...form,
|
||||
description: htmlDescription, // 使用转换后的HTML
|
||||
coverImage: JSON.parse(form.coverImage[0].response).data.url
|
||||
}
|
||||
console.log("提交的数据:", submitData)
|
||||
|
||||
if (!evt.isPass) return
|
||||
|
||||
if (form.priceType == "1") {
|
||||
@@ -307,7 +437,11 @@ const onsubmit = async (evt: TM.FORM_SUBMIT_RESULT) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const params = { ...form, coverImage: JSON.parse(form.coverImage[0].response).data.url }
|
||||
const params = {
|
||||
...form,
|
||||
description: htmlDescription, // 使用转换后的HTML
|
||||
coverImage: JSON.parse(form.coverImage[0].response).data.url
|
||||
}
|
||||
uni.showLoading({ title: "提交中..." })
|
||||
const { code, msg } = await (form.id ? prosperityShopApi.editGoods(params) : prosperityShopApi.addGoods(params))
|
||||
if (code != 0) return uni.showToast({ title: msg || "提交失败", icon: "none" })
|
||||
|
||||
Reference in new issue
Block a user