zhengyiming
12 小时以前 97f29024ce18babeb4b635c5d73f907ac493976e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!--
 * @Author: 秦少卫
 * @Date: 2024-06-11 16:17:17
 * @LastEditors: 秦少卫
 * @LastEditTime: 2024-06-12 15:43:54
 * @Description: 列表组件
-->
 
<template>
  <div class="material-type">
    <div class="item" v-for="item in materialTypeList" :key="item.id">
      <div class="top">
        <h3>{{ item.name }}</h3>
        <Button type="text" size="small" @click="emit('selectType', item.id)">查看更多</Button>
      </div>
      <div class="img-box">
        <div class="img-item" v-for="info in item.list" :key="info.id">
          <Image
            lazy
            :src="info.src"
            @click="(e) => emit('click', { info, e })"
            @dragend="(e) => emit('dragend', { info, e })"
            fit="contain"
            width="100%"
            height="100%"
            :alt="info.name"
          />
        </div>
      </div>
    </div>
  </div>
</template>
 
<script name="ImportJson" setup>
const baseURL = import.meta.env.APP_APIHOST;
 
const emit = defineEmits(['click', 'dragend']);
 
const props = defineProps({
  typeApi: {
    type: Function,
  },
  typeListApi: {
    type: Function,
  },
  typeKey: {
    type: String,
  },
  formatData: {
    type: Function,
  },
});
 
// 素材分类
const materialTypeList = ref([]);
const getMaterialTypesHandler = async () => {
  const res = await props.typeApi();
  materialTypeList.value = res.data.data.map((item) => {
    return {
      name: item.attributes.name,
      id: item.id,
      list: [],
    };
  });
};
 
const getMaterialsByTypeHandler = async () => {
  materialTypeList;
  let i = 0;
  for (const item of materialTypeList.value) {
    const res = await props.typeListApi({
      populate: {
        img: '*',
      },
      filters: {
        [props.typeKey]: {
          $eq: item.id,
        },
      },
      pagination: {
        page: 1,
        pageSize: 8,
      },
    });
    materialTypeList.value[i].list = props.formatData(res.data.data);
 
    i++;
  }
};
 
onMounted(async () => {
  await getMaterialTypesHandler();
  await getMaterialsByTypeHandler();
});
</script>
<style scoped lang="less">
.item {
  .top {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
  }
}
 
.img-box {
  background: #f1f2f4;
  display: grid;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(4, 60px);
  grid-template-rows: repeat(2, 70px);
  grid-row-gap: 10px;
  justify-content: space-between;
  padding: 8px;
  background: #f1f2f4;
  border-radius: 10px;
  margin-bottom: 10px;
  .img-item {
    border-radius: 5px;
    padding: 5px;
    cursor: pointer;
    &:hover {
      background: #bababa;
    }
  }
}
</style>