index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <div>
  3. <el-upload
  4. :http-request="customUpload"
  5. :before-upload="handleBeforeUpload"
  6. :on-error="handleUploadError"
  7. name="file"
  8. :show-file-list="false"
  9. class="editor-img-uploader"
  10. v-if="type == 'url'"
  11. >
  12. <i ref="uploadRef" class="editor-img-uploader"></i>
  13. </el-upload>
  14. </div>
  15. <div class="editor">
  16. <quill-editor
  17. ref="quillEditorRef"
  18. v-model:content="content"
  19. contentType="html"
  20. @textChange="(e) => $emit('update:modelValue', content)"
  21. :options="options"
  22. :style="styles"
  23. />
  24. </div>
  25. </template>
  26. <script setup>
  27. import axios from 'axios'
  28. import { QuillEditor } from "@vueup/vue-quill"
  29. import "@vueup/vue-quill/dist/vue-quill.snow.css"
  30. import { getToken } from "@/utils/auth"
  31. import request from "@/utils/request"
  32. const { proxy } = getCurrentInstance()
  33. const quillEditorRef = ref()
  34. const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload") // 保留作为备用
  35. const headers = ref({
  36. Authorization: "Bearer " + getToken()
  37. })
  38. const props = defineProps({
  39. /* 编辑器的内容 */
  40. modelValue: {
  41. type: String,
  42. },
  43. /* 高度 */
  44. height: {
  45. type: Number,
  46. default: null,
  47. },
  48. /* 最小高度 */
  49. minHeight: {
  50. type: Number,
  51. default: null,
  52. },
  53. /* 只读 */
  54. readOnly: {
  55. type: Boolean,
  56. default: false,
  57. },
  58. /* 上传文件大小限制(MB) */
  59. fileSize: {
  60. type: Number,
  61. default: 5,
  62. },
  63. /* 类型(base64格式、url格式) */
  64. type: {
  65. type: String,
  66. default: "url",
  67. }
  68. })
  69. const options = ref({
  70. theme: "snow",
  71. bounds: document.body,
  72. debug: "warn",
  73. modules: {
  74. // 工具栏配置
  75. toolbar: [
  76. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  77. ["blockquote", "code-block"], // 引用 代码块
  78. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  79. [{ indent: "-1" }, { indent: "+1" }], // 缩进
  80. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  81. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  82. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  83. [{ align: [] }], // 对齐方式
  84. ["clean"], // 清除文本格式
  85. ["link", "image", "video"] // 链接、图片、视频
  86. ],
  87. },
  88. placeholder: "请输入内容",
  89. readOnly: props.readOnly
  90. })
  91. const styles = computed(() => {
  92. let style = {}
  93. if (props.minHeight) {
  94. style.minHeight = `${props.minHeight}px`
  95. }
  96. if (props.height) {
  97. style.height = `${props.height}px`
  98. }
  99. return style
  100. })
  101. const content = ref("")
  102. watch(() => props.modelValue, (v) => {
  103. if (v !== content.value) {
  104. content.value = v == undefined ? "<p></p>" : v
  105. // 内容更新后,为所有图片添加跨域和防盗链属性
  106. nextTick(() => {
  107. if (quillEditorRef.value && props.type == 'url') {
  108. const quill = quillEditorRef.value.getQuill()
  109. addImageAttributes(quill.root)
  110. }
  111. })
  112. }
  113. }, { immediate: true })
  114. // 如果设置了上传地址则自定义图片上传事件
  115. onMounted(() => {
  116. if (props.type == 'url') {
  117. let quill = quillEditorRef.value.getQuill()
  118. let toolbar = quill.getModule("toolbar")
  119. toolbar.addHandler("image", (value) => {
  120. if (value) {
  121. proxy.$refs.uploadRef.click()
  122. } else {
  123. quill.format("image", false)
  124. }
  125. })
  126. quill.root.addEventListener('paste', handlePasteCapture, true)
  127. // 监听编辑器内容变化,为所有图片添加跨域和防盗链属性
  128. quill.on('text-change', () => {
  129. addImageAttributes(quill.root)
  130. })
  131. // 初始化时也为已有图片添加属性
  132. nextTick(() => {
  133. addImageAttributes(quill.root)
  134. })
  135. }
  136. })
  137. // 为编辑器中的所有图片添加跨域和防盗链属性
  138. function addImageAttributes(rootElement) {
  139. const images = rootElement.querySelectorAll('img')
  140. images.forEach(img => {
  141. // 如果图片还没有这些属性,则添加
  142. if (!img.hasAttribute('crossorigin')) {
  143. img.setAttribute('crossorigin', 'anonymous')
  144. }
  145. if (!img.hasAttribute('referrerpolicy')) {
  146. img.setAttribute('referrerpolicy', 'no-referrer')
  147. }
  148. // 确保图片样式正常
  149. if (!img.style.maxWidth) {
  150. img.style.maxWidth = '100%'
  151. img.style.height = 'auto'
  152. img.style.display = 'block'
  153. img.style.margin = '10px 0'
  154. }
  155. })
  156. }
  157. // 上传前校检格式和大小
  158. function handleBeforeUpload(file) {
  159. const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"]
  160. const isJPG = type.includes(file.type)
  161. //检验文件格式
  162. if (!isJPG) {
  163. proxy.$modal.msgError(`图片格式错误!`)
  164. return false
  165. }
  166. // 校检文件大小
  167. if (props.fileSize) {
  168. const isLt = file.size / 1024 / 1024 < props.fileSize
  169. if (!isLt) {
  170. proxy.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`)
  171. return false
  172. }
  173. }
  174. return true
  175. }
  176. // 自定义上传方法 - 通过后端获取签名后上传到OSS
  177. async function customUpload(options) {
  178. const file = options.file
  179. try {
  180. // 1. 从后端获取OSS签名
  181. const signatureResponse = await request({
  182. url: '/common/oss/signature',
  183. method: 'get',
  184. params: {
  185. dir: 'questions/images/'
  186. }
  187. })
  188. if (signatureResponse.code !== 200 || !signatureResponse.data) {
  189. throw new Error(signatureResponse.msg || '获取OSS签名失败')
  190. }
  191. const signature = signatureResponse.data
  192. // 2. 生成文件路径
  193. const filePath = generateFilePath(file, signature.dir)
  194. // 3. 构建FormData,使用PostObject方式上传
  195. const formData = new FormData()
  196. formData.append('key', filePath)
  197. formData.append('policy', signature.policy)
  198. formData.append('OSSAccessKeyId', signature.accessKeyId)
  199. formData.append('signature', signature.signature)
  200. formData.append('file', file)
  201. // 4. 上传到OSS
  202. const response = await fetch(signature.host, {
  203. method: 'POST',
  204. body: formData
  205. })
  206. if (response.ok || response.status === 204) {
  207. // 上传成功,返回OSS URL
  208. const ossUrl = `${signature.host}/${filePath}`
  209. handleUploadSuccess({
  210. code: 200,
  211. fileName: filePath,
  212. url: ossUrl
  213. }, file)
  214. } else {
  215. const errorText = await response.text()
  216. throw new Error(`上传失败: ${response.status} ${errorText}`)
  217. }
  218. } catch (error) {
  219. console.error('OSS上传失败:', error)
  220. proxy.$modal.msgError('上传图片失败: ' + (error.message || '未知错误'))
  221. }
  222. }
  223. // 生成文件路径
  224. function generateFilePath(file, dir) {
  225. const date = new Date()
  226. const year = date.getFullYear()
  227. const month = String(date.getMonth() + 1).padStart(2, '0')
  228. const day = String(date.getDate()).padStart(2, '0')
  229. const timestamp = Date.now()
  230. const random = Math.random().toString(36).substring(2, 15)
  231. const ext = file.name.substring(file.name.lastIndexOf('.'))
  232. return `${dir}${year}/${month}/${day}/${timestamp}_${random}${ext}`
  233. }
  234. // 上传成功处理
  235. function handleUploadSuccess(res, file) {
  236. // 如果上传成功
  237. if (res.code == 200) {
  238. // 获取富文本实例
  239. let quill = toRaw(quillEditorRef.value).getQuill()
  240. // 获取光标位置
  241. let length = quill.selection.savedRange.index
  242. // 插入图片,使用OSS的完整URL
  243. const imageUrl = res.url || (import.meta.env.VITE_APP_BASE_API + res.fileName)
  244. quill.insertEmbed(length, "image", imageUrl)
  245. // 等待图片插入后,为其添加跨域和防盗链属性
  246. nextTick(() => {
  247. const images = quill.root.querySelectorAll('img')
  248. images.forEach(img => {
  249. if (img.src === imageUrl || img.src.includes(imageUrl)) {
  250. img.setAttribute('crossorigin', 'anonymous')
  251. img.setAttribute('referrerpolicy', 'no-referrer')
  252. img.style.maxWidth = '100%'
  253. img.style.height = 'auto'
  254. img.style.display = 'block'
  255. img.style.margin = '10px 0'
  256. }
  257. })
  258. })
  259. // 调整光标到最后
  260. quill.setSelection(length + 1)
  261. } else {
  262. proxy.$modal.msgError("图片插入失败")
  263. }
  264. }
  265. // 上传失败处理
  266. function handleUploadError() {
  267. proxy.$modal.msgError("图片插入失败")
  268. }
  269. // 复制粘贴图片处理
  270. function handlePasteCapture(e) {
  271. const clipboard = e.clipboardData || window.clipboardData
  272. if (clipboard && clipboard.items) {
  273. for (let i = 0; i < clipboard.items.length; i++) {
  274. const item = clipboard.items[i]
  275. if (item.type.indexOf('image') !== -1) {
  276. e.preventDefault()
  277. const file = item.getAsFile()
  278. insertImage(file)
  279. }
  280. }
  281. }
  282. }
  283. async function insertImage(file) {
  284. try {
  285. // 使用OSS上传
  286. await customUpload({ file })
  287. } catch (error) {
  288. console.error('粘贴图片上传失败:', error)
  289. proxy.$modal.msgError('上传图片失败')
  290. }
  291. }
  292. </script>
  293. <style>
  294. .editor-img-uploader {
  295. display: none;
  296. }
  297. .editor, .ql-toolbar {
  298. white-space: pre-wrap !important;
  299. line-height: normal !important;
  300. }
  301. /* 确保富文本编辑器中的图片正常显示 */
  302. .editor :deep(img) {
  303. max-width: 100% !important;
  304. height: auto !important;
  305. display: block !important;
  306. margin: 10px 0 !important;
  307. border-radius: 4px;
  308. }
  309. .quill-img {
  310. display: none;
  311. }
  312. .ql-snow .ql-tooltip[data-mode="link"]::before {
  313. content: "请输入链接地址:";
  314. }
  315. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  316. border-right: 0px;
  317. content: "保存";
  318. padding-right: 0px;
  319. }
  320. .ql-snow .ql-tooltip[data-mode="video"]::before {
  321. content: "请输入视频地址:";
  322. }
  323. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  324. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  325. content: "14px";
  326. }
  327. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  328. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  329. content: "10px";
  330. }
  331. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  332. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  333. content: "18px";
  334. }
  335. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  336. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  337. content: "32px";
  338. }
  339. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  340. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  341. content: "文本";
  342. }
  343. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  344. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  345. content: "标题1";
  346. }
  347. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  348. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  349. content: "标题2";
  350. }
  351. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  352. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  353. content: "标题3";
  354. }
  355. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  356. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  357. content: "标题4";
  358. }
  359. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  360. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  361. content: "标题5";
  362. }
  363. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  364. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  365. content: "标题6";
  366. }
  367. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  368. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  369. content: "标准字体";
  370. }
  371. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  372. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  373. content: "衬线字体";
  374. }
  375. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  376. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  377. content: "等宽字体";
  378. }
  379. </style>