index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <div class="title-page">
  3. <div class="title-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="titleList" border>
  16. <el-table-column label="标题名称" align="center" prop="title" min-width="140" />
  17. <el-table-column label="导航类型" align="center" prop="remark" min-width="120">
  18. <template #default="scope">
  19. {{ navTypeMap[scope.row.remark] || scope.row.remark }}
  20. </template>
  21. </el-table-column>
  22. <el-table-column label="链接地址" align="center" prop="link" :show-overflow-tooltip="true" min-width="240" />
  23. <el-table-column label="状态" align="center" width="100">
  24. <template #default="scope">
  25. <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
  26. {{ scope.row.status === 1 ? '显示' : '隐藏' }}
  27. </el-tag>
  28. </template>
  29. </el-table-column>
  30. <el-table-column label="排序" align="center" prop="sort" width="80" />
  31. <el-table-column label="操作" align="center" width="150">
  32. <template #default="scope">
  33. <span class="action-link primary" @click="handleUpdate(scope.row)">编辑</span>
  34. <span class="action-link danger" @click="handleDelete(scope.row)">删除</span>
  35. </template>
  36. </el-table-column>
  37. </el-table>
  38. <!-- 分页 -->
  39. <pagination
  40. v-show="total > 0"
  41. v-model:page="queryParams.pageNum"
  42. v-model:limit="queryParams.pageSize"
  43. :total="total"
  44. @pagination="getList"
  45. />
  46. </div>
  47. </div>
  48. <!-- 新增/编辑对话框 -->
  49. <el-dialog v-model="dialog.visible" :title="dialog.title" width="620px" append-to-body @close="cancel">
  50. <el-form ref="titleFormRef" :model="form" :rules="rules" label-width="90px">
  51. <el-row :gutter="20">
  52. <el-col :span="12">
  53. <el-form-item label="标题名称" prop="title">
  54. <el-input v-model="form.title" placeholder="请输入标题名称" />
  55. </el-form-item>
  56. </el-col>
  57. <el-col :span="12">
  58. <el-form-item label="标题链接" prop="link">
  59. <el-input v-model="form.link" placeholder="请输入标题链接" />
  60. </el-form-item>
  61. </el-col>
  62. </el-row>
  63. <el-row :gutter="20">
  64. <el-col :span="12">
  65. <el-form-item label="导航类型" prop="remark">
  66. <el-select v-model="form.remark" placeholder="请选择" style="width: 100%">
  67. <el-option
  68. v-for="item in navTypeOptions"
  69. :key="item.value"
  70. :label="item.label"
  71. :value="item.value"
  72. />
  73. </el-select>
  74. </el-form-item>
  75. </el-col>
  76. <el-col :span="12">
  77. <el-form-item label="排序" prop="sort">
  78. <el-input-number v-model="form.sort" :min="0" controls-position="right" style="width: 100%" />
  79. </el-form-item>
  80. </el-col>
  81. </el-row>
  82. <el-form-item label="状态" prop="status">
  83. <el-switch
  84. v-model="form.status"
  85. :active-value="1"
  86. :inactive-value="0"
  87. active-text="显示"
  88. inactive-text="隐藏"
  89. />
  90. </el-form-item>
  91. </el-form>
  92. <template #footer>
  93. <div class="dialog-footer">
  94. <el-button type="primary" @click="submitForm">确认</el-button>
  95. <el-button @click="cancel">取消</el-button>
  96. </div>
  97. </template>
  98. </el-dialog>
  99. </div>
  100. </template>
  101. <script setup name="GiftTitle" lang="ts">
  102. import { getCurrentInstance, onMounted, reactive, ref } from 'vue';
  103. import type { ComponentInternalInstance } from 'vue';
  104. import type { ElFormInstance } from 'element-plus';
  105. import dayjs from 'dayjs';
  106. import { listAdContent, addAdContent, updateAdContent, delAdContent } from '@/api/ad/content';
  107. import { listNavigation } from '@/api/system/navigation';
  108. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  109. const loading = ref(false);
  110. const total = ref(0);
  111. const titleList = ref<any[]>([]);
  112. const navTypeOptions = ref<{ label: string; value: string }[]>([]);
  113. // 导航类型映射
  114. const navTypeMap: Record<string, string> = {
  115. home: '首页导航',
  116. inner: '内页导航',
  117. search: '搜索导航',
  118. footer: '底部导航',
  119. float: '浮动导航'
  120. };
  121. // 加载导航类型选项
  122. const loadNavTypeOptions = async () => {
  123. try {
  124. const res = await listNavigation({ pageNum: 1, pageSize: 100 });
  125. const list = res.rows || res.data || [];
  126. // 提取navType并去重
  127. const typeSet = new Set<string>();
  128. list.forEach((item: any) => {
  129. if (item.navType) {
  130. typeSet.add(item.navType);
  131. }
  132. });
  133. // 转换为下拉选项
  134. navTypeOptions.value = Array.from(typeSet).map((type) => ({
  135. label: navTypeMap[type] || type,
  136. value: type
  137. }));
  138. } catch (error) {
  139. console.error('获取导航类型失败', error);
  140. // 接口失败时使用默认选项
  141. navTypeOptions.value = [
  142. { label: '首页导航', value: 'home' },
  143. { label: '内页导航', value: 'inner' },
  144. { label: '搜索导航', value: 'search' },
  145. { label: '底部导航', value: 'footer' },
  146. { label: '浮动导航', value: 'float' }
  147. ];
  148. }
  149. };
  150. const queryParams = ref({
  151. pageNum: 1,
  152. pageSize: 10,
  153. adType: 'gift_title',
  154. title: ''
  155. });
  156. const dialog = reactive<DialogOption>({
  157. visible: false,
  158. title: ''
  159. });
  160. const titleFormRef = ref<ElFormInstance>();
  161. const initFormData = {
  162. id: undefined as number | undefined,
  163. adType: 'gift_title',
  164. title: '',
  165. remark: '',
  166. link: '',
  167. imageUrl: '', // 后端校验需要,标题广告可用占位
  168. sort: 0,
  169. status: 1,
  170. color: '#ffffff',
  171. extJson: '{}',
  172. startTime: '',
  173. endTime: ''
  174. };
  175. const form = ref({ ...initFormData });
  176. const rules = reactive({
  177. title: [{ required: true, message: '标题名称不能为空', trigger: 'blur' }]
  178. });
  179. const getList = async () => {
  180. loading.value = true;
  181. const res = await listAdContent({
  182. pageNum: queryParams.value.pageNum,
  183. pageSize: queryParams.value.pageSize,
  184. adType: queryParams.value.adType,
  185. title: queryParams.value.title
  186. });
  187. titleList.value = res.rows || [];
  188. total.value = res.total || 0;
  189. loading.value = false;
  190. };
  191. const reset = () => {
  192. form.value = { ...initFormData };
  193. titleFormRef.value?.resetFields();
  194. };
  195. const handleAdd = () => {
  196. reset();
  197. const today = dayjs().format('YYYY-MM-DD');
  198. const future = dayjs().add(365, 'day').format('YYYY-MM-DD');
  199. form.value.startTime = today;
  200. form.value.endTime = future;
  201. form.value.imageUrl = form.value.imageUrl || 'https://via.placeholder.com/1x1';
  202. dialog.visible = true;
  203. dialog.title = '新增标题';
  204. };
  205. const handleUpdate = (row: any) => {
  206. reset();
  207. form.value = {
  208. ...row,
  209. adType: 'gift_title',
  210. status: row.status ?? 1,
  211. remark: row.remark || '',
  212. color: row.color || '#ffffff',
  213. extJson: row.extJson || '{}',
  214. imageUrl: row.imageUrl || 'https://via.placeholder.com/1x1',
  215. startTime: row.startTime ? dayjs(row.startTime).format('YYYY-MM-DD') : '',
  216. endTime: row.endTime ? dayjs(row.endTime).format('YYYY-MM-DD') : ''
  217. };
  218. dialog.visible = true;
  219. dialog.title = '编辑标题';
  220. };
  221. const submitForm = () => {
  222. titleFormRef.value?.validate((valid: boolean) => {
  223. if (valid) {
  224. const api = form.value.id ? updateAdContent : addAdContent;
  225. api(form.value).then(() => {
  226. proxy?.$modal.msgSuccess(form.value.id ? '修改成功' : '新增成功');
  227. dialog.visible = false;
  228. getList();
  229. });
  230. }
  231. });
  232. };
  233. const handleDelete = (row: any) => {
  234. proxy
  235. ?.$modal.confirm(`是否确认删除标题名称为"${row.title}"的数据项?`)
  236. .then(() => delAdContent(row.id))
  237. .then(() => {
  238. proxy?.$modal.msgSuccess('删除成功');
  239. getList();
  240. });
  241. };
  242. const cancel = () => {
  243. reset();
  244. dialog.visible = false;
  245. };
  246. onMounted(() => {
  247. loadNavTypeOptions();
  248. getList();
  249. });
  250. </script>
  251. <style scoped lang="scss">
  252. .title-page {
  253. min-height: 100vh;
  254. background: #f5f5f5;
  255. padding: 20px;
  256. }
  257. .title-container {
  258. max-width: 1200px;
  259. margin: 0 auto;
  260. }
  261. .header-card {
  262. background: #fff;
  263. border-radius: 4px;
  264. padding: 15px 20px;
  265. margin-bottom: 12px;
  266. display: flex;
  267. justify-content: space-between;
  268. align-items: center;
  269. .table-title {
  270. font-size: 16px;
  271. font-weight: 600;
  272. color: #303133;
  273. }
  274. .action-btns {
  275. display: flex;
  276. gap: 10px;
  277. }
  278. }
  279. .table-card {
  280. background: #fff;
  281. border-radius: 4px;
  282. padding: 20px;
  283. }
  284. .table-header {
  285. display: flex;
  286. justify-content: space-between;
  287. align-items: center;
  288. margin-bottom: 15px;
  289. .table-title {
  290. font-size: 16px;
  291. font-weight: 600;
  292. color: #303133;
  293. }
  294. .action-btns {
  295. display: flex;
  296. gap: 10px;
  297. }
  298. }
  299. .action-link {
  300. cursor: pointer;
  301. margin: 0 8px;
  302. &.primary {
  303. color: #409eff;
  304. &:hover { color: #66b1ff; }
  305. }
  306. &.danger {
  307. color: #f56c6c;
  308. &:hover { color: #f78989; }
  309. }
  310. }
  311. </style>