index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div class="icon-page">
  3. <div class="icon-container">
  4. <!-- 标题栏(独立卡片) -->
  5. <div class="header-card">
  6. <div class="table-title">福礼图标广告信息列表</div>
  7. <div class="action-btns">
  8. <el-button type="primary" plain icon="Plus" @click="handleAdd">添加图标广告</el-button>
  9. <el-button icon="Refresh" @click="getList">刷新</el-button>
  10. </div>
  11. </div>
  12. <!-- 表格区域(独立卡片) -->
  13. <div class="table-card">
  14. <!-- 表格 -->
  15. <el-table v-loading="loading" :data="iconList" border>
  16. <el-table-column label="分类标题" align="center" prop="title" min-width="160" />
  17. <el-table-column label="封面图片" align="center" width="140">
  18. <template #default="scope">
  19. <el-image
  20. :src="scope.row.imageUrl"
  21. :preview-src-list="[scope.row.imageUrl]"
  22. fit="cover"
  23. style="width: 64px; height: 64px"
  24. lazy
  25. >
  26. <template #error>
  27. <div class="image-slot">
  28. <el-icon><Picture /></el-icon>
  29. </div>
  30. </template>
  31. </el-image>
  32. </template>
  33. </el-table-column>
  34. <el-table-column label="链接地址" align="center" prop="link" :show-overflow-tooltip="true" min-width="220">
  35. <template #default="scope">
  36. <span class="link-text">{{ scope.row.link || '#' }}</span>
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="状态" align="center" width="100">
  40. <template #default="scope">
  41. <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
  42. {{ scope.row.status === 1 ? '显示' : '隐藏' }}
  43. </el-tag>
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="排序" align="center" prop="sort" width="80" />
  47. <el-table-column label="操作" align="center" width="150">
  48. <template #default="scope">
  49. <span class="action-link primary" @click="handleUpdate(scope.row)">编辑</span>
  50. <span class="action-link danger" @click="handleDelete(scope.row)">删除</span>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. <!-- 分页 -->
  55. <pagination
  56. v-show="total > 0"
  57. v-model:page="queryParams.pageNum"
  58. v-model:limit="queryParams.pageSize"
  59. :total="total"
  60. @pagination="getList"
  61. />
  62. </div>
  63. </div>
  64. <!-- 新增/编辑对话框 -->
  65. <el-dialog v-model="dialog.visible" :title="dialog.title" width="620px" append-to-body @close="cancel">
  66. <el-form ref="iconFormRef" :model="form" :rules="rules" label-width="90px">
  67. <el-row :gutter="20">
  68. <el-col :span="12">
  69. <el-form-item label="公告标题" prop="title">
  70. <el-input v-model="form.title" placeholder="请输入公告标题" />
  71. </el-form-item>
  72. </el-col>
  73. <el-col :span="12">
  74. <el-form-item label="公告链接" prop="link">
  75. <el-input v-model="form.link" placeholder="请输入公告链接" />
  76. </el-form-item>
  77. </el-col>
  78. </el-row>
  79. <el-row :gutter="20">
  80. <el-col :span="12">
  81. <el-form-item label="排序" prop="sort">
  82. <el-input-number v-model="form.sort" :min="0" controls-position="right" style="width: 100%" />
  83. </el-form-item>
  84. </el-col>
  85. <el-col :span="12">
  86. <el-form-item label="状态" prop="status">
  87. <el-switch
  88. v-model="form.status"
  89. :active-value="1"
  90. :inactive-value="0"
  91. active-text="显示"
  92. inactive-text="隐藏"
  93. />
  94. </el-form-item>
  95. </el-col>
  96. </el-row>
  97. <el-form-item label="封面图片" prop="imageUrl">
  98. <image-upload v-model="form.imageUrl" :limit="1" />
  99. </el-form-item>
  100. </el-form>
  101. <template #footer>
  102. <div class="dialog-footer">
  103. <el-button type="primary" @click="submitForm">确认</el-button>
  104. <el-button @click="cancel">取消</el-button>
  105. </div>
  106. </template>
  107. </el-dialog>
  108. </div>
  109. </template>
  110. <script setup name="GiftIcon" lang="ts">
  111. import { getCurrentInstance, onMounted, reactive, ref } from 'vue';
  112. import type { ComponentInternalInstance } from 'vue';
  113. import type { FormInstance } from 'element-plus';
  114. import dayjs from 'dayjs';
  115. import { listAdContent, addAdContent, updateAdContent, delAdContent } from '@/api/ad/content';
  116. import { listByIds } from '@/api/system/oss';
  117. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  118. const loading = ref(false);
  119. const total = ref(0);
  120. const iconList = ref<any[]>([]);
  121. const queryParams = ref({
  122. pageNum: 1,
  123. pageSize: 10,
  124. adType: 'gift_icon'
  125. });
  126. const dialog = reactive<DialogOption>({
  127. visible: false,
  128. title: ''
  129. });
  130. const iconFormRef = ref<FormInstance>();
  131. const initFormData = {
  132. id: undefined as number | undefined,
  133. adType: 'gift_icon',
  134. title: '',
  135. imageUrl: '',
  136. link: '',
  137. sort: 0,
  138. status: 1,
  139. color: '#ffffff',
  140. remark: '',
  141. extJson: '{}',
  142. startTime: '',
  143. endTime: ''
  144. };
  145. const form = ref({ ...initFormData });
  146. const rules = reactive({
  147. title: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }]
  148. });
  149. const getList = async () => {
  150. loading.value = true;
  151. const res = await listAdContent({
  152. pageNum: queryParams.value.pageNum,
  153. pageSize: queryParams.value.pageSize,
  154. adType: queryParams.value.adType
  155. });
  156. const rows = res.rows || [];
  157. // 把 ossId 转换成真正的图片 URL
  158. const ossIds = rows.map((item: any) => item.imageUrl).filter((id: any) => id).join(',');
  159. if (ossIds) {
  160. try {
  161. const ossRes = await listByIds(ossIds);
  162. const ossMap = new Map(ossRes.data.map((oss: any) => [String(oss.ossId), oss.url]));
  163. rows.forEach((item: any) => {
  164. // 保存原始ossId用于编辑回显
  165. item.imageOssId = item.imageUrl;
  166. if (item.imageUrl && ossMap.has(String(item.imageUrl))) {
  167. item.imageUrl = ossMap.get(String(item.imageUrl));
  168. }
  169. });
  170. } catch (e) {
  171. console.error('获取图片URL失败', e);
  172. }
  173. }
  174. iconList.value = rows;
  175. total.value = res.total || 0;
  176. loading.value = false;
  177. };
  178. const reset = () => {
  179. form.value = { ...initFormData };
  180. iconFormRef.value?.resetFields();
  181. };
  182. const handleAdd = () => {
  183. reset();
  184. const today = dayjs().format('YYYY-MM-DD');
  185. const future = dayjs().add(365, 'day').format('YYYY-MM-DD');
  186. form.value.startTime = today;
  187. form.value.endTime = future;
  188. dialog.visible = true;
  189. dialog.title = '新增图标广告';
  190. };
  191. const handleUpdate = (row: any) => {
  192. reset();
  193. form.value = {
  194. ...row,
  195. adType: 'gift_icon',
  196. // 编辑时用ossId,这样image-upload组件才能正确回显
  197. imageUrl: row.imageOssId || row.imageUrl || '',
  198. status: row.status ?? 1,
  199. color: row.color || '#ffffff',
  200. remark: row.remark || '',
  201. extJson: row.extJson || '{}',
  202. startTime: row.startTime ? dayjs(row.startTime).format('YYYY-MM-DD') : '',
  203. endTime: row.endTime ? dayjs(row.endTime).format('YYYY-MM-DD') : ''
  204. };
  205. dialog.visible = true;
  206. dialog.title = '编辑图标广告';
  207. };
  208. const submitForm = () => {
  209. iconFormRef.value?.validate((valid: boolean) => {
  210. if (valid) {
  211. const api = form.value.id ? updateAdContent : addAdContent;
  212. api(form.value).then(() => {
  213. proxy?.$modal.msgSuccess(form.value.id ? '修改成功' : '新增成功');
  214. dialog.visible = false;
  215. getList();
  216. });
  217. }
  218. });
  219. };
  220. const handleDelete = (row: any) => {
  221. proxy?.$modal.confirm(`是否确认删除分类标题为\"${row.title}\"的数据项?`).then(() => {
  222. delAdContent(row.id).then(() => {
  223. proxy?.$modal.msgSuccess('删除成功');
  224. getList();
  225. });
  226. });
  227. };
  228. const cancel = () => {
  229. reset();
  230. dialog.visible = false;
  231. };
  232. onMounted(() => {
  233. getList();
  234. });
  235. </script>
  236. <style scoped lang="scss">
  237. .icon-page {
  238. min-height: 100vh;
  239. background: #f5f5f5;
  240. padding: 20px;
  241. }
  242. .icon-container {
  243. max-width: 1200px;
  244. margin: 0 auto;
  245. }
  246. .header-card {
  247. background: #fff;
  248. border-radius: 4px;
  249. padding: 15px 20px;
  250. margin-bottom: 12px;
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. .table-title {
  255. font-size: 16px;
  256. font-weight: 600;
  257. color: #303133;
  258. }
  259. .action-btns {
  260. display: flex;
  261. gap: 10px;
  262. }
  263. }
  264. .table-card {
  265. background: #fff;
  266. border-radius: 4px;
  267. padding: 20px;
  268. }
  269. .image-slot {
  270. width: 64px;
  271. height: 64px;
  272. display: flex;
  273. align-items: center;
  274. justify-content: center;
  275. background: #f5f7fa;
  276. color: #c0c4cc;
  277. border-radius: 4px;
  278. }
  279. .link-text {
  280. color: #409eff;
  281. }
  282. .action-link {
  283. cursor: pointer;
  284. margin: 0 8px;
  285. &.primary {
  286. color: #409eff;
  287. &:hover { color: #66b1ff; }
  288. }
  289. &.danger {
  290. color: #f56c6c;
  291. &:hover { color: #f78989; }
  292. }
  293. }
  294. </style>