ImgPage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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(file.size <= 1024 * 1000 ? formData : file).then((response) => {
  134. console.log(file)
  135. console.log(response)
  136. let resUrl = ''
  137. if (file.size <= 1024 * 1000) {
  138. resUrl = response.data.virtualPath
  139. } else {
  140. resUrl = response.url.replace(
  141. 'xn-back.oss-cn-nanjing.aliyuncs.com',
  142. 'oss.dacundianzi.com'
  143. )
  144. }
  145. console.log(resUrl)
  146. currentRow.value.bannerUrl = resUrl
  147. const write = unref(writeRef)
  148. write?.setValues({
  149. bannerUrl: resUrl
  150. })
  151. delLoading.value = false
  152. })
  153. },
  154. class: 'filePageUploader',
  155. fileList: currentRow.value.bannerUrl
  156. ? [
  157. {
  158. url: currentRow.value.bannerUrl
  159. }
  160. ]
  161. : [],
  162. headers: {
  163. token: getStorage('token')
  164. },
  165. onSuccess: (response) => {
  166. // currentRow.value.bannerUrl = response.url
  167. },
  168. slots: {
  169. default: () => <ElButton type="primary">上传文件</ElButton>
  170. }
  171. }
  172. }
  173. },
  174. {
  175. field: 'bannerTarget',
  176. label: '跳转地址',
  177. minWidth: 120,
  178. search: {
  179. hidden: true
  180. },
  181. table: {
  182. hidden: false
  183. },
  184. form: {
  185. colProps: {
  186. span: 24
  187. }
  188. }
  189. },
  190. {
  191. field: 'remark',
  192. label: '备注',
  193. minWidth: 120,
  194. search: {
  195. hidden: true
  196. },
  197. table: {
  198. hidden: false
  199. },
  200. form: {
  201. colProps: {
  202. span: 24
  203. },
  204. componentProps: {
  205. type: 'textarea'
  206. }
  207. }
  208. },
  209. {
  210. field: 'createTime',
  211. label: '创建时间',
  212. minWidth: 160,
  213. table: {
  214. hidden: false
  215. },
  216. search: {
  217. hidden: true
  218. },
  219. form: {
  220. hidden: true
  221. }
  222. },
  223. {
  224. field: 'action',
  225. width: '250px',
  226. label: '操作',
  227. search: {
  228. hidden: true
  229. },
  230. form: {
  231. hidden: true
  232. },
  233. detail: {
  234. hidden: true
  235. },
  236. table: {
  237. hidden: false,
  238. fixed: 'right',
  239. slots: {
  240. default: (data: any) => {
  241. return (
  242. <ElButtonGroup>
  243. <ElTooltip content="编辑">
  244. <ElButton text icon={EditIcon} onClick={() => handleEdit(data.row)} />
  245. </ElTooltip>
  246. <ElTooltip content="二维码">
  247. <ElButton text icon={QRIcon} onClick={() => showQrCode(data.row)} />
  248. </ElTooltip>
  249. <ElTooltip content="下载">
  250. <ElButton text icon={DownLoadIcon} onClick={() => downloadFile(data.row)} />
  251. </ElTooltip>
  252. <ElTooltip content="复制链接">
  253. <ElButton text icon={copyIcon} onClick={() => onCopy(data.row)} />
  254. </ElTooltip>
  255. <ElTooltip content="删除">
  256. <ElButton text icon={DeleteIcon} type="danger" onClick={() => delData(data.row)} />
  257. </ElTooltip>
  258. </ElButtonGroup>
  259. )
  260. }
  261. }
  262. }
  263. }
  264. ]
  265. // @ts-ignore
  266. const getSchemas = () => {
  267. let localSchemas = appStore.getPageData['ImgPage']
  268. if (localSchemas && localSchemas.schemas) {
  269. let localSchemasArr = localSchemas.schemas
  270. for (let i = 0; i < localSchemasArr.length; i++) {
  271. let item = localSchemasArr[i]
  272. let index = crudSchemas.findIndex((e) => {
  273. return e.field == item.field
  274. })
  275. if (index > 0) {
  276. set(crudSchemas[index], 'table.hidden', item.table.hidden)
  277. }
  278. }
  279. }
  280. }
  281. getSchemas()
  282. let allSchemas = useCrudSchemas(crudSchemas).allSchemas
  283. // 修改列设置后调用
  284. const setSchemas = (schemas: CrudSchema[]) => {
  285. let arr = schemas.map((item) => {
  286. return {
  287. field: item.field,
  288. path: 'hidden',
  289. value: item.table ? item.table.hidden : false
  290. }
  291. })
  292. setColumn(arr)
  293. }
  294. const writeRef = ref<ComponentRef<typeof Write>>()
  295. const handleEdit = (row: ImgData) => {
  296. currentRow.value = row
  297. formState.value = 'edit'
  298. dialogVisible.value = true
  299. }
  300. const save = async () => {
  301. const write = unref(writeRef)
  302. const formData = await write?.submit()
  303. if (formData) {
  304. delLoading.value = true
  305. try {
  306. if (formData.id) {
  307. let res = await updateTableApi(formData)
  308. if (res) {
  309. getList()
  310. }
  311. } else {
  312. let res = await saveTableApi(formData)
  313. if (res) {
  314. currentPage.value = 1
  315. getList()
  316. }
  317. }
  318. } catch (error) {
  319. } finally {
  320. delLoading.value = false
  321. dialogVisible.value = false
  322. }
  323. }
  324. }
  325. const delLoading = ref(false)
  326. const delData = async (row: ImgData) => {
  327. if (!row.id) return
  328. id.value = row?.id
  329. delLoading.value = true
  330. await delList(unref(id).length).finally(() => {
  331. delLoading.value = false
  332. })
  333. }
  334. const showQrCode = (row: ImgData) => {
  335. QrSrc.value = row.bannerUrl
  336. QrVisible.value = true
  337. }
  338. const downloadFile = (row: ImgData) => {
  339. window.open(row.bannerUrl)
  340. }
  341. import useClipboard from 'vue-clipboard3'
  342. const { toClipboard } = useClipboard()
  343. const onCopy = async (row: ImgData) => {
  344. try {
  345. await toClipboard(row.bannerUrl)
  346. ElMessage({
  347. message: '复制成功!',
  348. type: 'success'
  349. })
  350. } catch (error) {
  351. console.log(error)
  352. ElMessage({
  353. message: '复制失败!',
  354. type: 'error'
  355. })
  356. }
  357. }
  358. const handleAdd = () => {
  359. formState.value = 'add'
  360. currentRow.value = {
  361. bannerUrl: ''
  362. }
  363. dialogVisible.value = true
  364. }
  365. </script>
  366. <template>
  367. <ContentWrap>
  368. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  369. <div class="mb-10px">
  370. <el-button type="primary" @click="handleAdd">上传文件</el-button>
  371. <TableSetting page="ImgPage" :data="crudSchemas" @set-schemas="setSchemas" />
  372. </div>
  373. <Table
  374. v-model:pageSize="pageSize"
  375. v-model:currentPage="currentPage"
  376. :columns="allSchemas.tableColumns"
  377. :data="dataList"
  378. :loading="loading"
  379. :pagination="{
  380. total: total
  381. }"
  382. @register="tableRegister"
  383. />
  384. </ContentWrap>
  385. <Dialog v-model="dialogVisible" :title="formState == 'add' ? '新增文件' : '编辑文件'">
  386. <Write ref="writeRef" :form-schema="allSchemas.formSchema" :current-row="currentRow" />
  387. <template #footer>
  388. <ElButton type="primary" :loading="delLoading" @click="save">
  389. {{ t('exampleDemo.save') }}
  390. </ElButton>
  391. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  392. </template>
  393. </Dialog>
  394. <Dialog v-model="QrVisible" width="430px" title="二维码">
  395. <Qrcode
  396. :width="395"
  397. :options="{
  398. color: {
  399. dark: '#55D187',
  400. light: '#ffffff'
  401. }
  402. }"
  403. :text="QrSrc"
  404. />
  405. <template #footer>
  406. <ElButton @click="QrVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  407. </template>
  408. </Dialog>
  409. </template>
  410. @/hooks/event/useEmitt
  411. <style lang="less">
  412. .uploadBtn {
  413. display: inline-block;
  414. margin-right: 12px;
  415. }
  416. .filePageUploader {
  417. width: 100%;
  418. }
  419. </style>