FilePage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <script setup lang="tsx">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { Search } from '@/components/Search'
  4. import { useI18n } from '@/hooks/web/useI18n'
  5. import { ElButton, ElTooltip, ElMessage, ElButtonGroup } from 'element-plus'
  6. import { Table } from '@/components/Table'
  7. import { getTableListApi, delTableListApi, saveTableApi, updateTableApi } from '@/api/manage/file'
  8. import { useTable } from '@/hooks/web/useTable'
  9. import { ref, unref } from 'vue'
  10. import { useEmitt } from '@/hooks/event/useEmitt'
  11. import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
  12. import { TableSetting } from '@/components/TableSetting'
  13. import { usePageStore } from '@/store/modules/page'
  14. import { set } from 'lodash-es'
  15. import { useStorage } from '@/hooks/web/useStorage'
  16. import { Dialog } from '@/components/Dialog'
  17. import { FileData } from '@/api/manage/types'
  18. import Write from './components/Write.vue'
  19. import { useIcon } from '@/hooks/web/useIcon'
  20. import { Qrcode } from '@/components/Qrcode'
  21. import { uploadFile } from '@/api/common'
  22. defineOptions({
  23. name: 'FilePage'
  24. })
  25. const QRIcon = useIcon({ icon: 'ic:round-qr-code' })
  26. const DeleteIcon = useIcon({ icon: 'ep:delete' })
  27. const DownLoadIcon = useIcon({ icon: 'ep:download' })
  28. const copyIcon = useIcon({ icon: 'ep:document-copy' })
  29. const EditIcon = useIcon({ icon: 'ep:edit' })
  30. const id = ref<string>('')
  31. const QrVisible = ref<boolean>(false)
  32. const QrSrc = ref<string>('')
  33. const { getStorage } = useStorage()
  34. const searchParams = ref({})
  35. const setSearchParams = (params: any) => {
  36. searchParams.value = params
  37. getList()
  38. }
  39. const currentRow = ref<FileData>({
  40. virtualPath: ''
  41. })
  42. const formState = ref<string>('add')
  43. const { tableRegister, tableState, tableMethods } = useTable({
  44. fetchDataApi: async () => {
  45. const { currentPage, pageSize } = tableState
  46. const res = await getTableListApi({
  47. pageNum: unref(currentPage),
  48. pageSize: unref(pageSize),
  49. ...unref(searchParams)
  50. })
  51. return {
  52. list: res.data.list,
  53. total: Number(res.data.totalCount)
  54. }
  55. },
  56. fetchDelApi: async () => {
  57. const res = await delTableListApi(unref(id))
  58. return !!res
  59. }
  60. })
  61. const { loading, dataList, total, currentPage, pageSize } = tableState
  62. const { getList, delList, setColumn } = tableMethods
  63. getList()
  64. useEmitt({
  65. name: 'getList',
  66. callback: (type: string) => {
  67. if (type === 'add') {
  68. currentPage.value = 1
  69. }
  70. getList()
  71. }
  72. })
  73. const { t } = useI18n()
  74. const appStore = usePageStore()
  75. const dialogVisible = ref(false)
  76. // const fileList = ref([])
  77. const crudSchemas: CrudSchema[] = [
  78. {
  79. field: 'name',
  80. label: '名称',
  81. minWidth: 100,
  82. table: {
  83. hidden: false
  84. },
  85. form: {
  86. colProps: {
  87. span: 24
  88. }
  89. }
  90. },
  91. {
  92. field: 'version',
  93. label: '版本号',
  94. minWidth: 100,
  95. search: {
  96. hidden: true
  97. },
  98. table: {
  99. hidden: false
  100. }
  101. },
  102. {
  103. field: 'code',
  104. label: '版本序列号',
  105. minWidth: 100,
  106. table: {
  107. hidden: false
  108. },
  109. search: {
  110. hidden: true
  111. },
  112. form: {
  113. component: 'InputNumber'
  114. }
  115. },
  116. {
  117. field: 'fileName',
  118. label: '文件名',
  119. minWidth: 120,
  120. table: {
  121. hidden: false
  122. },
  123. form: {
  124. hidden: true
  125. }
  126. },
  127. {
  128. field: 'virtualPath',
  129. label: '文件链接',
  130. minWidth: 120,
  131. search: {
  132. hidden: true
  133. },
  134. table: {
  135. hidden: false
  136. },
  137. form: {
  138. colProps: {
  139. span: 24
  140. },
  141. component: 'Upload',
  142. componentProps: {
  143. httpRequest: (data: any) => {
  144. delLoading.value = true
  145. let file = data.file
  146. let formData = new FormData()
  147. formData.append('file', file)
  148. uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
  149. console.log(response)
  150. let resUrl = ''
  151. if (file.size <= 1024 * 1000) {
  152. resUrl = response.data.virtualPath
  153. } else {
  154. resUrl = response.url.replace(
  155. 'xn-back.oss-cn-nanjing.aliyuncs.com',
  156. 'oss.dacundianzi.com'
  157. )
  158. }
  159. currentRow.value = {
  160. ...currentRow.value,
  161. virtualPath: resUrl,
  162. fileName: file.name
  163. }
  164. const write = unref(writeRef)
  165. write?.setValues({
  166. bannerUrl: resUrl
  167. })
  168. delLoading.value = false
  169. })
  170. // uploadFile(formData).then((response) => {
  171. // currentRow.value = {
  172. // ...currentRow.value,
  173. // ...response.data
  174. // }
  175. // delLoading.value = false
  176. // })
  177. },
  178. class: 'filePageUploader',
  179. fileList: currentRow.value.virtualPath
  180. ? [
  181. {
  182. url: currentRow.value.virtualPath
  183. }
  184. ]
  185. : [],
  186. headers: {
  187. token: getStorage('token')
  188. },
  189. onSuccess: (response) => {
  190. currentRow.value = {
  191. ...currentRow.value,
  192. ...response.data
  193. }
  194. const write = unref(writeRef)
  195. write?.setValues({
  196. ...response.data
  197. })
  198. },
  199. slots: {
  200. default: () => <ElButton type="primary">上传文件</ElButton>
  201. }
  202. }
  203. }
  204. },
  205. {
  206. field: 'filePath',
  207. label: '文件路径',
  208. minWidth: 120,
  209. search: {
  210. hidden: true
  211. },
  212. table: {
  213. hidden: false
  214. },
  215. form: {
  216. hidden: true
  217. }
  218. },
  219. {
  220. field: 'content',
  221. label: '版本更新说明',
  222. minWidth: 120,
  223. search: {
  224. hidden: true
  225. },
  226. table: {
  227. hidden: false
  228. },
  229. form: {
  230. colProps: {
  231. span: 24
  232. },
  233. componentProps: {
  234. type: 'textarea'
  235. }
  236. }
  237. },
  238. {
  239. field: 'remark',
  240. label: '备注',
  241. minWidth: 120,
  242. search: {
  243. hidden: true
  244. },
  245. table: {
  246. hidden: false
  247. },
  248. form: {
  249. colProps: {
  250. span: 24
  251. },
  252. componentProps: {
  253. type: 'textarea'
  254. }
  255. }
  256. },
  257. {
  258. field: 'createTime',
  259. label: '创建时间',
  260. minWidth: 160,
  261. table: {
  262. hidden: false
  263. },
  264. search: {
  265. hidden: true
  266. },
  267. form: {
  268. hidden: true
  269. }
  270. },
  271. {
  272. field: 'action',
  273. width: '250px',
  274. label: '操作',
  275. search: {
  276. hidden: true
  277. },
  278. form: {
  279. hidden: true
  280. },
  281. detail: {
  282. hidden: true
  283. },
  284. table: {
  285. hidden: false,
  286. fixed: 'right',
  287. slots: {
  288. default: (data: any) => {
  289. return (
  290. <ElButtonGroup>
  291. <ElTooltip content="编辑">
  292. <ElButton text icon={EditIcon} onClick={() => handleEdit(data.row)} />
  293. </ElTooltip>
  294. <ElTooltip content="二维码">
  295. <ElButton text icon={QRIcon} onClick={() => showQrCode(data.row)} />
  296. </ElTooltip>
  297. <ElTooltip content="下载">
  298. <ElButton text icon={DownLoadIcon} onClick={() => downloadFile(data.row)} />
  299. </ElTooltip>
  300. <ElTooltip content="复制链接">
  301. <ElButton text icon={copyIcon} onClick={() => onCopy(data.row)} />
  302. </ElTooltip>
  303. <ElTooltip content="删除">
  304. <ElButton text icon={DeleteIcon} type="danger" onClick={() => delData(data.row)} />
  305. </ElTooltip>
  306. </ElButtonGroup>
  307. )
  308. }
  309. }
  310. }
  311. }
  312. ]
  313. // @ts-ignore
  314. const getSchemas = () => {
  315. let localSchemas = appStore.getPageData['FilePage']
  316. if (localSchemas && localSchemas.schemas) {
  317. let localSchemasArr = localSchemas.schemas
  318. for (let i = 0; i < localSchemasArr.length; i++) {
  319. let item = localSchemasArr[i]
  320. let index = crudSchemas.findIndex((e) => {
  321. return e.field == item.field
  322. })
  323. if (index > 0) {
  324. set(crudSchemas[index], 'table.hidden', item.table.hidden)
  325. }
  326. }
  327. }
  328. }
  329. getSchemas()
  330. let allSchemas = useCrudSchemas(crudSchemas).allSchemas
  331. // 修改列设置后调用
  332. const setSchemas = (schemas: CrudSchema[]) => {
  333. let arr = schemas.map((item) => {
  334. return {
  335. field: item.field,
  336. path: 'hidden',
  337. value: item.table ? item.table.hidden : false
  338. }
  339. })
  340. setColumn(arr)
  341. }
  342. const writeRef = ref<ComponentRef<typeof Write>>()
  343. const handleEdit = (row: FileData) => {
  344. currentRow.value = row
  345. formState.value = 'edit'
  346. dialogVisible.value = true
  347. }
  348. const save = async () => {
  349. const write = unref(writeRef)
  350. const formData = await write?.submit()
  351. if (formData) {
  352. delLoading.value = true
  353. try {
  354. if (formData.id) {
  355. let res = await updateTableApi(formData)
  356. if (res) {
  357. currentPage.value = 1
  358. getList()
  359. }
  360. } else {
  361. let res = await saveTableApi(formData)
  362. if (res) {
  363. currentPage.value = 1
  364. getList()
  365. }
  366. }
  367. } catch (error) {
  368. } finally {
  369. delLoading.value = false
  370. dialogVisible.value = false
  371. }
  372. }
  373. }
  374. const delLoading = ref(false)
  375. const delData = async (row: FileData) => {
  376. if (!row.id) return
  377. id.value = row?.id
  378. delLoading.value = true
  379. await delList(unref(id).length).finally(() => {
  380. delLoading.value = false
  381. })
  382. }
  383. const showQrCode = (row: FileData) => {
  384. QrSrc.value = row.virtualPath
  385. QrVisible.value = true
  386. }
  387. const downloadFile = (row: FileData) => {
  388. window.open(row.virtualPath)
  389. }
  390. import useClipboard from 'vue-clipboard3'
  391. const { toClipboard } = useClipboard()
  392. const onCopy = async (row: FileData) => {
  393. try {
  394. await toClipboard(row.virtualPath)
  395. ElMessage({
  396. message: '复制成功!',
  397. type: 'success'
  398. })
  399. } catch (error) {
  400. console.log(error)
  401. ElMessage({
  402. message: '复制失败!',
  403. type: 'error'
  404. })
  405. }
  406. }
  407. const handleAdd = () => {
  408. formState.value = 'add'
  409. currentRow.value = {
  410. virtualPath: ''
  411. }
  412. dialogVisible.value = true
  413. }
  414. </script>
  415. <template>
  416. <ContentWrap>
  417. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  418. <div class="mb-10px">
  419. <el-button type="primary" @click="handleAdd">上传文件</el-button>
  420. <TableSetting page="FilePage" :data="crudSchemas" @set-schemas="setSchemas" />
  421. </div>
  422. <Table
  423. v-model:pageSize="pageSize"
  424. v-model:currentPage="currentPage"
  425. :columns="allSchemas.tableColumns"
  426. :data="dataList"
  427. :loading="loading"
  428. :pagination="{
  429. total: total
  430. }"
  431. @register="tableRegister"
  432. />
  433. </ContentWrap>
  434. <Dialog v-model="dialogVisible" :title="formState == 'add' ? '新增文件' : '编辑文件'">
  435. <Write ref="writeRef" :form-schema="allSchemas.formSchema" :current-row="currentRow" />
  436. <template #footer>
  437. <ElButton type="primary" :loading="delLoading" @click="save">
  438. {{ t('exampleDemo.save') }}
  439. </ElButton>
  440. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  441. </template>
  442. </Dialog>
  443. <Dialog v-model="QrVisible" width="430px" title="二维码">
  444. <Qrcode
  445. :width="395"
  446. :options="{
  447. color: {
  448. dark: '#55D187',
  449. light: '#ffffff'
  450. }
  451. }"
  452. :text="QrSrc"
  453. />
  454. <template #footer>
  455. <ElButton @click="QrVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  456. </template>
  457. </Dialog>
  458. </template>
  459. @/hooks/event/useEmitt
  460. <style lang="less">
  461. .uploadBtn {
  462. display: inline-block;
  463. margin-right: 12px;
  464. }
  465. .filePageUploader {
  466. width: 100%;
  467. }
  468. </style>