ImgPage.vue 10 KB

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