TemplateManage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <!--
  2. * @Author: 半生瓜 1515706227@qq.com
  3. * @Date: 2024-05-06 19:31:40
  4. * @LastEditors: 半生瓜 1515706227@qq.com
  5. * @LastEditTime: 2024-05-27 23:58:07
  6. * @FilePath: \vue-element-plus-admin-mini\src\views\zy\Menu12.vue
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. -->
  9. <script setup lang="tsx">
  10. import { ElInput } from 'element-plus'
  11. import { ref } from 'vue'
  12. import { Dialog } from '@/components/Dialog'
  13. import { ContentWrap } from '@/components/ContentWrap'
  14. import { useI18n } from '@/hooks/web/useI18n'
  15. import { Table, TableColumn, TableSlotDefault } from '@/components/Table'
  16. import { getTemplate, upTemplate, saveTemplate, deleteTemplate } from '@/api/table'
  17. import { reactive, unref } from 'vue'
  18. import { ElButton, ElMessageBox, ElMessage, ElIcon } from 'element-plus'
  19. import { useTable } from '@/hooks/web/useTable'
  20. import { Search, Edit, Delete } from '@element-plus/icons-vue'
  21. import { useValidator } from '@/hooks/web/useValidator'
  22. import { Form, FormSchema } from '@/components/Form'
  23. import { useForm } from '@/hooks/web/useForm'
  24. import { BaseButton } from '@/components/Button'
  25. import { useUserStore } from '@/store/modules/user'
  26. const PATH_URL = window.VITE_API_BASE_PATH
  27. const { required } = useValidator()
  28. const text = ref('')
  29. const userStore = useUserStore()
  30. defineOptions({
  31. name: 'TemplateManage'
  32. })
  33. const { tableRegister, tableState, tableMethods } = useTable({
  34. fetchDataApi: async () => {
  35. const { currentPage, pageSize } = tableState
  36. const res = await getTemplate({
  37. page: unref(currentPage),
  38. pageSize: unref(pageSize),
  39. name: unref(text)
  40. })
  41. return {
  42. list: res.data.content,
  43. total: res.data.totalElements
  44. }
  45. }
  46. })
  47. const { loading, dataList, total, currentPage, pageSize } = tableState
  48. const { refresh, getElTableExpose } = tableMethods
  49. const { t } = useI18n()
  50. const columns = reactive<TableColumn[]>([
  51. {
  52. field: 'selection',
  53. type: 'selection'
  54. },
  55. {
  56. field: 'index',
  57. label: t('tableDemo.index'),
  58. type: 'index'
  59. },
  60. {
  61. field: 'zoneName',
  62. label: '模板名称'
  63. },
  64. {
  65. field: 'updateTime',
  66. label: '发布时间'
  67. },
  68. {
  69. field: 'action',
  70. label: t('tableDemo.action'),
  71. slots: {
  72. default: (data) => {
  73. return (
  74. <>
  75. <ElButton type="primary" onClick={() => edittab(data)}>
  76. {t('tableDemo.edit')}
  77. </ElButton>
  78. <ElButton type="danger" onClick={() => deletetab(data)}>
  79. {t('tableDemo.delete')}
  80. </ElButton>
  81. </>
  82. )
  83. }
  84. }
  85. }
  86. ])
  87. const imageUrl = ref('')
  88. const isedit = ref('添加')
  89. //删除
  90. const deletetab = async (data: TableSlotDefault) => {
  91. ElMessageBox.confirm('确定删除该数据?')
  92. .then(async () => {
  93. let arr = {
  94. zoneIds: [data.row.zoneId]
  95. }
  96. const res = await deleteTemplate(arr)
  97. console.log('kkkkkkss', res)
  98. refresh()
  99. if (res.code == 200) {
  100. ElMessage.success('删除成功')
  101. }
  102. refresh()
  103. })
  104. .catch(() => {
  105. // catch error
  106. })
  107. }
  108. const dialogVisible = ref(false)
  109. const Authorization = ref(`Bearer ${userStore.getToken}`)
  110. const headers = ref({ Authorization: Authorization.value })
  111. setInterval(() => {
  112. Authorization.value = `Bearer ${userStore.getToken}`
  113. console.log('Authorization', Authorization.value)
  114. headers.value = { Authorization: Authorization.value }
  115. }, 6000) //
  116. const { formRegister, formMethods } = useForm()
  117. const { getElFormExpose, getFormData } = formMethods
  118. const schema = reactive<FormSchema[]>([
  119. {
  120. field: 'zoneName',
  121. label: '模板名称',
  122. component: 'Input',
  123. colProps: {
  124. span: 24
  125. },
  126. componentProps: {
  127. style: {
  128. width: '100%'
  129. },
  130. placeholder: '请输入模板名称'
  131. },
  132. formItemProps: {
  133. rules: [required()]
  134. }
  135. },
  136. {
  137. field: 'typeNumber',
  138. label: '模板内容',
  139. component: 'Select',
  140. colProps: {
  141. span: 24
  142. },
  143. componentProps: {
  144. options: [
  145. {
  146. value: 1,
  147. label: '不含pak包'
  148. },
  149. {
  150. value: 2,
  151. label: '含pak包'
  152. }
  153. ],
  154. slots: {}
  155. },
  156. formItemProps: {
  157. rules: [required()]
  158. }
  159. },
  160. {
  161. field: 'thumbnail',
  162. component: 'Upload',
  163. label: '模板封面',
  164. formItemProps: {
  165. rules: [required()]
  166. },
  167. componentProps: {
  168. action: PATH_URL + '/zone/template/uploadImg',
  169. headers: headers,
  170. showFileList: false,
  171. onSuccess: async (_response, uploadFile) => {
  172. imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  173. const formData = await getFormData()
  174. formData.thumbnail = _response.data
  175. },
  176. beforeUpload: (rawFile) => {
  177. console.log('sadghasgdaj', rawFile.name.includes('jpeg'))
  178. console.log('kankangeshi', rawFile)
  179. if (rawFile.name.includes('jpeg')) {
  180. ElMessage.error('上传格式:png、jpg!')
  181. return false
  182. }
  183. if (rawFile.type !== 'image/png' && rawFile.type !== 'image/jpeg') {
  184. ElMessage.error('上传格式:png、jpg!')
  185. return false
  186. }
  187. return true
  188. },
  189. slots: {
  190. default: () => (
  191. <>
  192. {imageUrl.value ? <img src={imageUrl.value} class="avatar" /> : null}
  193. {!imageUrl.value ? (
  194. <ElIcon class="avatar-uploader-icon" size="large" style="font-size: 50px">
  195. +
  196. </ElIcon>
  197. ) : null}
  198. </>
  199. ),
  200. tip: () => <div class="el-upload__tip">上传格式:png、jpg</div>
  201. }
  202. }
  203. },
  204. {
  205. field: 'templateFilePath',
  206. component: 'Upload',
  207. label: `${t('formDemo.default')}`,
  208. colProps: {
  209. span: 24
  210. },
  211. formItemProps: {
  212. rules: [required()]
  213. },
  214. componentProps: {
  215. limit: 1,
  216. action: PATH_URL + '/zone/template/uploadTemplateZip',
  217. headers: headers,
  218. fileList: [],
  219. multiple: true,
  220. onPreview: (uploadFile) => {
  221. console.log('klklkl', uploadFile)
  222. },
  223. onRemove: async (file) => {
  224. console.log(file)
  225. const formData = await getFormData()
  226. formData.templateFilePath = ''
  227. formData.templateFileName = ''
  228. },
  229. onSuccess: async (_response, uploadFile) => {
  230. console.log('kankan_response', uploadFile)
  231. const formData = await getFormData()
  232. formData.templateFilePath = _response.data
  233. formData.templateFileName = uploadFile.name
  234. },
  235. beforeUpload: (rawFile) => {
  236. console.log('kokokoko', rawFile)
  237. if (rawFile.type !== 'application/x-zip-compressed' && rawFile.type !== 'application/zip') {
  238. ElMessage.error('只能上传ZIP文件')
  239. return false
  240. }
  241. return true
  242. },
  243. slots: {
  244. default: () => <BaseButton type="primary">点击上传</BaseButton>,
  245. tip: () => <div class="el-upload__tip">只能上传ZIP文件</div>
  246. }
  247. }
  248. }
  249. ])
  250. //编辑
  251. const edittab = async (data: TableSlotDefault) => {
  252. console.log('shanchu', data)
  253. isedit.value = '编辑'
  254. dialogVisible.value = true
  255. const formRef = await getElFormExpose()
  256. const formData = await getFormData()
  257. formData.zoneName = data.row.zoneName
  258. formData.templateFileName = data.row.templateFileName
  259. formData.templateFilePath = data.row.templateFilePath
  260. formData.thumbnail = data.row.thumbnail
  261. formData.pakPath = data.row.pakPath
  262. imageUrl.value = PATH_URL + data.row.thumbnail
  263. formData.zoneId = data.row.zoneId
  264. formData.typeNumber = data.row.typeNumber
  265. schema[3].componentProps.fileList = [
  266. {
  267. name: data.row.templateFileName,
  268. url: data.row.templateFilePath
  269. }
  270. ]
  271. }
  272. // 添加编辑模板管理
  273. const signIn = async () => {
  274. const formRef = await getElFormExpose()
  275. const formData = await getFormData()
  276. console.log('kakkkkak', formData)
  277. await formRef?.validate(async (isValid) => {
  278. if (isValid) {
  279. const res = await saveTemplate(formData)
  280. console.log('kankansss', res)
  281. if (res) {
  282. dialogVisible.value = false
  283. refresh()
  284. } else {
  285. ElMessage.error(res)
  286. }
  287. }
  288. })
  289. }
  290. //搜索
  291. const handersearch = function () {
  292. tableState.currentPage.value = 1
  293. refresh()
  294. }
  295. //多选删除
  296. const getSelections = async () => {
  297. const elTableRef = await getElTableExpose()
  298. const selections = elTableRef?.getSelectionRows()
  299. let arr = {
  300. zoneIds: []
  301. }
  302. if (selections) {
  303. arr.zoneIds = selections.map((item) => {
  304. return item.zoneId
  305. })
  306. }
  307. if (arr.zoneIds.length !== 0) {
  308. ElMessageBox.confirm('确定删除所选数据?')
  309. .then(async () => {
  310. console.log('kankan', arr)
  311. const res = await deleteTemplate(arr)
  312. console.log('kkkkkkss', res)
  313. if (res.code == 200) {
  314. ElMessage.success('删除成功')
  315. }
  316. refresh()
  317. })
  318. .catch(() => {
  319. // catch error
  320. })
  321. } else {
  322. ElMessage.warning('请选择数据')
  323. }
  324. }
  325. const tianjia = async () => {
  326. isedit.value = '添加'
  327. imageUrl.value = ''
  328. schema[2].componentProps.fileList = []
  329. dialogVisible.value = true
  330. }
  331. </script>
  332. <template>
  333. <ContentWrap>
  334. <div style="margin-bottom: 10px">
  335. <span style="color: #606266"> {{ t('levelDemo.menu') }}:</span>
  336. <el-input
  337. v-model="text"
  338. style="width: 200px; height: 43px; margin: 7px"
  339. placeholder="请输入模板名称"
  340. />
  341. <ElButton type="primary" size="large" @click="handersearch" :icon="Search">搜索</ElButton>
  342. <ElButton type="success" size="large" @click="tianjia" :icon="Edit">添加</ElButton>
  343. <ElButton type="danger" size="large" @click="getSelections" :icon="Delete">删除</ElButton>
  344. </div>
  345. <Table
  346. v-model:pageSize="pageSize"
  347. v-model:currentPage="currentPage"
  348. :columns="columns"
  349. :data="dataList"
  350. row-key="id"
  351. headerAlign="center"
  352. align="center"
  353. :loading="loading"
  354. sortable
  355. :pagination="{
  356. total: total
  357. }"
  358. @register="tableRegister"
  359. @refresh="refresh"
  360. />
  361. </ContentWrap>
  362. <Dialog v-model="dialogVisible" maxHeight="430px" width="35rem" :title="isedit">
  363. <Form :schema="schema" @register="formRegister" />
  364. <template #footer>
  365. <div style="width: 100%; display: flex; justify-content: center">
  366. <BaseButton
  367. style="font-size: 1rem; height: 2.2rem; margin: auto"
  368. type="primary"
  369. class="w-[15%]"
  370. @click="signIn"
  371. >
  372. 确定
  373. </BaseButton>
  374. </div>
  375. </template>
  376. </Dialog>
  377. </template>
  378. <style lang="less">
  379. .el-upload {
  380. position: relative;
  381. overflow: hidden;
  382. cursor: pointer;
  383. border: 1px dashed var(--el-border-color);
  384. border-radius: 6px;
  385. transition: var(--el-transition-duration-fast);
  386. }
  387. .el-upload:hover {
  388. border-color: var(--el-color-primary);
  389. }
  390. .el-icon.avatar-uploader-icon {
  391. width: 120px;
  392. height: 120px;
  393. font-size: 28px;
  394. color: #8c939d;
  395. text-align: center;
  396. }
  397. .avatar {
  398. width: 120px;
  399. height: 120px;
  400. }
  401. </style>