瀏覽代碼

上传文件方法封装

user5 2 年之前
父節點
當前提交
2a549b6bb3

+ 81 - 1
src/api/sys/OSSService.js

@@ -45,7 +45,7 @@ export const client = new OSS({
   region: 'oss-cn-hangzhou', // oss地址
   accessKeyId: 'LTAI5tQDWoM9c1WyJNPs86rX', // 通过阿里云控制台创建的AccessKey ID。
   accessKeySecret: '84dDIx4edT1n78KUOqqSmDZ35pchJv', // 通过阿里云控制台创建的AccessKey Secret。
-  bucket: 'xgxm-test', // 仓库名字
+  bucket: 'xg-pg', // 仓库名字
   useFetch: true, // 支持上传大于100KB的文件
   secure: false // 返回的url为https
 })
@@ -152,3 +152,83 @@ export async function httpRequest (file, name, type) { // 阿里云OSS上传
     file.onError('上传失败') // 触发el-upload组件的onError方法,此方法会移除文件列表
   })
 }
+
+// eslint-disable-next-line no-unused-vars
+function getTemporaryByUrl (url) {
+  return request({
+    url: '/oss/file/getTemporaryUrl',
+    method: 'get',
+    params: {url: url}
+  })
+}
+
+export async function openWindowOnUrl (row) {
+  var rowurl = ''
+  let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
+  if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
+    if (row.url !== null && row.url !== undefined && row.url !== '') {
+      // eslint-disable-next-line no-undef
+      onPreview(row.temporaryUrl)
+      return
+    } else {
+      await getTemporaryByUrl(row.raw.url).then((data) => {
+        // eslint-disable-next-line no-undef
+        onPreview(data.data)
+      })
+      return
+    }
+  }
+  if (row.url !== null && row.url !== undefined && row.url !== '') {
+    if (suffix === 'pdf') {
+      window.open('https://view.xdocin.com/xdoc?_xdoc=' + encodeURIComponent(row.temporaryUrl), '_blank')
+    } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
+      window.open('http://ow365.cn/?i=30045&furl=' + encodeURIComponent(row.temporaryUrl), '_blank')
+    } else {
+      window.open('https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(row.temporaryUrl), '_blank')
+    }
+  } else if (row.raw.url !== null && row.raw.url !== undefined && row.raw.url !== '') {
+    await getTemporaryByUrl(row.raw.url).then((data) => {
+      rowurl = data.data
+    })
+    if (suffix === 'pdf') {
+      window.open('https://view.xdocin.com/xdoc?_xdoc=' + encodeURIComponent(rowurl), '_blank')
+    } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
+      window.open('http://ow365.cn/?i=30045&furl=' + encodeURIComponent(rowurl), '_blank')
+    } else {
+      window.open('https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(rowurl), '_blank')
+    }
+  }
+}
+
+export async function toHref (row) {
+  const link = document.createElement('a')
+  if (row.id === null || row.id === undefined || row.id === '') {
+    await getTemporaryByUrl(row.raw.url).then((data) => {
+      const url = data.data // 完整的url则直接使用
+      // 这里是将url转成blob地址,
+      fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
+        link.href = URL.createObjectURL(blob)
+        link.download = row.name || '' // 下载文件的名字
+        // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
+        document.body.appendChild(link)
+        link.click()
+        // 在资源下载完成后 清除 占用的缓存资源
+        window.URL.revokeObjectURL(link.href)
+        document.body.removeChild(link)
+      })
+    })
+  } else {
+    const url = row.temporaryUrl // 完整的url则直接使用
+    // 这里是将url转成blob地址,
+    fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
+      link.href = URL.createObjectURL(blob)
+      link.download = row.name || '' // 下载文件的名字
+      // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
+      document.body.appendChild(link)
+      link.click()
+      // 在资源下载完成后 清除 占用的缓存资源
+      window.URL.revokeObjectURL(link.href)
+      document.body.removeChild(link)
+    })
+  }
+}

+ 10 - 64
src/views/modules/sys/project/AssessForm.vue

@@ -415,6 +415,8 @@
   import SelectUserTree from '@/views/modules/utils/treeUserSelect'
   import OSSSerive, {
     httpRequest,
+    openWindowOnUrl,
+    toHref,
     handleRemove,
     fileName
   } from '@/api/sys/OSSService'
@@ -496,6 +498,9 @@
       this.ossService = new OSSSerive()
       this.projectService = new ProjectService()
     },
+    mounted () {
+      window.onPreview = this.onPreview
+    },
     methods: {
       uploadVideoProcess (event, file, fileList) {
         this.progressFlag = true // 显示进度条
@@ -523,36 +528,7 @@
       //   }
       // },
       async toHref (row) {
-        const link = document.createElement('a')
-        if (row.id === null || row.id === undefined || row.id === '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            const url = data.data // 完整的url则直接使用
-            // 这里是将url转成blob地址,
-            fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-              link.href = URL.createObjectURL(blob)
-              link.download = row.name || '' // 下载文件的名字
-              // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-              document.body.appendChild(link)
-              link.click()
-              // 在资源下载完成后 清除 占用的缓存资源
-              window.URL.revokeObjectURL(link.href)
-              document.body.removeChild(link)
-            })
-          })
-        } else {
-          const url = row.temporaryUrl // 完整的url则直接使用
-          // 这里是将url转成blob地址,
-          fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-            link.href = URL.createObjectURL(blob)
-            link.download = row.name || '' // 下载文件的名字
-            // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-            document.body.appendChild(link)
-            link.click()
-            // 在资源下载完成后 清除 占用的缓存资源
-            window.URL.revokeObjectURL(link.href)
-            document.body.removeChild(link)
-          })
-        }
+        toHref(row)
       },
       onPreview (url) {
         this.url = url
@@ -655,6 +631,7 @@
       doSubmit () {
         if (this.progressFlag === true) {
           this.$message.warning('文件正在上传中,请稍等')
+          return
         }
         this.$refs['inputForm'].validate((valid) => {
           if (valid) {
@@ -708,9 +685,9 @@
           item.createBy = this.$store.state.user.name
           this.dataListNew.push(item)
         })
-        const isLt2M = file.size / 1024 / 1024 < 300
+        const isLt2M = file.size / 1024 / 1024 < 1024
         if (isLt2M === false) {
-          this.$message.error('文件大小不能超过 ' + 300 + 'M !')
+          this.$message.error('文件大小不能超过 ' + 1 + 'G !')
           this.fileList = []
           this.filesArra2 = []
         } else {
@@ -721,38 +698,7 @@
         }
       },
       async showFile (row) {
-        let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
-        if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
-          if (row.url !== null && row.url !== undefined && row.url !== '') {
-            this.onPreview(row.url)
-            return
-          } else {
-            await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-              this.onPreview(data.data)
-            })
-            return
-          }
-        }
-        if (row.url !== null && row.url !== undefined && row.url !== '') {
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + row.url, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + row.url, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + row.url, '_blank')
-          }
-        } else if (row.raw.url !== null && row.raw.url !== undefined && row.raw.url !== '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            this.rowurl = data.data
-          })
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + this.rowurl, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + this.rowurl, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + this.rowurl, '_blank')
-          }
-        }
+        await openWindowOnUrl(row)
       },
       // 排序
       sortChangeHandle (column) {

+ 10 - 64
src/views/modules/sys/project/ConsultationForm.vue

@@ -418,6 +418,8 @@
   import SelectUserTree from '@/views/modules/utils/treeUserSelect'
   import OSSSerive, {
     httpRequest,
+    toHref,
+    openWindowOnUrl,
     handleRemove,
     fileName
   } from '@/api/sys/OSSService'
@@ -499,6 +501,9 @@
       this.ossService = new OSSSerive()
       this.projectService = new ProjectService()
     },
+    mounted () {
+      window.onPreview = this.onPreview
+    },
     methods: {
 
       uploadVideoProcess (event, file, fileList) {
@@ -510,36 +515,7 @@
         }
       },
       async toHref (row) {
-        const link = document.createElement('a')
-        if (row.id === null || row.id === undefined || row.id === '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            const url = data.data // 完整的url则直接使用
-            // 这里是将url转成blob地址,
-            fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-              link.href = URL.createObjectURL(blob)
-              link.download = row.name || '' // 下载文件的名字
-              // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-              document.body.appendChild(link)
-              link.click()
-              // 在资源下载完成后 清除 占用的缓存资源
-              window.URL.revokeObjectURL(link.href)
-              document.body.removeChild(link)
-            })
-          })
-        } else {
-          const url = row.temporaryUrl // 完整的url则直接使用
-          // 这里是将url转成blob地址,
-          fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-            link.href = URL.createObjectURL(blob)
-            link.download = row.name || '' // 下载文件的名字
-            // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-            document.body.appendChild(link)
-            link.click()
-            // 在资源下载完成后 清除 占用的缓存资源
-            window.URL.revokeObjectURL(link.href)
-            document.body.removeChild(link)
-          })
-        }
+        toHref(row)
       },
       onPreview (url) {
         this.url = url
@@ -641,6 +617,7 @@
       doSubmit () {
         if (this.progressFlag === true) {
           this.$message.warning('文件正在上传中,请稍等')
+          return
         }
         this.$refs['inputForm'].validate((valid) => {
           if (valid) {
@@ -694,9 +671,9 @@
           item.createBy = this.$store.state.user.name
           this.dataListNew.push(item)
         })
-        const isLt2M = file.size / 1024 / 1024 < 300
+        const isLt2M = file.size / 1024 / 1024 < 1024
         if (isLt2M === false) {
-          this.$message.error('文件大小不能超过 ' + 300 + 'M !')
+          this.$message.error('文件大小不能超过 ' + 1 + 'G !')
           this.fileList = []
           this.filesArra2 = []
         } else {
@@ -707,38 +684,7 @@
         }
       },
       async showFile (row) {
-        let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
-        if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
-          if (row.url !== null && row.url !== undefined && row.url !== '') {
-            this.onPreview(row.url)
-            return
-          } else {
-            await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-              this.onPreview(data.data)
-            })
-            return
-          }
-        }
-        if (row.url !== null && row.url !== undefined && row.url !== '') {
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + row.url, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + row.url, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + row.url, '_blank')
-          }
-        } else if (row.raw.url !== null && row.raw.url !== undefined && row.raw.url !== '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            this.rowurl = data.data
-          })
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + this.rowurl, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + this.rowurl, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + this.rowurl, '_blank')
-          }
-        }
+        await openWindowOnUrl(row)
       },
       // 排序
       sortChangeHandle (column) {

+ 11 - 65
src/views/modules/sys/workClient/WorkClientForm.vue

@@ -390,6 +390,8 @@
   import SelectTree from '@/components/treeSelect/treeSelect.vue'
   import OSSSerive, {
     httpRequest,
+    toHref,
+    openWindowOnUrl,
     handleRemove,
     fileName
   } from '@/api/sys/OSSService'
@@ -527,6 +529,9 @@
       this.ossService = new OSSSerive()
       this.workClientService = new WorkClientService()
     },
+    mounted () {
+      window.onPreview = this.onPreview
+    },
     methods: {
       uploadVideoProcess (event, file, fileList) {
         this.progressFlag = true // 显示进度条
@@ -540,7 +545,7 @@
         let value = this.inputForm.workClientInfo.name
         if (value !== null && value !== undefined && value !== '') {
           this.workClientService.isExist(value).then((data) => {
-            console.log('data',data.data)
+            console.log('data', data.data)
             if (data.data !== 0) {
               this.$message.error('该客户名称已存在')
               this.isSubmit = true
@@ -552,7 +557,7 @@
         let value = this.inputForm.workClientInfo.uscCode
         if (value !== null && value !== undefined && value !== '') {
           this.workClientService.isExist(value).then((data) => {
-            console.log('data',data.data)
+            console.log('data', data.data)
             if (data.data !== 0) {
               this.$message.error('该统一社会信用代码已存在')
               this.isSubmit = true
@@ -561,36 +566,7 @@
         }
       },
       async toHref (row) {
-        const link = document.createElement('a')
-        if (row.id === null || row.id === undefined || row.id === '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            const url = data.data // 完整的url则直接使用
-            // 这里是将url转成blob地址,
-            fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-              link.href = URL.createObjectURL(blob)
-              link.download = row.name || '' // 下载文件的名字
-              // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-              document.body.appendChild(link)
-              link.click()
-              // 在资源下载完成后 清除 占用的缓存资源
-              window.URL.revokeObjectURL(link.href)
-              document.body.removeChild(link)
-            })
-          })
-        } else {
-          const url = row.temporaryUrl // 完整的url则直接使用
-          // 这里是将url转成blob地址,
-          fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
-            link.href = URL.createObjectURL(blob)
-            link.download = row.name || '' // 下载文件的名字
-            // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
-            document.body.appendChild(link)
-            link.click()
-            // 在资源下载完成后 清除 占用的缓存资源
-            window.URL.revokeObjectURL(link.href)
-            document.body.removeChild(link)
-          })
-        }
+        toHref(row)
       },
       onPreview (url) {
         this.url = url
@@ -732,6 +708,7 @@
       doSubmit () {
         if (this.progressFlag === true) {
           this.$message.warning('文件正在上传中,请稍等')
+          return
         }
         this.$refs['inputForm'].validate((valid) => {
           if (valid) {
@@ -831,7 +808,7 @@
         this.showVi = true
       },
       httpRequest (file) {
-        httpRequest(file, fileName(file), 'projectRecords')
+        httpRequest(file, fileName(file), 'workClient')
       },
       handleRemove () {
         this.fileList = handleRemove()
@@ -854,38 +831,7 @@
         }
       },
       async showFile (row) {
-        let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
-        if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
-          if (row.url !== null && row.url !== undefined && row.url !== '') {
-            this.onPreview(row.url)
-            return
-          } else {
-            await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-              this.onPreview(data.data)
-            })
-            return
-          }
-        }
-        if (row.url !== null && row.url !== undefined && row.url !== '') {
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + row.url, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + row.url, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + row.url, '_blank')
-          }
-        } else if (row.raw.url !== null && row.raw.url !== undefined && row.raw.url !== '') {
-          await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            this.rowurl = data.data
-          })
-          if (suffix === 'pdf') {
-            window.open('https://view.xdocin.com/xdoc?_xdoc=' + this.rowurl, '_blank')
-          } else if (suffix === 'rar' || suffix === 'zip' || suffix === 'jar' || suffix === '7z') {
-            window.open('http://ow365.cn/?i=25008&furl=' + this.rowurl, '_blank')
-          } else {
-            window.open('https://view.officeapps.live.com/op/view.aspx?src=' + this.rowurl, '_blank')
-          }
-        }
+        await openWindowOnUrl(row)
       },
       // 排序
       sortChangeHandle (column) {