123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <script setup lang="tsx">
- import { ContentWrap } from '@/components/ContentWrap'
- import { Search } from '@/components/Search'
- import { useI18n } from '@/hooks/web/useI18n'
- import { ElButton, ElTooltip, ElMessage, ElButtonGroup } from 'element-plus'
- import { Table } from '@/components/Table'
- import { getTableListApi, delTableListApi, saveTableApi, updateTableApi } from '@/api/manage/img'
- import { useTable } from '@/hooks/web/useTable'
- import { ref, unref } from 'vue'
- import { useEmitt } from '@/hooks/event/useEmitt'
- import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
- import { TableSetting } from '@/components/TableSetting'
- import { usePageStore } from '@/store/modules/page'
- import { set } from 'lodash-es'
- import { useStorage } from '@/hooks/web/useStorage'
- import { Dialog } from '@/components/Dialog'
- import { ImgData } from '@/api/manage/types'
- import Write from './components/Write.vue'
- import { useIcon } from '@/hooks/web/useIcon'
- import { uploadFile } from '@/api/common'
- import { Qrcode } from '@/components/Qrcode'
- import { DictRowData } from '@/api/manage/types'
- import { getDict } from '@/api/manage/dict'
- defineOptions({
- name: 'ImgPage'
- })
- const QRIcon = useIcon({ icon: 'ic:round-qr-code' })
- const DeleteIcon = useIcon({ icon: 'ep:delete' })
- const DownLoadIcon = useIcon({ icon: 'ep:download' })
- const copyIcon = useIcon({ icon: 'ep:document-copy' })
- const EditIcon = useIcon({ icon: 'ep:edit' })
- const id = ref<string>('')
- const QrVisible = ref<boolean>(false)
- const QrSrc = ref<string>('')
- const { getStorage } = useStorage()
- const searchParams = ref({})
- const setSearchParams = (params: any) => {
- searchParams.value = params
- getList()
- }
- const currentRow = ref<ImgData>({
- bannerUrl: ''
- })
- const formState = ref<string>('add')
- const { tableRegister, tableState, tableMethods } = useTable({
- fetchDataApi: async () => {
- const { currentPage, pageSize } = tableState
- const res = await getTableListApi({
- pageNum: unref(currentPage),
- pageSize: unref(pageSize),
- ...unref(searchParams)
- })
- return {
- list: res.data.list,
- total: Number(res.data.totalCount)
- }
- },
- fetchDelApi: async () => {
- const res = await delTableListApi(unref(id))
- return !!res
- }
- })
- const { loading, dataList, total, currentPage, pageSize } = tableState
- const { getList, delList, setColumn } = tableMethods
- getList()
- useEmitt({
- name: 'getList',
- callback: (type: string) => {
- if (type === 'add') {
- currentPage.value = 1
- }
- getList()
- }
- })
- const bannerTypeList = ref<DictRowData[]>([])
- getDict({
- pid: '1693799827333287937'
- }).then((res) => {
- bannerTypeList.value = res.data
- })
- const { t } = useI18n()
- const appStore = usePageStore()
- const dialogVisible = ref(false)
- // const fileList = ref([])
- const crudSchemas: CrudSchema[] = [
- {
- field: 'bannerType',
- label: '类型',
- minWidth: 120,
- table: {
- hidden: false,
- formatter: (_: Recordable, _colomun, cellValue: string) => {
- let option = bannerTypeList.value.find((item) => item.code === cellValue)
- return option ? option.name : ''
- }
- },
- form: {
- component: 'Select',
- optionApi: async () => {
- const res = await getDict({
- pid: '1693799827333287937'
- })
- return res.data.map((e: DictRowData) => {
- return {
- label: e.name,
- value: e.code
- }
- })
- }
- }
- },
- {
- field: 'bannerUrl',
- label: '图片地址',
- minWidth: 120,
- search: {
- hidden: true
- },
- table: {
- hidden: false
- },
- form: {
- colProps: {
- span: 24
- },
- component: 'Upload',
- componentProps: {
- httpRequest: (data: any) => {
- delLoading.value = true
- let file = data.file
- let formData = new FormData()
- formData.append('file', file)
- uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
- console.log(file)
- console.log(response)
- let resUrl = ''
- if (file.size <= 1024 * 1000) {
- resUrl = response.data.virtualPath
- } else {
- resUrl = response.url.replace(
- 'xn-back.oss-cn-nanjing.aliyuncs.com',
- 'oss.dacundianzi.com'
- )
- }
- console.log(resUrl)
- currentRow.value.bannerUrl = resUrl
- const write = unref(writeRef)
- write?.setValues({
- bannerUrl: resUrl
- })
- delLoading.value = false
- })
- },
- class: 'filePageUploader',
- fileList: currentRow.value.bannerUrl
- ? [
- {
- url: currentRow.value.bannerUrl
- }
- ]
- : [],
- headers: {
- token: getStorage('token')
- },
- onSuccess: (response) => {
- // currentRow.value.bannerUrl = response.url
- },
- slots: {
- default: () => <ElButton type="primary">上传文件</ElButton>
- }
- }
- }
- },
- {
- field: 'bannerTarget',
- label: '跳转地址',
- minWidth: 120,
- search: {
- hidden: true
- },
- table: {
- hidden: false
- },
- form: {
- colProps: {
- span: 24
- }
- }
- },
- {
- field: 'remark',
- label: '备注',
- minWidth: 120,
- search: {
- hidden: true
- },
- table: {
- hidden: false
- },
- form: {
- colProps: {
- span: 24
- },
- componentProps: {
- type: 'textarea'
- }
- }
- },
- {
- field: 'createTime',
- label: '创建时间',
- minWidth: 160,
- table: {
- hidden: false
- },
- search: {
- hidden: true
- },
- form: {
- hidden: true
- }
- },
- {
- field: 'action',
- width: '250px',
- label: '操作',
- search: {
- hidden: true
- },
- form: {
- hidden: true
- },
- detail: {
- hidden: true
- },
- table: {
- hidden: false,
- fixed: 'right',
- slots: {
- default: (data: any) => {
- return (
- <ElButtonGroup>
- <ElTooltip content="编辑">
- <ElButton text icon={EditIcon} onClick={() => handleEdit(data.row)} />
- </ElTooltip>
- <ElTooltip content="二维码">
- <ElButton text icon={QRIcon} onClick={() => showQrCode(data.row)} />
- </ElTooltip>
- <ElTooltip content="下载">
- <ElButton text icon={DownLoadIcon} onClick={() => downloadFile(data.row)} />
- </ElTooltip>
- <ElTooltip content="复制链接">
- <ElButton text icon={copyIcon} onClick={() => onCopy(data.row)} />
- </ElTooltip>
- <ElTooltip content="删除">
- <ElButton text icon={DeleteIcon} type="danger" onClick={() => delData(data.row)} />
- </ElTooltip>
- </ElButtonGroup>
- )
- }
- }
- }
- }
- ]
- // @ts-ignore
- const getSchemas = () => {
- let localSchemas = appStore.getPageData['ImgPage']
- if (localSchemas && localSchemas.schemas) {
- let localSchemasArr = localSchemas.schemas
- for (let i = 0; i < localSchemasArr.length; i++) {
- let item = localSchemasArr[i]
- let index = crudSchemas.findIndex((e) => {
- return e.field == item.field
- })
- if (index > 0) {
- set(crudSchemas[index], 'table.hidden', item.table.hidden)
- }
- }
- }
- }
- getSchemas()
- let allSchemas = useCrudSchemas(crudSchemas).allSchemas
- // 修改列设置后调用
- const setSchemas = (schemas: CrudSchema[]) => {
- let arr = schemas.map((item) => {
- return {
- field: item.field,
- path: 'hidden',
- value: item.table ? item.table.hidden : false
- }
- })
- setColumn(arr)
- }
- const writeRef = ref<ComponentRef<typeof Write>>()
- const handleEdit = (row: ImgData) => {
- currentRow.value = row
- formState.value = 'edit'
- dialogVisible.value = true
- }
- const save = async () => {
- const write = unref(writeRef)
- const formData = await write?.submit()
- if (formData) {
- delLoading.value = true
- try {
- if (formData.id) {
- let res = await updateTableApi(formData)
- if (res) {
- getList()
- }
- } else {
- let res = await saveTableApi(formData)
- if (res) {
- currentPage.value = 1
- getList()
- }
- }
- } catch (error) {
- } finally {
- delLoading.value = false
- dialogVisible.value = false
- }
- }
- }
- const delLoading = ref(false)
- const delData = async (row: ImgData) => {
- if (!row.id) return
- id.value = row?.id
- delLoading.value = true
- await delList(unref(id).length).finally(() => {
- delLoading.value = false
- })
- }
- const showQrCode = (row: ImgData) => {
- QrSrc.value = row.bannerUrl
- QrVisible.value = true
- }
- const downloadFile = (row: ImgData) => {
- window.open(row.bannerUrl)
- }
- import useClipboard from 'vue-clipboard3'
- const { toClipboard } = useClipboard()
- const onCopy = async (row: ImgData) => {
- try {
- await toClipboard(row.bannerUrl)
- ElMessage({
- message: '复制成功!',
- type: 'success'
- })
- } catch (error) {
- console.log(error)
- ElMessage({
- message: '复制失败!',
- type: 'error'
- })
- }
- }
- const handleAdd = () => {
- formState.value = 'add'
- currentRow.value = {
- bannerUrl: ''
- }
- dialogVisible.value = true
- }
- </script>
- <template>
- <ContentWrap>
- <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
- <div class="mb-10px">
- <el-button type="primary" @click="handleAdd">上传文件</el-button>
- <TableSetting page="ImgPage" :data="crudSchemas" @set-schemas="setSchemas" />
- </div>
- <Table
- v-model:pageSize="pageSize"
- v-model:currentPage="currentPage"
- :columns="allSchemas.tableColumns"
- :data="dataList"
- :loading="loading"
- :pagination="{
- total: total
- }"
- @register="tableRegister"
- />
- </ContentWrap>
- <Dialog v-model="dialogVisible" :title="formState == 'add' ? '新增文件' : '编辑文件'">
- <Write ref="writeRef" :form-schema="allSchemas.formSchema" :current-row="currentRow" />
- <template #footer>
- <ElButton type="primary" :loading="delLoading" @click="save">
- {{ t('exampleDemo.save') }}
- </ElButton>
- <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
- </template>
- </Dialog>
- <Dialog v-model="QrVisible" width="430px" title="二维码">
- <Qrcode
- :width="395"
- :options="{
- color: {
- dark: '#55D187',
- light: '#ffffff'
- }
- }"
- :text="QrSrc"
- />
- <template #footer>
- <ElButton @click="QrVisible = false">{{ t('dialogDemo.close') }}</ElButton>
- </template>
- </Dialog>
- </template>
- @/hooks/event/useEmitt
- <style lang="less">
- .uploadBtn {
- display: inline-block;
- margin-right: 12px;
- }
- .filePageUploader {
- width: 100%;
- }
- </style>
|