ImgPage.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/img'
  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 { ImgData } 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. defineOptions({
  22. name: 'ImgPage'
  23. })
  24. const QRIcon = useIcon({ icon: 'ic:round-qr-code' })
  25. const DeleteIcon = useIcon({ icon: 'ep:delete' })
  26. const DownLoadIcon = useIcon({ icon: 'ep:download' })
  27. const copyIcon = useIcon({ icon: 'ep:document-copy' })
  28. const EditIcon = useIcon({ icon: 'ep:edit' })
  29. const id = ref<string>('')
  30. const QrVisible = ref<boolean>(false)
  31. const QrSrc = ref<string>('')
  32. const { getStorage } = useStorage()
  33. const searchParams = ref({})
  34. const setSearchParams = (params: any) => {
  35. searchParams.value = params
  36. getList()
  37. }
  38. const currentRow = ref<ImgData>({
  39. bannerUrl: ''
  40. })
  41. const formState = ref<string>('add')
  42. const { tableRegister, tableState, tableMethods } = useTable({
  43. fetchDataApi: async () => {
  44. const { currentPage, pageSize } = tableState
  45. const res = await getTableListApi({
  46. pageNum: unref(currentPage),
  47. pageSize: unref(pageSize),
  48. ...unref(searchParams)
  49. })
  50. return {
  51. list: res.data.list,
  52. total: Number(res.data.totalCount)
  53. }
  54. },
  55. fetchDelApi: async () => {
  56. const res = await delTableListApi(unref(id))
  57. return !!res
  58. }
  59. })
  60. const { loading, dataList, total, currentPage, pageSize } = tableState
  61. const { getList, delList, setColumn } = tableMethods
  62. getList()
  63. useEmitt({
  64. name: 'getList',
  65. callback: (type: string) => {
  66. if (type === 'add') {
  67. currentPage.value = 1
  68. }
  69. getList()
  70. }
  71. })
  72. const { t } = useI18n()
  73. const appStore = usePageStore()
  74. const dialogVisible = ref(false)
  75. // const fileList = ref([])
  76. const crudSchemas: CrudSchema[] = [
  77. {
  78. field: 'bannerType',
  79. label: '类型',
  80. minWidth: 120,
  81. table: {
  82. hidden: false,
  83. formatter: (_: Recordable, _colomun, cellValue: string) => {
  84. return cellValue
  85. ? cellValue === '0'
  86. ? '首页'
  87. : cellValue === '1'
  88. ? '中间'
  89. : cellValue === '2'
  90. ? '底部'
  91. : ''
  92. : ''
  93. }
  94. },
  95. form: {
  96. component: 'Select',
  97. componentProps: {
  98. options: [
  99. {
  100. label: '首页',
  101. value: '0'
  102. },
  103. {
  104. label: '中间',
  105. value: '1'
  106. },
  107. {
  108. label: '底部',
  109. value: '2'
  110. }
  111. ]
  112. }
  113. }
  114. },
  115. {
  116. field: 'bannerUrl',
  117. label: '图片地址',
  118. minWidth: 120,
  119. search: {
  120. hidden: true
  121. },
  122. table: {
  123. hidden: false
  124. },
  125. form: {
  126. colProps: {
  127. span: 24
  128. },
  129. component: 'Upload',
  130. componentProps: {
  131. action: '/api/sysApk/uploadFile',
  132. limit: 1,
  133. class: 'filePageUploader',
  134. fileList: currentRow.value.bannerUrl
  135. ? [
  136. {
  137. url: currentRow.value.bannerUrl
  138. }
  139. ]
  140. : [],
  141. headers: {
  142. token: getStorage('token')
  143. },
  144. onSuccess: (response) => {
  145. currentRow.value.bannerUrl = response.data.virtualPath
  146. },
  147. onExceed: (_files, responses) => {
  148. currentRow.value.bannerUrl = responses[0].response.data.virtualPath
  149. },
  150. slots: {
  151. default: () => <ElButton type="primary">上传文件</ElButton>
  152. }
  153. }
  154. }
  155. },
  156. {
  157. field: 'bannerTarget',
  158. label: '跳转地址',
  159. minWidth: 120,
  160. search: {
  161. hidden: true
  162. },
  163. table: {
  164. hidden: false
  165. },
  166. form: {
  167. colProps: {
  168. span: 24
  169. }
  170. }
  171. },
  172. {
  173. field: 'remark',
  174. label: '备注',
  175. minWidth: 120,
  176. search: {
  177. hidden: true
  178. },
  179. table: {
  180. hidden: false
  181. },
  182. form: {
  183. colProps: {
  184. span: 24
  185. },
  186. componentProps: {
  187. type: 'textarea'
  188. }
  189. }
  190. },
  191. {
  192. field: 'createTime',
  193. label: '创建时间',
  194. minWidth: 160,
  195. table: {
  196. hidden: false
  197. },
  198. search: {
  199. hidden: true
  200. },
  201. form: {
  202. hidden: true
  203. }
  204. },
  205. {
  206. field: 'action',
  207. width: '250px',
  208. label: '操作',
  209. search: {
  210. hidden: true
  211. },
  212. form: {
  213. hidden: true
  214. },
  215. detail: {
  216. hidden: true
  217. },
  218. table: {
  219. hidden: false,
  220. fixed: 'right',
  221. slots: {
  222. default: (data: any) => {
  223. return (
  224. <ElButtonGroup>
  225. <ElTooltip content="编辑">
  226. <ElButton text icon={EditIcon} onClick={() => handleEdit(data.row)} />
  227. </ElTooltip>
  228. <ElTooltip content="二维码">
  229. <ElButton text icon={QRIcon} onClick={() => showQrCode(data.row)} />
  230. </ElTooltip>
  231. <ElTooltip content="下载">
  232. <ElButton text icon={DownLoadIcon} onClick={() => downloadFile(data.row)} />
  233. </ElTooltip>
  234. <ElTooltip content="复制链接">
  235. <ElButton text icon={copyIcon} onClick={() => onCopy(data.row)} />
  236. </ElTooltip>
  237. <ElTooltip content="删除">
  238. <ElButton text icon={DeleteIcon} type="danger" onClick={() => delData(data.row)} />
  239. </ElTooltip>
  240. </ElButtonGroup>
  241. )
  242. }
  243. }
  244. }
  245. }
  246. ]
  247. // @ts-ignore
  248. const getSchemas = () => {
  249. let localSchemas = appStore.getPageData['ImgPage']
  250. if (localSchemas && localSchemas.schemas) {
  251. let localSchemasArr = localSchemas.schemas
  252. for (let i = 0; i < localSchemasArr.length; i++) {
  253. let item = localSchemasArr[i]
  254. let index = crudSchemas.findIndex((e) => {
  255. return e.field == item.field
  256. })
  257. if (index > 0) {
  258. set(crudSchemas[index], 'table.hidden', item.table.hidden)
  259. }
  260. }
  261. }
  262. }
  263. getSchemas()
  264. let allSchemas = useCrudSchemas(crudSchemas).allSchemas
  265. // 修改列设置后调用
  266. const setSchemas = (schemas: CrudSchema[]) => {
  267. let arr = schemas.map((item) => {
  268. return {
  269. field: item.field,
  270. path: 'hidden',
  271. value: item.table ? item.table.hidden : false
  272. }
  273. })
  274. setColumn(arr)
  275. }
  276. const writeRef = ref<ComponentRef<typeof Write>>()
  277. const handleEdit = (row: ImgData) => {
  278. currentRow.value = row
  279. formState.value = 'edit'
  280. dialogVisible.value = true
  281. }
  282. const save = async () => {
  283. const write = unref(writeRef)
  284. const formData = await write?.submit()
  285. if (formData) {
  286. delLoading.value = true
  287. try {
  288. if (formData.id) {
  289. let res = await updateTableApi(formData)
  290. if (res) {
  291. currentPage.value = 1
  292. getList()
  293. }
  294. } else {
  295. let res = await saveTableApi(formData)
  296. if (res) {
  297. currentPage.value = 1
  298. getList()
  299. }
  300. }
  301. } catch (error) {
  302. } finally {
  303. delLoading.value = false
  304. dialogVisible.value = false
  305. }
  306. }
  307. }
  308. const delLoading = ref(false)
  309. const delData = async (row: ImgData) => {
  310. if (!row.id) return
  311. id.value = row?.id
  312. delLoading.value = true
  313. await delList(unref(id).length).finally(() => {
  314. delLoading.value = false
  315. })
  316. }
  317. const showQrCode = (row: ImgData) => {
  318. QrSrc.value = row.bannerUrl
  319. QrVisible.value = true
  320. }
  321. const downloadFile = (row: ImgData) => {
  322. window.open(row.bannerUrl)
  323. }
  324. const onCopy = (row: ImgData) => {
  325. navigator.clipboard.writeText(row.bannerUrl).then(() => {
  326. ElMessage({
  327. message: '复制成功!',
  328. type: 'success'
  329. })
  330. })
  331. }
  332. const handleAdd = () => {
  333. formState.value = 'add'
  334. currentRow.value = {
  335. bannerUrl: ''
  336. }
  337. dialogVisible.value = true
  338. }
  339. </script>
  340. <template>
  341. <ContentWrap>
  342. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  343. <div class="mb-10px">
  344. <el-button type="primary" @click="handleAdd">上传文件</el-button>
  345. <TableSetting page="ImgPage" :data="crudSchemas" @set-schemas="setSchemas" />
  346. </div>
  347. <Table
  348. v-model:pageSize="pageSize"
  349. v-model:currentPage="currentPage"
  350. :columns="allSchemas.tableColumns"
  351. :data="dataList"
  352. :loading="loading"
  353. :pagination="{
  354. total: total
  355. }"
  356. @register="tableRegister"
  357. />
  358. </ContentWrap>
  359. <Dialog v-model="dialogVisible" :title="formState == 'add' ? '新增文件' : '编辑文件'">
  360. <Write ref="writeRef" :form-schema="allSchemas.formSchema" :current-row="currentRow" />
  361. <template #footer>
  362. <ElButton type="primary" :loading="delLoading" @click="save">
  363. {{ t('exampleDemo.save') }}
  364. </ElButton>
  365. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  366. </template>
  367. </Dialog>
  368. <Dialog v-model="QrVisible" width="430px" title="二维码">
  369. <Qrcode
  370. :width="395"
  371. :options="{
  372. color: {
  373. dark: '#55D187',
  374. light: '#ffffff'
  375. }
  376. }"
  377. :text="QrSrc"
  378. />
  379. <template #footer>
  380. <ElButton @click="QrVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  381. </template>
  382. </Dialog>
  383. </template>
  384. @/hooks/event/useEmitt
  385. <style lang="less">
  386. .uploadBtn {
  387. display: inline-block;
  388. margin-right: 12px;
  389. }
  390. .filePageUploader {
  391. width: 100%;
  392. }
  393. </style>