upload.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div v-loading.fullscreen.lock="fullscreenLoading">
  3. <div class="gva-table-box">
  4. <warning-bar
  5. title="点击“文件名/备注”可以编辑文件名或者备注内容。"
  6. />
  7. <div class="gva-btn-list">
  8. <upload-common
  9. :image-common="imageCommon"
  10. @on-success="getTableData"
  11. />
  12. <upload-image
  13. :image-url="imageUrl"
  14. :file-size="512"
  15. :max-w-h="1080"
  16. @on-success="getTableData"
  17. />
  18. <el-input
  19. v-model="search.keyword"
  20. class="keyword"
  21. placeholder="请输入文件名或备注"
  22. />
  23. <el-button
  24. type="primary"
  25. icon="search"
  26. @click="getTableData"
  27. >查询</el-button>
  28. </div>
  29. <el-table :data="tableData">
  30. <el-table-column
  31. align="left"
  32. label="预览"
  33. width="100"
  34. >
  35. <template #default="scope">
  36. <CustomPic
  37. pic-type="file"
  38. :pic-src="scope.row.url"
  39. preview
  40. />
  41. </template>
  42. </el-table-column>
  43. <el-table-column
  44. align="left"
  45. label="日期"
  46. prop="UpdatedAt"
  47. width="180"
  48. >
  49. <template #default="scope">
  50. <div>{{ formatDate(scope.row.UpdatedAt) }}</div>
  51. </template>
  52. </el-table-column>
  53. <el-table-column
  54. align="left"
  55. label="文件名/备注"
  56. prop="name"
  57. width="180"
  58. >
  59. <template #default="scope">
  60. <div
  61. class="name"
  62. @click="editFileNameFunc(scope.row)"
  63. >{{ scope.row.name }}</div>
  64. </template>
  65. </el-table-column>
  66. <el-table-column
  67. align="left"
  68. label="链接"
  69. prop="url"
  70. min-width="300"
  71. />
  72. <el-table-column
  73. align="left"
  74. label="标签"
  75. prop="tag"
  76. width="100"
  77. >
  78. <template #default="scope">
  79. <el-tag
  80. :type="scope.row.tag === 'jpg' ? 'info' : 'success'"
  81. disable-transitions
  82. >{{ scope.row.tag }}
  83. </el-tag>
  84. </template>
  85. </el-table-column>
  86. <el-table-column
  87. align="left"
  88. label="操作"
  89. width="160"
  90. >
  91. <template #default="scope">
  92. <el-button
  93. icon="download"
  94. type="primary"
  95. link
  96. @click="downloadFile(scope.row)"
  97. >下载</el-button>
  98. <el-button
  99. icon="delete"
  100. type="primary"
  101. link
  102. @click="deleteFileFunc(scope.row)"
  103. >删除</el-button>
  104. </template>
  105. </el-table-column>
  106. </el-table>
  107. <div class="gva-pagination">
  108. <el-pagination
  109. :current-page="page"
  110. :page-size="pageSize"
  111. :page-sizes="[10, 30, 50, 100]"
  112. :style="{ float: 'right', padding: '20px' }"
  113. :total="total"
  114. layout="total, sizes, prev, pager, next, jumper"
  115. @current-change="handleCurrentChange"
  116. @size-change="handleSizeChange"
  117. />
  118. </div>
  119. </div>
  120. </div>
  121. </template>
  122. <script setup>
  123. import { getFileList, deleteFile, editFileName } from '@/api/fileUploadAndDownload'
  124. import { downloadImage } from '@/utils/downloadImg'
  125. import CustomPic from '@/components/customPic/index.vue'
  126. import UploadImage from '@/components/upload/image.vue'
  127. import UploadCommon from '@/components/upload/common.vue'
  128. import { formatDate } from '@/utils/format'
  129. import WarningBar from '@/components/warningBar/warningBar.vue'
  130. import { ref } from 'vue'
  131. import { ElMessage, ElMessageBox } from 'element-plus'
  132. defineOptions({
  133. name: 'Upload',
  134. })
  135. const path = ref(import.meta.env.VITE_BASE_API)
  136. const imageUrl = ref('')
  137. const imageCommon = ref('')
  138. const page = ref(1)
  139. const total = ref(0)
  140. const pageSize = ref(10)
  141. const search = ref({})
  142. const tableData = ref([])
  143. // 分页
  144. const handleSizeChange = (val) => {
  145. pageSize.value = val
  146. getTableData()
  147. }
  148. const handleCurrentChange = (val) => {
  149. page.value = val
  150. getTableData()
  151. }
  152. // 查询
  153. const getTableData = async() => {
  154. const table = await getFileList({ page: page.value, pageSize: pageSize.value, ...search.value })
  155. if (table.code === 0) {
  156. tableData.value = table.data.list
  157. total.value = table.data.total
  158. page.value = table.data.page
  159. pageSize.value = table.data.pageSize
  160. }
  161. }
  162. getTableData()
  163. const deleteFileFunc = async(row) => {
  164. ElMessageBox.confirm('此操作将永久删除文件, 是否继续?', '提示', {
  165. confirmButtonText: '确定',
  166. cancelButtonText: '取消',
  167. type: 'warning',
  168. })
  169. .then(async() => {
  170. const res = await deleteFile(row)
  171. if (res.code === 0) {
  172. ElMessage({
  173. type: 'success',
  174. message: '删除成功!',
  175. })
  176. if (tableData.value.length === 1 && page.value > 1) {
  177. page.value--
  178. }
  179. getTableData()
  180. }
  181. })
  182. .catch(() => {
  183. ElMessage({
  184. type: 'info',
  185. message: '已取消删除',
  186. })
  187. })
  188. }
  189. const downloadFile = (row) => {
  190. if (row.url.indexOf('http://') > -1 || row.url.indexOf('https://') > -1) {
  191. downloadImage(row.url, row.name)
  192. } else {
  193. debugger
  194. downloadImage(path.value + '/' + row.url, row.name)
  195. }
  196. }
  197. /**
  198. * 编辑文件名或者备注
  199. * @param row
  200. * @returns {Promise<void>}
  201. */
  202. const editFileNameFunc = async(row) => {
  203. ElMessageBox.prompt('请输入文件名或者备注', '编辑', {
  204. confirmButtonText: '确定',
  205. cancelButtonText: '取消',
  206. inputPattern: /\S/,
  207. inputErrorMessage: '不能为空',
  208. inputValue: row.name
  209. }).then(async({ value }) => {
  210. row.name = value
  211. // console.log(row)
  212. const res = await editFileName(row)
  213. if (res.code === 0) {
  214. ElMessage({
  215. type: 'success',
  216. message: '编辑成功!',
  217. })
  218. getTableData()
  219. }
  220. }).catch(() => {
  221. ElMessage({
  222. type: 'info',
  223. message: '取消修改'
  224. })
  225. })
  226. }
  227. </script>
  228. <style scoped>
  229. .name {
  230. cursor: pointer;
  231. }
  232. </style>