Explorar o código

famous paper - preview/download oss file

hare8999@163.com hai 1 ano
pai
achega
311123eec3

+ 4 - 0
src/common/MxConst.js

@@ -39,6 +39,10 @@ export default {
         competition: 2, // 学乐园 - 竞赛
         ai: 3, // 数据中心 - AI匹配试题
         elective: 4 // 选科测评
+      },
+      paperSource: {
+        zxxk: 0, // 学科网 - 在题库里存在原题
+        oss: 1 // 第3方下载 - 只有纸质文件可供下载
       }
     },
     formType: {

+ 2 - 1
src/main.js

@@ -49,7 +49,8 @@ import hasHistory from "@/directive/hasHistory"
 import MxDialog from '@/components/MxDialog/index'
 import DictData from "@/components/DictData"
 
-Vue.prototype.$imgBase = 'https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/mingxueMainImgs/'
+Vue.prototype.$ossBase = 'https://mingxuejingbang.oss-cn-beijing.aliyuncs.com'
+Vue.prototype.$imgBase = Vue.prototype.$ossBase + '/mingxueMainImgs/'
 Vue.prototype.$Dialog = MxDialog
 Vue.prototype.mxGlobal = globalVariable
 // 全局方法挂载

+ 31 - 0
src/utils/download-helper.js

@@ -0,0 +1,31 @@
+export function downloadBlobFile(response, filename) {
+  // download blob file
+  const url = window.URL.createObjectURL(new Blob([response.data]))
+
+  // get file suffix from response header
+  // build file name from data.name data.score data.batchName and suffix
+  let disposition = response.headers['content-disposition']
+  disposition = disposition.replace('"', '').replace("'", '')
+  const suffix = disposition.substring(disposition.lastIndexOf('.'), disposition.length - 1)
+  const fileName = `${filename}${suffix}`
+
+  // Create a download link and trigger a click event to download the file
+  const link = document.createElement('a')
+  link.href = url
+  link.setAttribute('download', fileName)
+  document.body.appendChild(link)
+  link.click()
+
+  // Remove the link element and release the object URL
+  document.body.removeChild(link)
+  window.URL.revokeObjectURL(url)
+}
+
+export function downloadOssFile(url, fileName) {
+  const link = document.createElement('a')
+  link.href = url
+  link.setAttribute('download', fileName)
+  document.body.appendChild(link)
+  link.click()
+  document.body.removeChild(link)
+}

+ 33 - 4
src/views/questioncenter/components/famous-paper.vue

@@ -18,11 +18,11 @@
               {{ item.papername }}
             </div>
             <div class="center_opera">
-              <div class="view" @click="toPreView(item.id, item.papername)">
+              <div class="view" @click="toPreView(item)">
                 <img src="@/assets/images/icon_view.png" alt=""/>
                 <span>预览</span>
               </div>
-              <div class="download" @click="paperDownLoad(item.id)">
+              <div class="download" @click="paperDownLoad(item)">
                 <span>下载</span>
               </div>
               <div class="shoucan">
@@ -59,6 +59,8 @@ import { downloadRealPaper, paperGrade, papersCancelCollect, papersCollect, pape
 import { mapGetters } from 'vuex'
 import MxCondition from '@/components/MxCondition/mx-condition'
 import MxSearchGroup from '@/components/MxSearch/mx-search-group'
+import MxConst from "@/common/MxConst";
+import {downloadOssFile} from "@/utils/download-helper";
 
 export default {
   name: 'famous-paper',
@@ -91,11 +93,38 @@ export default {
     ...mapGetters(['period', 'isFrontTeacher'])
   },
   methods: {
-    paperDownLoad(paperId) {
+    _combineOssPath(item) {
+      const {osspath, filename} = item
+      return `${this.$ossBase}/${osspath}/${filename}`
+    },
+    _ossPreview(url) {
+      const pdfSuffixes = ['.pdf']
+      const msSuffixes = ['.doc','.ppt','.xls','.docx', 'pptx', '.xlsx']
+      if (pdfSuffixes.some(s => url.endsWith(s))) {
+        window.open('/pdfView/index.html?src=' + url)
+      } else if (msSuffixes.some(s => url.endsWith(s))) {
+        window.open('https://view.officeapps.live.com/op/view.aspx?src=' + url)
+      } else {
+        this.$message.error('格式错误,无法预览该格式文件!');
+      }
+    },
+    paperDownLoad(item) {
+      const {id: paperId, paperSource, filename} = item
+      if (paperSource == MxConst.enum.paper.paperSource.oss) {
+        const ossFullPath = this._combineOssPath(item)
+        downloadOssFile(ossFullPath, filename)
+        return
+      }
       downloadRealPaper(paperId, this.period)
     },
     // 跳转到预览页面
-    toPreView(paperId, paperName) {
+    toPreView(item) {
+      const {id: paperId, papername: paperName, paperSource} = item
+      if (paperSource == MxConst.enum.paper.paperSource.oss) {
+        const ossFullPath = this._combineOssPath(item)
+        this._ossPreview(ossFullPath)
+        return
+      }
       this.$router.push({
         path: '/question-center/bestPaper/preview',
         query: { paperId, paperName }