index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div class="long-ad-page">
  3. <div class="long-ad-container">
  4. <!-- 标题卡片 -->
  5. <el-card class="title-card" shadow="never">
  6. <span class="title-text">首页横幅广告</span>
  7. </el-card>
  8. <!-- 广告图片卡片 -->
  9. <el-card class="ad-card" shadow="hover" @click="handleClickAd">
  10. <el-image
  11. v-if="adData.imageUrl"
  12. :src="adData.imageUrl"
  13. fit="contain"
  14. class="ad-image"
  15. >
  16. <template #error>
  17. <div class="image-slot">
  18. <el-icon><Picture /></el-icon>
  19. <span>点击编辑广告</span>
  20. </div>
  21. </template>
  22. </el-image>
  23. <div v-else class="image-slot">
  24. <el-icon><Picture /></el-icon>
  25. <span>点击添加广告</span>
  26. </div>
  27. </el-card>
  28. <!-- 编辑表单卡片(点击后显示) -->
  29. <el-card v-if="showForm" class="form-card" shadow="never">
  30. <el-form :model="form" label-width="80px">
  31. <el-form-item label="链接:">
  32. <el-input v-model="form.link" placeholder="请输入链接地址" />
  33. </el-form-item>
  34. <el-form-item label="图片:">
  35. <div class="upload-area">
  36. <upload-image v-model="form.imageUrl" :limit="1" />
  37. <span class="upload-tip">建议尺寸:1200 x 300 像素</span>
  38. </div>
  39. </el-form-item>
  40. <el-form-item>
  41. <el-button type="primary" @click="handleSubmit">提交</el-button>
  42. <el-button @click="showForm = false">取消</el-button>
  43. </el-form-item>
  44. </el-form>
  45. </el-card>
  46. </div>
  47. </div>
  48. </template>
  49. <script setup name="GiftLongAd" lang="ts">
  50. import { ref, reactive, onMounted } from 'vue';
  51. import { ElMessage } from 'element-plus';
  52. import { Picture } from '@element-plus/icons-vue';
  53. import dayjs from 'dayjs';
  54. import { listAdContent, addAdContent, updateAdContent } from '@/api/ad/content';
  55. const AD_TYPE = 'gift_long_ad';
  56. const showForm = ref(false);
  57. const adData = ref({
  58. id: null as number | null,
  59. imageUrl: '',
  60. link: ''
  61. });
  62. const form = reactive({
  63. link: '',
  64. imageUrl: ''
  65. });
  66. const loadAdData = async () => {
  67. try {
  68. const res = await listAdContent({ adType: AD_TYPE, pageSize: 1 });
  69. const rows = res.rows || [];
  70. if (rows.length > 0) {
  71. const data = rows[0];
  72. adData.value.id = data.id;
  73. adData.value.imageUrl = data.imageUrl || '';
  74. adData.value.link = data.link || '';
  75. form.link = data.link || '';
  76. form.imageUrl = data.imageUrl || '';
  77. }
  78. } catch (error) {
  79. console.error('加载广告数据失败', error);
  80. }
  81. };
  82. const handleClickAd = () => {
  83. showForm.value = true;
  84. };
  85. const handleSubmit = async () => {
  86. try {
  87. const today = dayjs().format('YYYY-MM-DD');
  88. const future = dayjs().add(365, 'day').format('YYYY-MM-DD');
  89. const data = {
  90. id: adData.value.id,
  91. adType: AD_TYPE,
  92. title: '首页横幅广告',
  93. imageUrl: form.imageUrl,
  94. link: form.link,
  95. sort: 0,
  96. status: 1,
  97. color: '#ffffff',
  98. remark: '',
  99. extJson: '{}',
  100. startTime: today,
  101. endTime: future
  102. };
  103. if (adData.value.id) {
  104. await updateAdContent(data);
  105. } else {
  106. await addAdContent(data);
  107. }
  108. ElMessage.success('提交成功');
  109. showForm.value = false;
  110. await loadAdData();
  111. } catch (error) {
  112. ElMessage.error('提交失败');
  113. }
  114. };
  115. onMounted(() => {
  116. loadAdData();
  117. });
  118. </script>
  119. <style scoped lang="scss">
  120. .long-ad-page {
  121. min-height: 100vh;
  122. background: #f5f5f5;
  123. padding: 20px;
  124. }
  125. .long-ad-container {
  126. max-width: 1200px;
  127. margin: 0 auto;
  128. }
  129. .title-card {
  130. margin-bottom: 16px;
  131. :deep(.el-card__body) {
  132. padding: 12px 20px;
  133. }
  134. .title-text {
  135. font-size: 14px;
  136. font-weight: 500;
  137. color: #409eff;
  138. }
  139. }
  140. .ad-card {
  141. margin-bottom: 16px;
  142. cursor: pointer;
  143. transition: all 0.3s;
  144. &:hover {
  145. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  146. }
  147. :deep(.el-card__body) {
  148. padding: 0;
  149. }
  150. .ad-image {
  151. width: 100%;
  152. display: block;
  153. :deep(img) {
  154. width: 100%;
  155. height: auto;
  156. display: block;
  157. }
  158. }
  159. }
  160. .image-slot {
  161. display: flex;
  162. flex-direction: column;
  163. justify-content: center;
  164. align-items: center;
  165. width: 100%;
  166. height: 200px;
  167. background: #f5f7fa;
  168. color: #909399;
  169. font-size: 14px;
  170. .el-icon {
  171. font-size: 40px;
  172. margin-bottom: 10px;
  173. }
  174. }
  175. .form-card {
  176. :deep(.el-card__body) {
  177. padding: 20px;
  178. }
  179. }
  180. .upload-area {
  181. display: flex;
  182. flex-direction: column;
  183. gap: 10px;
  184. .upload-tip {
  185. color: #909399;
  186. font-size: 12px;
  187. }
  188. }
  189. </style>