xiangpei
昨天 d30ff90ce9ed4eb1d60384a1ee62ad3735635bd1
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<template>
  <div class="wrapper">
    <div class="wap-content">
      <div class="query-wrapper">
        <div class="query-item">
          <div>搜索范围</div>
          <Input placeholder="商品名称" @on-clear="goodsData=[]; goodsParams.goodsName=''; goodsParams.pageNumber = 1;  getQueryGoodsList()" @on-enter="()=>{goodsData=[]; goodsParams.pageNumber = 1; getQueryGoodsList();}" clearable style="width: 150px"
            v-model="goodsParams.goodsName" />
        </div>
        <div class="query-item">
          <Cascader v-model="category" placeholder="请选择商品分类" style="width: 150px" :data="cateList"></Cascader>
        </div>
        <div class="query-item">
          <Button type="primary" @click="goodsData=[]; goodsParams.pageNumber = 1; getQueryGoodsList();" icon="ios-search">搜索</Button>
        </div>
      </div>
      <div >
        <Scroll class="wap-content-list" :on-reach-bottom="handleReachBottom" :distance-to-edge="[3,3]">
 
          <div class="wap-content-item" :class="{ active: item.selected }" @click="checkedGoods(item, index)" v-for="(item, index) in goodsData" :key="index">
            <div>
              <img :src="item.thumbnail" alt="" />
            </div>
            <div class="wap-content-desc">
              <div class="wap-content-desc-title">{{ item.goodsName }}</div>
              <div class="wap-sku">{{ item.goodsUnit }}</div>
              <div class="wap-sku"><Tag :color="item.salesModel === 'RETAIL' ? 'default' : 'geekblue'">{{item.salesModel === "RETAIL" ? "零售型" : "批发型"}}</Tag></div>
              <div class="wap-content-desc-bottom">
                <div>¥{{ item.price | unitPrice }}</div>
              </div>
            </div>
          </div>
          <Spin size="large" fix v-if="loading"></Spin>
 
          <div v-if="empty" class="empty">暂无商品信息</div>
        </Scroll>
 
      </div>
    </div>
  </div>
</template>
<script>
import * as API_Goods from "@/api/goods";
export default {
  props: {
    selectedWay: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      type: "multiple", //单选或者多选 single  multiple
 
      cateList: [], // 商品分类列表
      total: "", // 商品总数
      goodsParams: {
        // 请求商品列表参数
        pageNumber: 1,
        pageSize: 18,
        order: "desc",
        goodsName: "",
        sn: "",
        categoryPath: "",
        marketEnable: "UPPER",
        authFlag: "PASS",
        sort:"createTime"
      },
      category: [], // 选中的商品分类
      goodsData: [], // 商品列表
      empty: false, // 是否空数据
      loading: false, // 商品加载loading
    };
  },
  watch: {
    category(val) {
      this.goodsParams.categoryPath = val[2];
    },
    selectedWay: {
      handler(val) {
        this.$emit("selected", this.selectedWay);
      },
      deep: true,
      immediate: true,
    },
    "goodsParams.categoryPath": {
      handler: function () {
        this.goodsData = [];
        (this.goodsParams.pageNumber = 0), this.getQueryGoodsList();
      },
      deep: true,
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    handleReachBottom() {
      // 页面触底触发加载
      setTimeout(() => {
        if (
          this.goodsParams.pageNumber * this.goodsParams.pageSize <=
          this.total
        ) {
          this.goodsParams.pageNumber++;
          this.getQueryGoodsList();
        }
      }, 1500);
    },
    getQueryGoodsList() {
      // 根据商品分类筛选商品
      API_Goods.getGoodsSkuData(this.goodsParams).then((res) => {
        this.initGoods(res);
      });
    },
 
    initGoods(res) {
      // 获取商品列表
      if (res.result.records.length != 0) {
        let data = res.result.records;
        data.forEach((item) => {
          item.selected = false;
          item.___type = "goods"; //设置为goods让pc wap知道标识
 
          this.selectedWay.forEach((e) => {
            if (e.id === item.id) {
              item.selected = true;
            }
          });
        });
        /**
         * 解决数据请求中,滚动栏会一直上下跳动
         */
        this.total = res.result.total;
        this.goodsData.push(...res.result.records);
      } else {
        this.empty = true;
      }
    },
 
    // 查询商品
    init() {
      Promise.all([
        API_Goods.getGoodsSkuData(this.goodsParams),
        API_Goods.getGoodsCategoryAll(0),
      ]).then((res) => {
        // 商品
        this.initGoods(res[0]);
 
        // 分类
        if (res[1].result) {
          this.deepGroup(res[1].result);
        }
      });
    },
 
    deepGroup(val) {
      val.forEach((item) => {
        let childWay = []; //第二级
        // 第二层
        if (item.children) {
          item.children.forEach((child) => {
            // // 第三层
            if (child.children) {
              child.children.forEach((grandson, index, arr) => {
                arr[index] = {
                  value: grandson.id,
                  label: grandson.name,
                  children: "",
                };
              });
            }
            let children = {
              value: child.id,
              label: child.name,
              children: child.children,
            };
            childWay.push(children);
          });
        }
 
        // 第一层
        let way = {
          value: item.id,
          label: item.name,
          children: childWay,
        };
 
        this.cateList.push(way);
      });
    },
 
    /**
     * 点击商品
     */
    checkedGoods(val, index) {
      // 如果单选的话
      if (this.type != "multiple") {
        this.goodsData.forEach((item) => {
          item.selected = false;
        });
        this.selectedWay = [];
        val.selected = true;
        this.selectedWay.push(val);
 
        return false;
      }
 
      if (val.selected == false) {
        val.selected = true;
        this.selectedWay.push(val);
      } else {
        val.selected = false;
        for (let i = 0; i < this.selectedWay.length; i++) {
          if (this.selectedWay[i].id === val.id) {
            this.selectedWay.splice(i, 1);
            break;
          }
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
@import "./style.scss";
.wap-content {
  width: 100%;
}
.empty {
  text-align: center;
  padding: 8px 0;
  width: 100%;
}
.wap-content {
  flex: 1;
  padding: 0;
}
.wap-content-list {
  position: relative;
}
.wap-content-item {
  width: 210px;
  margin: 10px 7px;
  padding: 6px 0;
}
 
.active {
  background: url("../../assets/selected.png") no-repeat;
  background-position: right;
  background-size: 10%;
}
</style>