From 67864e67b33e2c320e7ac5c8d5db48ab8124e1fc Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 26 四月 2023 22:47:28 +0800
Subject: [PATCH] fix 修复 oss 路径漏改问题

---
 src/components/Editor/index.vue |  252 +++++++++++++++++++++++++-------------------------
 1 files changed, 127 insertions(+), 125 deletions(-)

diff --git a/src/components/Editor/index.vue b/src/components/Editor/index.vue
index bc8da7b..a0728eb 100644
--- a/src/components/Editor/index.vue
+++ b/src/components/Editor/index.vue
@@ -1,166 +1,168 @@
 <template>
   <div>
     <el-upload
-        :action="uploadUrl"
-        :before-upload="handleBeforeUpload"
-        :on-success="handleUploadSuccess"
-        :on-error="handleUploadError"
-        class="editor-img-uploader"
-        name="file"
-        :show-file-list="false"
-        :headers="headers"
-        style="display: none"
-        ref="uploadRef"
-        v-if="type == 'url'"
+      :action="upload.url"
+      :before-upload="handleBeforeUpload"
+      :on-success="handleUploadSuccess"
+      :on-error="handleUploadError"
+      class="editor-img-uploader"
+      name="file"
+      :show-file-list="false"
+      :headers="upload.headers"
+      style="display: none"
+      v-if="type === 'url'"
     >
     </el-upload>
     <div class="editor">
       <quill-editor
-          ref="myQuillEditor"
-          v-model:content="content"
-          contentType="html"
-          @textChange="(e) => $emit('update:modelValue', content)"
-          :options="options"
-          :style="styles"
+        ref="myQuillEditor"
+        v-model:content="content"
+        contentType="html"
+        @textChange="(e: any) => $emit('update:modelValue', content)"
+        :options="options"
+        :style="styles"
       />
     </div>
   </div>
 </template>
 
-<script setup>
+<script setup lang="ts">
 import { QuillEditor, Quill } from '@vueup/vue-quill';
 import '@vueup/vue-quill/dist/vue-quill.snow.css';
 import { getToken } from "@/utils/auth";
+import { ComponentInternalInstance } from "vue";
 
 const props = defineProps({
-  /* 缂栬緫鍣ㄧ殑鍐呭 */
-  modelValue: {
-    type: String,
-  },
-  /* 楂樺害 */
-  height: {
-    type: Number,
-    default: null,
-  },
-  /* 鏈�灏忛珮搴� */
-  minHeight: {
-    type: Number,
-    default: null,
-  },
-  /* 鍙 */
-  readOnly: {
-    type: Boolean,
-    default: false,
-  },
-  /* 涓婁紶鏂囦欢澶у皬闄愬埗(MB) */
-  fileSize: {
-    type: Number,
-    default: 5,
-  },
-  /* 绫诲瀷锛坆ase64鏍煎紡銆乽rl鏍煎紡锛� */
-  type: {
-    type: String,
-    default: "url",
-  }
+    /* 缂栬緫鍣ㄧ殑鍐呭 */
+    modelValue: {
+        type: String,
+    },
+    /* 楂樺害 */
+    height: {
+        type: Number,
+        default: null,
+    },
+    /* 鏈�灏忛珮搴� */
+    minHeight: {
+        type: Number,
+        default: null,
+    },
+    /* 鍙 */
+    readOnly: {
+        type: Boolean,
+        default: false,
+    },
+    /* 涓婁紶鏂囦欢澶у皬闄愬埗(MB) */
+    fileSize: {
+        type: Number,
+        default: 5,
+    },
+    /* 绫诲瀷锛坆ase64鏍煎紡銆乽rl鏍煎紡锛� */
+    type: {
+        type: String,
+        default: "url",
+    }
 });
 
-const { proxy } = getCurrentInstance();
-// 涓婁紶鐨勫浘鐗囨湇鍔″櫒鍦板潃
-const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/system/oss/upload");
-const headers = ref({ Authorization: "Bearer " + getToken() });
+const { proxy } = getCurrentInstance() as ComponentInternalInstance;
+
+const upload = reactive<UploadOption>({
+    headers: { Authorization: "Bearer " + getToken() },
+    url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload'
+})
 const myQuillEditor = ref();
 
 const options = ref({
-  theme: "snow",
-  bounds: document.body,
-  debug: "warn",
-  modules: {
-    // 宸ュ叿鏍忛厤缃�
-    toolbar: {
-      container: [
-        ["bold", "italic", "underline", "strike"],       // 鍔犵矖 鏂滀綋 涓嬪垝绾� 鍒犻櫎绾�
-        ["blockquote", "code-block"],                    // 寮曠敤  浠g爜鍧�
-        [{ list: "ordered" }, { list: "bullet"} ],       // 鏈夊簭銆佹棤搴忓垪琛�
-        [{ indent: "-1" }, { indent: "+1" }],            // 缂╄繘
-        [{ size: ["small", false, "large", "huge"] }],   // 瀛椾綋澶у皬
-        [{ header: [1, 2, 3, 4, 5, 6, false] }],         // 鏍囬
-        [{ color: [] }, { background: [] }],             // 瀛椾綋棰滆壊銆佸瓧浣撹儗鏅鑹�
-        [{ align: [] }],                                 // 瀵归綈鏂瑰紡
-        ["clean"],                                       // 娓呴櫎鏂囨湰鏍煎紡
-        ["link", "image", "video"]                       // 閾炬帴銆佸浘鐗囥�佽棰�
-      ],
-      handlers: {
-        image: function (value) {
-          if (value) {
-            // 璋冪敤element鍥剧墖涓婁紶
-            document.querySelector(".editor-img-uploader>.el-upload").click();
-          } else {
-            Quill.format("image", true);
-          }
-        },
-      },
-    }
-  },
-  placeholder: '璇疯緭鍏ュ唴瀹�',
-  readOnly: props.readOnly,
+    theme: "snow",
+    bounds: document.body,
+    debug: "warn",
+    modules: {
+        // 宸ュ叿鏍忛厤缃�
+        toolbar: {
+            container: [
+                ["bold", "italic", "underline", "strike"],       // 鍔犵矖 鏂滀綋 涓嬪垝绾� 鍒犻櫎绾�
+                ["blockquote", "code-block"],                    // 寮曠敤  浠g爜鍧�
+                [{ list: "ordered" }, { list: "bullet"} ],       // 鏈夊簭銆佹棤搴忓垪琛�
+                [{ indent: "-1" }, { indent: "+1" }],            // 缂╄繘
+                [{ size: ["small", false, "large", "huge"] }],   // 瀛椾綋澶у皬
+                [{ header: [1, 2, 3, 4, 5, 6, false] }],         // 鏍囬
+                [{ color: [] }, { background: [] }],             // 瀛椾綋棰滆壊銆佸瓧浣撹儗鏅鑹�
+                [{ align: [] }],                                 // 瀵归綈鏂瑰紡
+                ["clean"],                                       // 娓呴櫎鏂囨湰鏍煎紡
+                ["link", "image", "video"]                       // 閾炬帴銆佸浘鐗囥�佽棰�
+            ],
+            handlers: {
+                image: function (value: any) {
+                    if (value) {
+                        // 璋冪敤element鍥剧墖涓婁紶
+                        (document.querySelector(".editor-img-uploader>.el-upload") as HTMLDivElement)?.click();
+                    } else {
+                        Quill.format("image", true);
+                    }
+                },
+            },
+        }
+    },
+    placeholder: '璇疯緭鍏ュ唴瀹�',
+    readOnly: props.readOnly,
 });
 
 const styles = computed(() => {
-  let style = {};
-  if (props.minHeight) {
-    style.minHeight = `${props.minHeight}px`;
-  }
-  if (props.height) {
-    style.height = `${props.height}px`;
-  }
-  return style;
+    let style: any = {};
+    if (props.minHeight) {
+        style.minHeight = `${props.minHeight}px`;
+    }
+    if (props.height) {
+        style.height = `${props.height}px`;
+    }
+    return style;
 })
 
 const content = ref("");
 watch(() => props.modelValue, (v) => {
-  if (v !== content) {
-    content.value = v === undefined ? "<p></p>" : v;
-  }
+    if (v !== content.value) {
+        content.value = v === undefined ? "<p></p>" : v;
+    }
 }, { immediate: true });
 
 // 鍥剧墖涓婁紶鎴愬姛杩斿洖鍥剧墖鍦板潃
-function handleUploadSuccess(res, file) {
-  // 鑾峰彇瀵屾枃鏈疄渚�
-  let quill = toRaw(myQuillEditor.value).getQuill();
-  // 濡傛灉涓婁紶鎴愬姛
-  if (res.code == 200) {
-    // 鑾峰彇鍏夋爣浣嶇疆
-    let length = quill.selection.savedRange.index;
-    // 鎻掑叆鍥剧墖锛宺es涓烘湇鍔″櫒杩斿洖鐨勫浘鐗囬摼鎺ュ湴鍧�
-    quill.insertEmbed(length, "image", res.data.url);
-    // 璋冩暣鍏夋爣鍒版渶鍚�
-    quill.setSelection(length + 1);
-    proxy.$modal.closeLoading();
-  } else {
-    proxy.$modal.loading(res.msg);
-    proxy.$modal.closeLoading();
-  }
+const handleUploadSuccess = (res: any) => {
+    // 鑾峰彇瀵屾枃鏈疄渚�
+    let quill = toRaw(myQuillEditor.value).getQuill();
+    // 濡傛灉涓婁紶鎴愬姛
+    if (res.code === 200) {
+        // 鑾峰彇鍏夋爣浣嶇疆
+        let length = quill.selection.savedRange.index;
+        // 鎻掑叆鍥剧墖锛宺es涓烘湇鍔″櫒杩斿洖鐨勫浘鐗囬摼鎺ュ湴鍧�
+        quill.insertEmbed(length, "image", res.data.url);
+        // 璋冩暣鍏夋爣鍒版渶鍚�
+        quill.setSelection(length + 1);
+        proxy?.$modal.closeLoading();
+    } else {
+        proxy?.$modal.loading(res.msg);
+        proxy?.$modal.closeLoading();
+    }
 }
 
 // 鍥剧墖涓婁紶鍓嶆嫤鎴�
-function handleBeforeUpload(file) {
-  // 鏍℃鏂囦欢澶у皬
-  if (props.fileSize) {
-    const isLt = file.size / 1024 / 1024 < props.fileSize;
-    if (!isLt) {
-      proxy.$modal.msgError(`涓婁紶鏂囦欢澶у皬涓嶈兘瓒呰繃 ${props.fileSize} MB!`);
-      return false;
+const handleBeforeUpload = (file: any) => {
+    // 鏍℃鏂囦欢澶у皬
+    if (props.fileSize) {
+        const isLt = file.size / 1024 / 1024 < props.fileSize;
+        if (!isLt) {
+            proxy?.$modal.msgError(`涓婁紶鏂囦欢澶у皬涓嶈兘瓒呰繃 ${props.fileSize} MB!`);
+            return false;
+        }
     }
-  }
-  proxy.$modal.loading("姝e湪涓婁紶鏂囦欢锛岃绋嶅��...");
-  return true;
+    proxy?.$modal.loading('姝e湪涓婁紶鏂囦欢锛岃绋嶅��...');
+    return true;
 }
 
 // 鍥剧墖澶辫触鎷︽埅
-function handleUploadError(err) {
-  proxy.$modal.msgError("涓婁紶鏂囦欢澶辫触");
+const handleUploadError = (err: any) => {
+    console.error(err);
+    proxy?.$modal.msgError('涓婁紶鏂囦欢澶辫触');
 }
-
 </script>
 
 <style>
@@ -175,9 +177,9 @@
   content: "璇疯緭鍏ラ摼鎺ュ湴鍧�:";
 }
 .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
-  border-right: 0px;
+  border-right: 0;
   content: "淇濆瓨";
-  padding-right: 0px;
+  padding-right: 0;
 }
 
 .ql-snow .ql-tooltip[data-mode="video"]::before {

--
Gitblit v1.9.3