DetailsEnclosure.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!--
  2. * @Author: liuzhenlin 461480418@qq.ocm
  3. * @Date: 2023-01-10 15:03:27
  4. * @LastEditors: liuzhenlin
  5. * @LastEditTime: 2023-05-18 15:09:27
  6. * @Description: file content
  7. * @FilePath: \订单全流程管理系统\src\views\contract\components\DetailsEnclosure.vue
  8. -->
  9. <template>
  10. <!-- 附件 -->
  11. <div class="enclosure-container">
  12. <el-row class="mb10">
  13. <el-col class="text-right" :span="24">
  14. <el-upload
  15. ref="uploadRef"
  16. action="#"
  17. :before-upload="
  18. (file) => {
  19. return beforeAvatarUpload(file)
  20. }
  21. "
  22. :file-list="fileList"
  23. :http-request="uploadrequest">
  24. <el-button size="mini" type="primary">点击上传</el-button>
  25. <el-button v-permissions="['business:detail:enclosure:add']" size="mini" type="primary">点击上传</el-button>
  26. </el-upload>
  27. </el-col>
  28. </el-row>
  29. <el-table border :data="enclosureData" height="calc(100% - 80px)">
  30. <el-table-column
  31. v-for="(item, index) in columns"
  32. :key="index"
  33. align="center"
  34. :label="item.label"
  35. :min-width="item.width"
  36. :prop="item.prop"
  37. show-overflow-tooltip>
  38. <template #default="{ row }">
  39. <span v-if="item.prop == 'createdTime'">
  40. {{ parseTime(row.createdTime, '{y}-{m}-{d} {h}:{i}') }}
  41. </span>
  42. <span v-else>{{ row[item.prop] }}</span>
  43. </template>
  44. </el-table-column>
  45. <el-table-column align="center" label="操作">
  46. <template slot-scope="scope">
  47. <el-button v-permissions="['business:detail:enclosure:download']" type="text" @click="downFile(scope.row)">
  48. 下载
  49. </el-button>
  50. <el-button v-permissions="['business:detail:enclosure:delete']" type="text" @click="handleDel(scope.row)">
  51. 删除
  52. </el-button>
  53. </template>
  54. </el-table-column>
  55. </el-table>
  56. <el-dialog title="编辑" :visible.sync="editVisible" width="300px">
  57. <el-form ref="editForm" :model="editFiles" :rules="editRules">
  58. <el-row :gutter="20">
  59. <el-col :span="24">
  60. <el-form-item label="新名称" prop="contractCode">
  61. <el-input v-model="editFiles.fileName" placeholder="请输入新名称" />
  62. </el-form-item>
  63. </el-col>
  64. </el-row>
  65. </el-form>
  66. <span slot="footer">
  67. <el-button type="primary" @click="saveEditName">保存</el-button>
  68. <el-button @click="editVisible = false">取消</el-button>
  69. </span>
  70. </el-dialog>
  71. </div>
  72. </template>
  73. <script>
  74. import axios from 'axios'
  75. import to from 'await-to-js'
  76. import enclosureApi from '@/api/proj/business'
  77. import asyncUploadFile from '@/utils/uploadajax'
  78. import downloadFileByByte from '@/utils/base64ToFile'
  79. export default {
  80. name: 'DetailsEnclosure',
  81. // props: {
  82. // // 项目Id
  83. // busId: {
  84. // type: Number,
  85. // default: 0,
  86. // },
  87. // },
  88. data() {
  89. return {
  90. busId: undefined,
  91. fileList: [],
  92. fileSettings: {
  93. // 文件配置信息
  94. fileSize: 52428800,
  95. fileTypes: '.doc,.docx,.zip,.xls,.xlsx,.rar,.jpg,.jpeg,.gif,.png,.jfif,.txt,.pdf',
  96. pictureSize: 52428800,
  97. pictureTypes: '.jpg,.jpeg,.gif,.png,.jfif,.txt',
  98. types: '.doc,.docx,.zip,.xls,.xlsx,.rar,.jpg,.jpeg,.gif,.png,.jfif,.mp4,.txt,.pdf',
  99. videoSize: 104857600,
  100. videoType: '.mp4',
  101. },
  102. editVisible: false, //编辑
  103. // 编辑文件
  104. editFiles: {
  105. fileName: '',
  106. id: 0,
  107. },
  108. editRules: {
  109. fileName: [{ required: true, trigger: 'blur', message: '请输入新名称' }],
  110. },
  111. enclosureData: [],
  112. columns: [
  113. {
  114. label: '附件名称',
  115. width: '300px',
  116. prop: 'fileName',
  117. },
  118. {
  119. label: '上传人',
  120. width: '60px',
  121. prop: 'createdName',
  122. },
  123. {
  124. label: '上传时间',
  125. width: 'auto',
  126. prop: 'createdTime',
  127. },
  128. ],
  129. }
  130. },
  131. mounted() {
  132. // this.getEnclosureList()
  133. },
  134. methods: {
  135. open(busId) {
  136. console.log('busId', busId)
  137. this.busId = busId
  138. this.getEnclosureList()
  139. },
  140. async getEnclosureList() {
  141. let params = { busId: this.busId }
  142. const [err, res] = await to(enclosureApi.getBusinessEnclosureList(params))
  143. if (err) return
  144. this.enclosureData = res.data
  145. },
  146. // 下载
  147. /**
  148. * 下载文件以及自定义文件名称
  149. */
  150. downFile(row) {
  151. this.$message.warning('开始下载,请稍后。')
  152. if (row.fileUrl.startsWith('dingtalk')) {
  153. this.downDingtalkFile(row)
  154. return
  155. }
  156. let url = row.fileUrl
  157. let fileName = row.fileName
  158. const xhr = new XMLHttpRequest()
  159. xhr.open('GET', url, true)
  160. xhr.responseType = 'blob' // 通过文件下载url拿到对应的blob对象
  161. xhr.onload = () => {
  162. if (xhr.status === 200) {
  163. let link = document.createElement('a')
  164. let body = document.querySelector('body')
  165. link.href = window.URL.createObjectURL(xhr.response)
  166. link.download = fileName
  167. link.click()
  168. this.$message.success('下载成功')
  169. body.removeChild(link)
  170. window.URL.revokeObjectURL(link.href)
  171. }
  172. }
  173. xhr.send()
  174. },
  175. downDingtalkFile(row) {
  176. enclosureApi
  177. .downDingTalkFile({ id: row.id })
  178. .then((res) => {
  179. if (res.code == 200) {
  180. this.$message.success('下载成功')
  181. downloadFileByByte(res.data, row.fileName)
  182. }
  183. })
  184. .catch((err) => {
  185. this.$message.error('下载失败')
  186. console.error(err)
  187. })
  188. },
  189. // 重命名
  190. rename(id) {
  191. this.editFiles.id = id
  192. this.editVisible = true
  193. },
  194. // 编辑新名称
  195. async saveEditName() {
  196. let params = { ...this.editFiles }
  197. const [valid] = await to(this.$refs.editForm.validate())
  198. if (valid == false) return
  199. const [err, res] = await to(enclosureApi.updateEnclosure(params))
  200. if (err) return
  201. if (res.code == 200) this.getEnclosureList()
  202. else return
  203. this.editVisible = false
  204. },
  205. // 上传图片
  206. beforeAvatarUpload(file) {
  207. let flag1 = file.size < this.fileSettings.fileSize
  208. if (!flag1) {
  209. this.$message.warning('文件过大,请重新选择!')
  210. return false
  211. }
  212. let flag2 = this.fileSettings.fileTypes.split(',').includes('.' + file.name.split('.').pop())
  213. if (!flag2) {
  214. this.$message.warning('文件类型不符合,请重新选择!')
  215. return false
  216. }
  217. return true
  218. },
  219. // 上传
  220. uploadrequest(option) {
  221. let _this = this
  222. let url = process.env.VUE_APP_UPLOAD_WEED
  223. axios
  224. .post(url)
  225. .then(function (res) {
  226. if (res.data && res.data.fid && res.data.fid !== '') {
  227. option.action = `${process.env.VUE_APP_PROTOCOL}${res.data.publicUrl}/${res.data.fid}`
  228. let file_name = option.file.name
  229. let index = file_name.lastIndexOf('.')
  230. let file_extend = ''
  231. if (index > 0) {
  232. // 截取名称中的扩展名
  233. file_extend = file_name.substr(index + 1)
  234. }
  235. let uploadform = {
  236. fileName: file_name, // 资料名称
  237. fileSize: option.file.size.toString(), // 资料大小
  238. fileType: file_extend, // 资料文件类型
  239. fileUrl: `${process.env.VUE_APP_PROTOCOL}${res.data.publicUrl}/${res.data.fid}`, // 资料存储url
  240. fileSource: '附件上传',
  241. }
  242. asyncUploadFile(option).then(() => {
  243. let params = Object.assign({ ...uploadform, busId: _this.busId })
  244. _this.addEnclosure(params)
  245. // 一秒后删除上传信息
  246. setTimeout(() => {
  247. _this.$refs.uploadRef.clearFiles()
  248. }, 1000)
  249. })
  250. } else {
  251. _this.$message({
  252. type: 'warning',
  253. message: '未上传成功!请刷新界面重新上传!',
  254. })
  255. }
  256. })
  257. .catch(function () {
  258. _this.$message({
  259. type: 'warning',
  260. message: '未上传成功!请重新上传!',
  261. })
  262. })
  263. },
  264. // 新增附件接口
  265. async addEnclosure(params) {
  266. const [err, res] = await to(enclosureApi.createBusinessEnclosure(params))
  267. if (err) return
  268. if (res.code == 200) await this.getEnclosureList()
  269. },
  270. // 删除图片
  271. handleDel(row) {
  272. this.$confirm('确认删除?', '提示', {
  273. confirmButtonText: '确定',
  274. cancelButtonText: '取消',
  275. type: 'warning',
  276. })
  277. .then(async () => {
  278. const [err, res] = await to(enclosureApi.deleteBusinessEnclosure({ ids: [row.id] }))
  279. if (err) return
  280. if (res.code == 200) {
  281. this.$message({
  282. type: 'success',
  283. message: '删除成功!',
  284. })
  285. this.getEnclosureList()
  286. }
  287. })
  288. .catch(() => {})
  289. },
  290. },
  291. }
  292. </script>
  293. <style lang="scss" scoped>
  294. .enclosure-container {
  295. height: 100%;
  296. .mb10 {
  297. margin-bottom: 10px;
  298. }
  299. .collection {
  300. height: 100%;
  301. .el-row {
  302. height: 30px;
  303. }
  304. }
  305. .text-right {
  306. text-align: right;
  307. }
  308. }
  309. </style>