search.vue 17.3 KB
Newer Older
1 2
<template>
    <div class="product-list">
3
        <searchtop :keyword="keyword" @onSearch="onSearch"  />
4
        <div class="filterbar">
5 6 7 8 9
            <ul :class="filterSort?'show':''">
                <li :class="filterIndex===0?'selected':''" v-on:click="onFilterBar(0)"><span>{{filterIndex==11?'价格最低':(filterIndex==12?'价格最高':'综合')}}</span><van-icon name="arrow" class="down" /></li>
                <li :class="filterIndex===1?'selected':''" v-on:click="onFilterBar(1)"><span>销量</span></li>
                <li :class="filterIndex===2?'selected':''" v-on:click="onFilterBar(2)"><span>上新</span></li>
                <li :class="filterIndex===3?'selected':''" v-on:click="onFilterBar(3)"><span>筛选</span></li>
10
            </ul>
11
            <div :class="'item_options '+(filterSort?'show':'')">
12
                <ul>
13 14 15
                    <li :class="filterIndex==10?'selected':''" v-on:click="onFilterBar(10)">综合</li>
                    <li :class="filterIndex==11?'selected':''"  v-on:click="onFilterBar(11)">价格降序</li>
                    <li :class="filterIndex==12?'selected':''" v-on:click="onFilterBar(12)">价格最高</li>
16 17
                </ul>
            </div>
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
            <div :class="'item_options '+(filterShow?'show':'')">
                <ul>
                    <li v-for="category in categories"  :class="category.id === categoryId ?'selected':''" v-on:click="onCategoryClick(category.id)">
                        {{ category.name }}
                    </li>
                </ul>
            </div>

<!--            <van-popup v-model="filterShow" position="right" class="filterlayer" >-->
<!--                <div class="filterInner" style="overflow-y: scroll;max-height: 100%;">-->
<!--                    <ul>-->
<!--                        <li>-->
<!--                            <van-cell title="清洁类型" is-link arrow-direction="down" />-->
<!--                        </li>-->
<!--                        <div style="clear: both;"></div>-->
<!--                        <div class="tags_selection">-->
<!--                            <div class="option">-->
<!--                                <a href="javascript:void 0;">牙龈护理111</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">抛光</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">清洁</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">正畸专用</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">敏感</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">亮白</a>-->
<!--                            </div>-->
<!--                            <div style="clear: both;"></div>-->
<!--                        </div>-->
<!--                    </ul>-->
<!--                    <ul>-->
<!--                        <li>-->
<!--                            <van-cell title="清洁类型" is-link arrow-direction="down" />-->
<!--                        </li>-->
<!--                        <div style="clear: both;"></div>-->
<!--                        <div class="tags_selection">-->
<!--                            <div class="option">-->
<!--                                <a href="javascript:void 0;">牙龈护理111</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">抛光</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">清洁</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">正畸专用</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">敏感</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">亮白</a>-->
<!--                            </div>-->
<!--                            <div style="clear: both;"></div>-->
<!--                        </div>-->
<!--                    </ul>-->
<!--                    <ul>-->
<!--                        <li>-->
<!--                            <van-cell title="清洁类型" is-link arrow-direction="down" />-->
<!--                        </li>-->
<!--                        <div style="clear: both;"></div>-->
<!--                        <div class="tags_selection">-->
<!--                            <div class="option">-->
<!--                                <a href="javascript:void 0;">牙龈护理111</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">抛光</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">清洁</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">正畸专用</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">敏感</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">亮白</a>-->
<!--                            </div>-->
<!--                            <div style="clear: both;"></div>-->
<!--                        </div>-->
<!--                    </ul>-->
<!--                    <ul>-->
<!--                        <li>-->
<!--                            <van-cell title="清洁类型" is-link arrow-direction="down" />-->
<!--                        </li>-->
<!--                        <div style="clear: both;"></div>-->
<!--                        <div class="tags_selection">-->
<!--                            <div class="option">-->
<!--                                <a href="javascript:void 0;">牙龈护理111</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">抛光</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">清洁</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">正畸专用</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">敏感</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">亮白</a>-->
<!--                            </div>-->
<!--                            <div style="clear: both;"></div>-->
<!--                        </div>-->
<!--                    </ul>-->
<!--                    <ul>-->
<!--                        <li>-->
<!--                            <van-cell title="清洁类型" is-link arrow-direction="down" />-->
<!--                        </li>-->
<!--                        <div style="clear: both;"></div>-->
<!--                        <div class="tags_selection">-->
<!--                            <div class="option">-->
<!--                                <a href="javascript:void 0;">牙龈护理111</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">抛光</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">清洁</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">正畸专用</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">敏感</a>-->
<!--                            </div>-->
<!--                            <div class="option ">-->
<!--                                <a href="javascript:void 0;">亮白</a>-->
<!--                            </div>-->
<!--                            <div style="clear: both;"></div>-->
<!--                        </div>-->
<!--                    </ul>-->
<!--                    <div style="clear: both;"></div>-->
<!--                    <van-button size="large"  style="height: 40px;margin-bottom: 15px;line-height: 40px;">清楚选项</van-button>-->
<!--                    <div style="height:50px;"></div>-->
<!--                </div>-->
<!--                <div class="filterlayer_bottom_buttons">-->
<!--                    <span class="filterlayer_bottom_button cancel">取消</span>-->
<!--                    <span class="filterlayer_bottom_button confirm">确认</span>-->
<!--                </div>-->
<!--            </van-popup>-->
172 173
        </div>

174 175 176 177 178 179 180 181 182 183 184
        <van-list
                v-model="loading"
                :finished="finished"
                finished-text="没有更多了"
                @load="onLoad"
        >
            <div v-for="(product,i) in products" :key="i">
                <product-card :product='product' @click="showProduct(product)" />
            </div>
        </van-list>

185 186 187 188 189
    </div>
</template>

<script>
import searchtop from "../../components/search/searchtop";
190
import {getProductCondition, getProductPage} from "../../api/search";
191 192 193 194 195 196 197

export default {
  components: {
    searchtop
  },
  data() {
    return {
198 199 200 201 202
      page: 0,
      pageSize: 10,
      loading: false,
      finished: false,

203
      keyword: this.$route.params.keyword,
204 205 206 207 208 209 210 211

      filterIndex: 0,
      filterSort: false, // 是否展示几个【排序】
      filterShow: false, // 是否展示【筛选】

      sortField: undefined,
      sortOrder: undefined,

212 213 214
      products:[], // 搜索出的商品
      categories: [], // 筛选的分类
      categoryId: undefined, // 选中的分类编号
215 216 217 218
    };
  },
  methods: {
    onFilterBar(value) {
219 220
      if (value === 0) {
        this.filterSort = !this.filterSort;
221
        this.filterShow = false;
222 223
      } else if (value === 3) {
        this.filterShow = !this.filterShow;
224
      } else {
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 256 257 258 259 260 261 262 263
        // 如果非合法 10、11、12
        if (value !== 10
            && value !== 11
            && value !== 12) {
          alert('暂不支持');
          return;
        }
        // 设置 filterSort 和 filterIndex 属性
        this.filterSort = false;
        this.filterIndex = value;
        // 标记加载中
        this.loading = true;
        // 根据 value 的值,设置 sortField、sortOrder
        switch (value) {
          case 10:
            this.sortField = undefined;
            this.sortOrder = undefined;
            break;
          case 11:
            this.sortField = 'buyPrice';
            this.sortOrder = 'desc';
            break;
          case 12:
            this.sortField = 'buyPrice';
            this.sortOrder = 'asc';
            break;
        }
        // 根据排序,重新搜索
        let page = 1;
        getProductPage({
          pageNo: page,
          pageSize: this.pageSize,
          keyword: this.keyword,
          sortField: this.sortField,
          sortOrder: this.sortOrder,
        }).then(data => {
          this.products = [];
          this.handleData(page, data);
        });
264 265
      }
    },
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    onCategoryClick(value) {
      // 设置分类编号
      this.categoryId = value;
      // 隐藏弹出
      this.filterShow = false;
      // 根据分类,重新搜索
      let page = 1;
      getProductPage({
        pageNo: page,
        pageSize: this.pageSize,
        keyword: this.keyword,
        sortField: this.sortField,
        sortOrder: this.sortOrder,
        cid: this.categoryId,
      }).then(data => {
        this.products = [];
        this.handleData(page, data);
      });
    },
285 286
    showProduct(product){
        this.$router.push('/product/'+product.id);
287 288 289 290 291 292 293 294 295 296 297
    },
    onSearch(keyword) {
      this.loading = true;
      // 设置 keyword
      this.keyword = keyword;
      // 重置其它字段
      this.filterIndex = 0;
      this.filterSort = false;
      this.filterShow = false;
      this.sortField = undefined;
      this.sortOrder = undefined;
298
      this.categoryId = undefined;
299 300 301 302 303 304 305 306 307
      // 查询
      let page = 1;
      getProductPage({
        pageNo: page,
        pageSize: this.pageSize,
        keyword: keyword,
      }).then(data => {
        this.products = [];
        this.handleData(page, data);
308
        this.loadSearchCondition();
309 310 311
      });
    },
    onLoad() {
312
      // debugger;
313 314 315 316 317
      // 进入下一页
      let page = this.page + 1;
      getProductPage({
        pageNo: page,
        pageSize: this.pageSize,
318
        keyword: this.keyword,
319 320
      }).then(data => {
        this.handleData(page, data);
321
        this.loadSearchCondition();
322 323 324 325 326 327 328 329 330 331 332 333 334 335
      });
    },
    handleData(page, data) {
      this.loading = true;
      // 设置下页面
      this.page = page;
      // 数据保存到 list 中
      this.products.push(...data.list);
      // 判断页数
      if (this.products.length >= data.total) {
        this.finished = true;
      }
      // 标记不在加载中
      this.loading = false;
336 337 338 339 340 341 342
    },
    loadSearchCondition() {
      getProductCondition({
        keyword: this.keyword,
      }).then(data => {
        this.categories = data.categories;
      });
343
    }
344 345
  },
  mounted() {
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  }
};
</script>

<style lang="less">
.product-list{
    .additional .price{
        position: absolute;
        bottom: 6px;
        height: 34px;
    }
}
.filterbar {
  font-size: 14px;
  position: relative;
  ul {
    width: 100%;
    height: 40px;
  }
  li {
    color: #999;
    width: 25%;
    float: left;
    text-align: center;
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #eee;
    i {
      font-size: 12px;
      margin-left: 2px;
    }
    .down {
      -webkit-transform: rotate(90deg);
      transform: rotate(90deg);
    }
  }
  .selected {
    color: #e93b3d;
  }
  .show {
    .down {
      -webkit-transform: rotate(-90deg);
      transform: rotate(-90deg);
    }
  }
  .item_options {
    width: 100%;
    position: absolute;
    top: 41px;
    background: #fff;
    display: none;
    z-index: 1;
    li {
      width: 100%;
      text-align: left;
      padding-left: 10px;
    }
  }

  .item_options.show {
    display: block;
  }
  .filterlayer {
    width: 85%;
    height: 100%;
    background-color: #f7f7f7;
    &_bottom {
      &_buttons {
        display: -webkit-box;
        display: -webkit-flex;
        display: flex;
        z-index: 10;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;

        .cancel {
          color: #333;
          background-color: #fff;
          box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, 0.07);
        }
        .confirm {
          color: #fff;
          background-color: #e93b3d;
        }
      }
      &_button {
        -webkit-box-flex: 1;
        -webkit-flex: 1;
        flex: 1;
        font-size: 16px;
        height: 49px;
        line-height: 49px;
        text-align: center;
      }
    }
    ul {
      height: auto;
      background: #fff;
      margin-bottom: 15px;
    }
    li {
      width: 100%;
      text-align: left;
        height: auto;
        line-height: initial;
      .van-cell{
        font-size: 16px;
      }
      .big {
        height: 25px;
        max-width: 250px;
        overflow: hidden;
        font-size: 16px;
        color: #333;
        text-overflow: ellipsis;
        margin-left: 10px;
      }
    }
    .filterInner{
        overflow-y: scroll;
        max-height: 100%;
        .van-button{
            height: 40px;
            margin-bottom: 15px;
            line-height: 40px;
        }
    }
    .tags_selection {
      font-size: 14px;
      text-align: center;
      background-color: #fff;
      position: relative;
      margin: 0;
      padding: 10px 0 0 10px;
      .option {
        box-sizing: border-box;
        float: left;
        width: 33.33%;
        padding-right: 10px;
        height: 30px;
        line-height: 30px;
        margin-bottom: 10px;
        a {
          position: relative;
          display: block;
          padding: 0 5px;
          color: #666;
          background-color: #f7f7f7;
          border-radius: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
            height: 30px;
        }
      }
    }
  }
}
</style>