news-tab.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <index-title-wrap title="通关指南">
  3. <view class="bg-white mx-card">
  4. <uv-tabs :current="current" :list="tabs" :scrollable="false" @change="current=$event.index"/>
  5. <view class="p-20 grid grid-cols-2 gap-20">
  6. <view v-for="n in typedNews" class="news-tab-item flex items-center rounded-lg"
  7. :style="{'--bg': n.bg}" @click="handleClick(n)">
  8. <view class="pl-15">
  9. <uv-text size="14" type="main" bold :text="n.subType"/>
  10. <uv-text type="tips" size="12" :text="n.description" margin="3px 0 0 0"/>
  11. </view>
  12. </view>
  13. </view>
  14. </view>
  15. </index-title-wrap>
  16. </template>
  17. <script>
  18. export default {
  19. name: "news-tab"
  20. }
  21. </script>
  22. <script setup>
  23. import {ref, computed, onMounted} from 'vue'
  24. import _ from 'lodash'
  25. import mxConst from "@/common/mxConst";
  26. import mxConfig from "@/common/mxConfig";
  27. import IndexTitleWrap from "@/pages/index/components/index-title-wrap.vue";
  28. import {getMainList} from '@/api/webApi/career-news'
  29. import {useTransfer} from "@/hooks/useTransfer";
  30. import {combineOssFile} from "@/utils";
  31. const {transferTo} = useTransfer()
  32. const current = ref(0)
  33. const news = ref([])
  34. const bgPool = [
  35. ['ie-guide-1.png', 'ie-guide-2.png', 'ie-guide-3.png', 'ie-guide-4.png'],
  36. ['ie-guide-5.png', 'ie-guide-6.png', 'ie-guide-7.png'],
  37. ['ie-guide-8.png']
  38. ]
  39. const groupedNews = computed(() => _.groupBy(news.value, n => n.type))
  40. const tabs = computed(() => _.keys(groupedNews.value).map(name => ({name})))
  41. const currentType = computed(() => _.get(tabs.value, `[${current.value}].name`))
  42. const typedNews = computed(() => {
  43. if (!currentType.value) return []
  44. const currentNews = groupedNews.value[currentType.value]
  45. return currentNews.map((n, i) => {
  46. const bgConfigs = bgPool[current.value % bgPool.length]
  47. const bg = bgConfigs[i % bgConfigs.length]
  48. return {...n, bg: `url(${getAssetsImage(bg)})`}
  49. })
  50. })
  51. onMounted(() => getList())
  52. function handleClick(item) {
  53. const ids = item.refIds?.split(',')
  54. if (ids.length === 1) {
  55. const next = {id: item.refIds}
  56. transferTo(mxConst.routes.newsDetail, next)
  57. } else if (ids.length > 1) {
  58. const next = {title: item.subType, ids: item.refIds}
  59. transferTo(mxConst.routes.newsGroup, next)
  60. } else {
  61. throw new Error('Please check getMainList.rows.refIds')
  62. }
  63. }
  64. async function getList() {
  65. const {rows} = await getMainList()
  66. news.value = rows
  67. }
  68. const getAssetsImage = (name) => {
  69. // 因为动态的css变量不会自动适配router.base, 打开后面的注释可达成。
  70. const routerBase = mxConfig.ossFileBase || __uniConfig.router.base || '/'
  71. return combineOssFile(`${routerBase}static/home/${name}`)
  72. }
  73. </script>
  74. <style scoped lang="scss">
  75. .news-tab-item {
  76. height: 75px;
  77. background-repeat: repeat;
  78. background-size: cover;
  79. background-image: var(--bg);
  80. background-position: 8px -12px;
  81. }
  82. </style>