index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <script setup lang="ts">
  2. import RegularPage from '@/components/RegularPage.vue'
  3. import { onMounted, ref, reactive } from 'vue'
  4. import type { BaseTableColumns } from 'tdesign-vue-next'
  5. import CarousalDialog from '@/pages/carousal/components/CarousalDialog.vue'
  6. import { activeCarousal, deleteCarousal, getCarousalList, inactiveCarousal } from '@/api/carousal'
  7. import { BrowseIcon } from 'tdesign-icons-vue-next'
  8. import type { CarousalResult } from '@/model/carousals'
  9. const columns: BaseTableColumns = [
  10. {
  11. title: 'ID',
  12. colKey: 'id',
  13. width: 100,
  14. },
  15. {
  16. title: '轮播图',
  17. colKey: 'imageUrl',
  18. width: 100,
  19. },
  20. {
  21. title: '显示状态',
  22. colKey: 'status',
  23. width: 100,
  24. },
  25. {
  26. title: '操作',
  27. colKey: 'operation',
  28. width: 100,
  29. }
  30. ]
  31. const loading = ref<boolean>(false)
  32. const data = ref<CarousalResult[]>([])
  33. const pagination = reactive<{page:number,size:number,total:number}>({
  34. page: 1,
  35. size: 10,
  36. total:0
  37. })
  38. const query = reactive<{page:number,size:number}>({
  39. page:1,
  40. size:10,
  41. })
  42. const carousalDialogVisible = ref<boolean>(false)
  43. const isEdit = ref<boolean>(false)
  44. const currentTableData = ref<CarousalResult | {}>({})
  45. const fetchSaveCarousalData = async () :Promise<void> => {
  46. /* TODO: 获取轮播图表格数据 */
  47. query.page = pagination.page
  48. query.size = pagination.size
  49. const getCarousalListApi = await getCarousalList(query)
  50. data.value = getCarousalListApi.data
  51. pagination.total = getCarousalListApi.pagination.total
  52. }
  53. onMounted(fetchSaveCarousalData)
  54. const editClick = (row:object):void => {
  55. isEdit.value = true
  56. currentTableData.value = Object.assign({}, row)
  57. carousalDialogVisible.value = true
  58. }
  59. const showClick = async (res:CarousalResult):Promise<void> => {
  60. if (res.status === 'active'){
  61. await inactiveCarousal(res.id)
  62. } else {
  63. await activeCarousal(res.id)
  64. }
  65. await fetchSaveCarousalData()
  66. }
  67. const delClick = async (row:CarousalResult) :Promise<void>=> {
  68. await deleteCarousal(row.id)
  69. await fetchSaveCarousalData()
  70. }
  71. const handleCreateCarousal = () :void => {
  72. isEdit.value = false
  73. carousalDialogVisible.value = true
  74. currentTableData.value = {}
  75. }
  76. const onPageChange = ():void => {
  77. /* TODO: 分页切换 */
  78. }
  79. const handleSuccessDialog = async () :Promise<void> => {
  80. await fetchSaveCarousalData()
  81. carousalDialogVisible.value = false
  82. }
  83. </script>
  84. <template>
  85. <RegularPage title="轮播图管理">
  86. <template #right-area>
  87. <TButton @click="handleCreateCarousal">创建轮播图</TButton>
  88. </template>
  89. <TTable
  90. class="w-full h-full"
  91. row-key="id"
  92. height="92%"
  93. table-layout="auto"
  94. :data="data"
  95. :loading="loading"
  96. :columns="columns"
  97. cell-empty-content="-"
  98. :paginationAffixedBottom="true"
  99. :pagination="{
  100. total: pagination.total,
  101. current: pagination.page,
  102. pageSize: pagination.size,
  103. onChange: onPageChange
  104. }"
  105. >
  106. <template #imageUrl="{row}">
  107. <div class="tdesign-demo-image-viewer__base">
  108. <t-image-viewer :images="[row.imageUrl]">
  109. <template #trigger="{ open }">
  110. <div class="tdesign-demo-image-viewer__ui-image">
  111. <img alt="test" :src="row.imageUrl" class="tdesign-demo-image-viewer__ui-image--img" />
  112. <div class="tdesign-demo-image-viewer__ui-image--hover" @click="open">
  113. <span><BrowseIcon size="1.4em" /> 预览</span>
  114. </div>
  115. </div>
  116. </template>
  117. </t-image-viewer>
  118. </div>
  119. </template>
  120. <template #status="{row}">
  121. <TSpace>
  122. <TTag :theme="row.status === 'active' ? 'success' : 'danger'" variant="light">{{row.status === 'active' ? '显示中' : '已关闭'}}</TTag>
  123. </TSpace>
  124. </template>
  125. <template #roles="{ row }">
  126. <TSpace>
  127. <TTag v-for="role in row.roles" :key="role.id" theme="success" variant="light">{{
  128. role.label
  129. }}</TTag>
  130. </TSpace>
  131. </template>
  132. <template #operation="{ row }">
  133. <TButton variant="text" size="small" theme="primary" @click="editClick(row)">编辑</TButton>
  134. <TButton variant="text" size="small" theme="primary" @click="showClick(row)">{{ row.status === 'active' ? '关闭显示' : '开启显示'}}</TButton>
  135. <TButton variant="text" size="small" theme="danger" @click="delClick(row)">删除</TButton>
  136. </template>
  137. </TTable>
  138. <CarousalDialog
  139. :carousal="currentTableData"
  140. v-model:visible="carousalDialogVisible"
  141. :isEdit="isEdit"
  142. @success="handleSuccessDialog"
  143. headerTitle="轮播图"
  144. ></CarousalDialog>
  145. </RegularPage>
  146. </template>
  147. <style scoped>
  148. :deep(.t-card__body) {
  149. flex: 1;
  150. min-height: 0;
  151. }
  152. .tdesign-demo-image-viewer__ui-image {
  153. width: 100%;
  154. height: 100%;
  155. display: inline-flex;
  156. position: relative;
  157. justify-content: center;
  158. align-items: center;
  159. border-radius: var(--td-radius-small);
  160. overflow: hidden;
  161. }
  162. .tdesign-demo-image-viewer__ui-image--hover {
  163. width: 100%;
  164. height: 100%;
  165. display: flex;
  166. justify-content: center;
  167. align-items: center;
  168. position: absolute;
  169. left: 0;
  170. top: 0;
  171. opacity: 0;
  172. background-color: rgba(0, 0, 0, 0.6);
  173. color: var(--td-text-color-anti);
  174. line-height: 22px;
  175. transition: 0.2s;
  176. }
  177. .tdesign-demo-image-viewer__ui-image:hover .tdesign-demo-image-viewer__ui-image--hover {
  178. opacity: 1;
  179. cursor: pointer;
  180. }
  181. .tdesign-demo-image-viewer__ui-image--img {
  182. width: auto;
  183. height: auto;
  184. max-width: 100%;
  185. max-height: 100%;
  186. cursor: pointer;
  187. position: absolute;
  188. }
  189. .tdesign-demo-image-viewer__ui-image--icons .tdesign-demo-icon {
  190. cursor: pointer;
  191. }
  192. .tdesign-demo-image-viewer__base {
  193. width: 100px;
  194. height: 100px;
  195. border: 4px solid var(--td-bg-color-secondarycontainer);
  196. border-radius: var(--td-radius-medium);
  197. }
  198. </style>