Browse Source

添加上传附件

zhangbiao 3 years ago
parent
commit
b9513d12d6

+ 1 - 0
src/components/tinymce/TinymceEditor.vue

@@ -174,6 +174,7 @@ export default {
                 bucket: bucket
               });
               // 上传oss
+              
               try {
                 client.put(ossPath + file.name, file).then(res => {
                   console.log(res);

+ 61 - 3
src/views/articleManagement/articleCreate.vue

@@ -127,6 +127,16 @@
     <div class="common-card a-card-margin-top">
       <div class="information-title">文章内容</div>
       <TinymceEditor v-model="content"></TinymceEditor>
+      <div>
+        <div>上传附件</div>
+        <div>
+          <a-upload :customRequest="uploadOssFile" :default-file-list="defaultFileList">
+            <a-button>
+              <a-icon type="upload" />Upload
+            </a-button>
+          </a-upload>
+        </div>
+      </div>
       <div class="submit-row">
         <a-button type="primary" @click="handleSubmitBtnFun">提交</a-button>
         <a-button :style="{ marginLeft: '160px' }" @click="preview">预览</a-button>
@@ -153,6 +163,7 @@
 </template>
 
 <script>
+const OSS = require("ali-oss");
 import {
   formatePathParams,
   formateStringDate,
@@ -183,7 +194,8 @@ export default {
       bodyStyle: {
         img: "{max-width:100%;}",
         color: "red"
-      }
+      },
+      defaultFileList: []
     };
   },
   created() {
@@ -366,6 +378,10 @@ export default {
                 engineertypeid: that.engineerType.id
               };
               let tag = formateDateToString(that.learnDate);
+              let links = [];
+              that.defaultFileList.forEach(element => {
+                links.push(element.url);
+              });
               let bodyParams = {
                 name: values.name,
                 description: values.description,
@@ -373,7 +389,7 @@ export default {
                 contents: that.content,
                 tags: tag ? [tag] : [],
                 engineerTypes: [that.engineerType.id],
-                links: []
+                links: links
               };
               that.$_http
                 .post(that.$_API.INTERFACE_POST_ADMIN_MATERIALS, bodyParams, {
@@ -406,6 +422,10 @@ export default {
                 engineertypeid: that.engineerType.id
               };
               let tag = formateDateToString(that.learnDate);
+              let links = [];
+              that.defaultFileList.forEach(element => {
+                links.push(element.url);
+              });
               let bodyParams = {
                 id: that.$route.query.id,
                 name: values.name,
@@ -414,7 +434,7 @@ export default {
                 contents: that.content,
                 tags: tag ? [tag] : [],
                 engineerTypes: [that.engineerType.id],
-                links: []
+                links: links
               };
               console.log(
                 "保存修改日期格式为YYYYMMDD",
@@ -441,6 +461,44 @@ export default {
         }
       });
     },
+    uploadOssFile(fileData) {
+      let file = fileData.file;
+      let that = this;
+      let bucket = "jtxt-file-public";
+      // let filetype =
+      //   ".pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx";
+      let ossPath = "file/";
+      that.$_http.get(that.$_API.INTERFACE_GET_ASSUME_OSS_WRITER).then(res => {
+        console.log("--oss-writer--" + JSON.stringify(res));
+        const client = new OSS({
+          // yourRegion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
+          region: "oss-cn-beijing",
+          // 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
+          accessKeyId: res.data.accessKeyId,
+          accessKeySecret: res.data.accessKeySecret,
+          // 从STS服务获取的安全令牌(SecurityToken)。
+          stsToken: res.data.securityToken,
+          // 填写Bucket名称。
+          bucket: bucket
+        });
+        try {
+          client.put(ossPath + file.name, file).then(res => {
+            console.log(res);
+            let upFile = {
+              uid: this.defaultFileList.length + 1,
+              name: file.name,
+              status: "done",
+              url: res.url
+            };
+            this.defaultFileList.push(upFile);
+            fileData.onSuccess();
+          });
+        } catch (e) {
+          fileData.onError();
+          console.error(e);
+        }
+      });
+    },
     // 预览
     preview() {
       this.previewVisible = true;