index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <div class="material-page">
  3. <!-- 返回按钮 -->
  4. <div class="header">
  5. <van-icon name="arrow-left" @click="goBack" />
  6. <span>{{ $t('material.materialCenter') }}</span>
  7. <span></span>
  8. </div>
  9. <!-- 搜索 -->
  10. <div class="search-bar">
  11. <van-search
  12. v-model="keyword"
  13. :placeholder="$t('material.searchMaterial')"
  14. shape="round"
  15. background="transparent"
  16. @search="onSearch"
  17. />
  18. </div>
  19. <!-- 类型筛选 -->
  20. <div class="type-tabs">
  21. <div
  22. class="tab-item"
  23. :class="{ active: type === '' }"
  24. @click="type = ''"
  25. >
  26. {{ $t('common.all') }}
  27. </div>
  28. <div
  29. class="tab-item"
  30. :class="{ active: type === 'image' }"
  31. @click="type = 'image'"
  32. >
  33. {{ $t('material.image') }}
  34. </div>
  35. <div
  36. class="tab-item"
  37. :class="{ active: type === 'text' }"
  38. @click="type = 'text'"
  39. >
  40. {{ $t('material.text') }}
  41. </div>
  42. <div
  43. class="tab-item"
  44. :class="{ active: type === 'video' }"
  45. @click="type = 'video'"
  46. >
  47. {{ $t('material.video') }}
  48. </div>
  49. </div>
  50. <!-- 素材列表 -->
  51. <div class="material-list">
  52. <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
  53. <van-list
  54. v-model:loading="loading"
  55. :finished="finished"
  56. :finished-text="materialList.length ? '' : $t('common.noData')"
  57. @load="loadMore"
  58. >
  59. <!-- 图片素材 -->
  60. <div class="image-grid" v-if="(type === 'image' || type === '') && imageList.length">
  61. <div
  62. class="image-item"
  63. v-for="item in imageList"
  64. :key="item.id"
  65. @click="previewImage(item)"
  66. >
  67. <img :src="getMediaUrl(item)" />
  68. <div class="image-actions">
  69. <van-icon name="eye-o" @click.stop="previewImage(item)" />
  70. <van-icon name="down" @click.stop="downloadImage(item)" />
  71. </div>
  72. </div>
  73. </div>
  74. <!-- 文本素材 -->
  75. <div class="text-list" v-if="(type === 'text' || type === '') && textList.length">
  76. <div
  77. class="text-item"
  78. v-for="item in textList"
  79. :key="item.id"
  80. >
  81. <div class="text-title">{{ getName(item) }}</div>
  82. <div class="text-content">{{ item.content }}</div>
  83. <div class="text-actions">
  84. <van-button size="small" @click="copyText(item.content || '')">
  85. {{ $t('common.copy') }}
  86. </van-button>
  87. </div>
  88. </div>
  89. </div>
  90. <!-- 视频素材 -->
  91. <div class="video-list" v-if="(type === 'video' || type === '') && videoList.length">
  92. <div
  93. class="video-item"
  94. v-for="item in videoList"
  95. :key="item.id"
  96. >
  97. <video :src="getMediaUrl(item)" controls></video>
  98. <div class="video-title">{{ getName(item) }}</div>
  99. </div>
  100. </div>
  101. </van-list>
  102. </van-pull-refresh>
  103. </div>
  104. </div>
  105. </template>
  106. <script setup lang="ts">
  107. import { ref, computed, watch, onMounted } from "vue";
  108. import { useRouter } from "vue-router";
  109. import { useI18n } from "vue-i18n";
  110. import { showToast, showImagePreview } from "vant";
  111. import { requestGetMaterialList, type MaterialInfo } from "@/api/material";
  112. const { t } = useI18n();
  113. const router = useRouter();
  114. const keyword = ref('');
  115. const type = ref<'' | 'image' | 'text' | 'video'>('');
  116. const materialList = ref<MaterialInfo[]>([]);
  117. const loading = ref(false);
  118. const finished = ref(false);
  119. const refreshing = ref(false);
  120. const page = ref(1);
  121. const pageSize = 20;
  122. // 获取素材的URL (图片/视频)
  123. const getMediaUrl = (item: MaterialInfo): string => {
  124. return item.url || '';
  125. };
  126. // 获取素材名称
  127. const getName = (item: MaterialInfo): string => {
  128. return item.name || '';
  129. };
  130. const imageList = computed(() =>
  131. materialList.value.filter(m => m.type === 'image')
  132. );
  133. const textList = computed(() =>
  134. materialList.value.filter(m => m.type === 'text')
  135. );
  136. const videoList = computed(() =>
  137. materialList.value.filter(m => m.type === 'video')
  138. );
  139. const getMaterials = async (isRefresh = false) => {
  140. if (isRefresh) {
  141. page.value = 1;
  142. finished.value = false;
  143. }
  144. try {
  145. const res = await requestGetMaterialList({
  146. current: page.value,
  147. size: pageSize
  148. });
  149. if (res.code === 200) {
  150. let list = res.data?.list || [];
  151. // 前端过滤类型
  152. if (type.value) {
  153. list = list.filter(m => m.type === type.value);
  154. }
  155. // 前端过滤关键词
  156. if (keyword.value) {
  157. const kw = keyword.value.toLowerCase();
  158. list = list.filter(m =>
  159. m.name?.toLowerCase().includes(kw) ||
  160. m.content?.toLowerCase().includes(kw)
  161. );
  162. }
  163. if (isRefresh) {
  164. materialList.value = list;
  165. } else {
  166. materialList.value = [...materialList.value, ...list];
  167. }
  168. // 判断是否还有更多数据
  169. const total = res.data?.paging?.total || 0;
  170. if (materialList.value.length >= total || list.length < pageSize) {
  171. finished.value = true;
  172. }
  173. } else {
  174. finished.value = true;
  175. }
  176. } catch (e) {
  177. console.error('获取素材列表失败', e);
  178. finished.value = true;
  179. }
  180. loading.value = false;
  181. refreshing.value = false;
  182. };
  183. const onSearch = () => {
  184. getMaterials(true);
  185. };
  186. const onRefresh = () => {
  187. getMaterials(true);
  188. };
  189. const loadMore = () => {
  190. page.value++;
  191. getMaterials();
  192. };
  193. const previewImage = (item: MaterialInfo) => {
  194. const url = getMediaUrl(item);
  195. if (url) {
  196. showImagePreview({
  197. images: [url],
  198. startPosition: 0
  199. });
  200. }
  201. };
  202. const downloadImage = (item: MaterialInfo) => {
  203. const url = getMediaUrl(item);
  204. if (url) {
  205. const link = document.createElement('a');
  206. link.href = url;
  207. link.download = item.name || `material_${item.id}.jpg`;
  208. link.target = '_blank';
  209. link.click();
  210. showToast(t('material.download') + ' started');
  211. }
  212. };
  213. const copyText = async (content: string) => {
  214. try {
  215. await navigator.clipboard.writeText(content);
  216. showToast(t('common.copySuccess'));
  217. } catch (e) {
  218. const textarea = document.createElement('textarea');
  219. textarea.value = content;
  220. document.body.appendChild(textarea);
  221. textarea.select();
  222. document.execCommand('copy');
  223. document.body.removeChild(textarea);
  224. showToast(t('common.copySuccess'));
  225. }
  226. };
  227. const goBack = () => {
  228. router.back();
  229. };
  230. watch(type, () => {
  231. getMaterials(true);
  232. });
  233. onMounted(() => {
  234. getMaterials(true);
  235. });
  236. </script>
  237. <style lang="scss" scoped>
  238. .material-page {
  239. min-height: 100vh;
  240. background: #121212;
  241. padding-bottom: 80px;
  242. }
  243. .header {
  244. display: flex;
  245. align-items: center;
  246. justify-content: space-between;
  247. padding: 16px;
  248. color: #fff;
  249. font-size: 16px;
  250. font-weight: 600;
  251. .van-icon {
  252. font-size: 20px;
  253. }
  254. }
  255. .search-bar {
  256. padding: 0 16px;
  257. :deep(.van-search) {
  258. padding: 0;
  259. .van-search__content {
  260. background: rgba(255, 255, 255, 0.08);
  261. }
  262. .van-field__control {
  263. color: #fff;
  264. }
  265. .van-icon {
  266. color: rgba(255, 255, 255, 0.5);
  267. }
  268. }
  269. }
  270. .type-tabs {
  271. display: flex;
  272. gap: 8px;
  273. padding: 16px;
  274. .tab-item {
  275. flex: 1;
  276. text-align: center;
  277. padding: 8px 0;
  278. background: rgba(255, 255, 255, 0.05);
  279. border-radius: 8px;
  280. font-size: 12px;
  281. color: rgba(255, 255, 255, 0.6);
  282. &.active {
  283. background: linear-gradient(135deg, #ffc300 0%, #ff9500 100%);
  284. color: #000;
  285. font-weight: 600;
  286. }
  287. }
  288. }
  289. .material-list {
  290. padding: 0 16px;
  291. }
  292. .image-grid {
  293. display: grid;
  294. grid-template-columns: repeat(2, 1fr);
  295. gap: 12px;
  296. margin-bottom: 16px;
  297. .image-item {
  298. position: relative;
  299. border-radius: 12px;
  300. overflow: hidden;
  301. background: rgba(255, 255, 255, 0.05);
  302. img {
  303. width: 100%;
  304. height: 120px;
  305. object-fit: cover;
  306. }
  307. .image-actions {
  308. position: absolute;
  309. bottom: 0;
  310. left: 0;
  311. right: 0;
  312. display: flex;
  313. justify-content: center;
  314. gap: 24px;
  315. padding: 8px;
  316. background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  317. .van-icon {
  318. font-size: 20px;
  319. color: #fff;
  320. }
  321. }
  322. }
  323. }
  324. .text-list {
  325. .text-item {
  326. padding: 16px;
  327. background: rgba(255, 255, 255, 0.05);
  328. border-radius: 12px;
  329. margin-bottom: 12px;
  330. .text-title {
  331. font-size: 14px;
  332. font-weight: 600;
  333. color: #fff;
  334. margin-bottom: 8px;
  335. }
  336. .text-content {
  337. font-size: 13px;
  338. color: rgba(255, 255, 255, 0.7);
  339. line-height: 1.5;
  340. margin-bottom: 12px;
  341. }
  342. .text-actions {
  343. text-align: right;
  344. .van-button {
  345. background: linear-gradient(135deg, #ffc300 0%, #ff9500 100%);
  346. border: none;
  347. color: #000;
  348. font-weight: 600;
  349. }
  350. }
  351. }
  352. }
  353. .video-list {
  354. .video-item {
  355. margin-bottom: 16px;
  356. video {
  357. width: 100%;
  358. border-radius: 12px;
  359. }
  360. .video-title {
  361. font-size: 13px;
  362. color: rgba(255, 255, 255, 0.7);
  363. margin-top: 8px;
  364. }
  365. }
  366. }
  367. </style>