brandStore.js 29.1 KB
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061


import api from '../../common/api.js';
import { getShopID } from '../../utils/util';
import router from '../../router/index.js'
import { parseBrandListData, cancelFilterSelectState } from '../../utils/productListUtil';
import { formatTime, shouldDiscardTap, getShopIdFromUrl } from '../../utils/util';

// import {
//   logEvent,
//   YB_PAGE_OPEN_L,
//   YB_SHARE_RESULT_L,
// } from '../../libs/analytics.js'
import { getChannelCode, getGenderCode } from '../../utils/home';

const { Actionsheet, extend } = require('../../vendors/zanui/index');
// import { postFormId } from '../../libs/formIdCollectRequest.js'
import { getQRCodeSource } from '../../utils/miniQRCodeRoute.js'
import config from '../../config.js';

// import { getUnionID } from '../../utils/login'

//获取应用实例
let app = getApp();
const screenHeight = app.globalData.systemInfo.screenHeight;
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;

const BANNER_RATIO = 234 / 750;
let bannerWidth = windowWidth;
let bannerHeight = bannerWidth * BANNER_RATIO;

const DEVICE_WIDTH_RATIO = windowWidth / 320;
const scale = windowWidth / 375;

let listWidth = Math.ceil(137.5 * DEVICE_WIDTH_RATIO);
let listHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);

const IMAGE_WIDTH = 145;
const IMAGE_HEIGHT = 193;
const IMAGE_RATIO = IMAGE_HEIGHT / IMAGE_WIDTH;
let listImageTop = 31;
let listImageWidth = listWidth;
let listImageHeight = Math.ceil(listWidth * IMAGE_RATIO);
let listMarginHorizontal = (windowWidth - listWidth * 2) / 3;

let PV_ID = new Date().getTime() + '';

Page(extend({}, Actionsheet, {

  /**
   * 页面的初始数据
   */
  data: {
    lastTapTimeStamp: 0,
    screenHeight,
    windowHeight,
    scale,
    bannerWidth,
    bannerHeight,

    shop_id: '',
    shop_name: '',
    bannerUrl:'',
    page_param: {},

    from_page_name: '',
    from_page_param: '',
    current_page_name: 'brandStore',
    current_page_param: '',

    info: {
      isLoading: false,
      error: null,
      data: null,
    },

    floor: {
      isLoading: false,
      error: null,
      data: null
    },

    list: {
      isLoading: false,
      error: null,
      data: null,
      pageSize: 60,
      currentPage: 0,
      pageCount: 0,
      total: 0,
      endReached: false,
    },

    couponList: [],

    filter: {
      item1: {
        key: 'item1',
        name: '默认',
        asc: '',
        isRadio: true,
        selected: true,
      },
      item2: {
        key: 'item2',
        name: '新品',
        asc: 's_t_desc',
        isRadio: true,
        selected: false,
      },
      item3: {
        key: 'item3',
        name: '价格',
        defaults: 'asc',
        asc: 's_p_asc',
        desc: 's_p_desc',
        isRadio: false,
        isAsc: true,
        selected: false,
      },
      item4: {
        key: 'item4',
        name: '折扣',
        defaults: 'desc',
        asc: 'p_d_desc',
        desc: 'p_d_asc',
        isRadio: false,
        isAsc: false,
        selected: false,
      }
    },

    filterGenderItem: {
      key: 'filterGenderItem',
      name: '筛选',
      isRadio: true,
      selected: true,
      hiddenGenderSelectView: true,
    },

    genderFilter: {
      item1: {
        key: 'item1',
        name: '男生/BOYS',
        selected: true,
        value: '1,3',
        title: '男生',
      },
      item2: {
        key: 'item2',
        name: '女生/GIRLS',
        selected: false,
        value: '2,4',
        title: '女生',
      }
    },

    fixedFilter: false,
    isShowIndicator: false,
    scrollTop: 0,
    isShowSnapshoot: false,
    snapshootShareData: {},

    actionsheet: {
      componentId: 'shareActionSheet',
      show: false,
      closeOnClickOverlay: true,
      isNewShareStyle: true,
      cancelText: '取消',
      actions: [{
        name: '分享给好友',
        className: 'action-class',
        loading: false,
        openType: 'share',
        image_src: '../../images/share_wechat@2x.png'
      },
      {
        name: '生成海报分享',
        className: 'action-class',
        loading: false,
        image_src: '../../images/share_wxpeng@2x.png'
      }]
    },

    isGettingCoupon: false,
    isGoApp: false,
    appParameter: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options && options.scene && options.scene.length == 32) {
      // var scene = decodeURIComponent(options.scene)
      // if (scene.length > 0) shop_id = scene;
      let scene = options.scene
      getQRCodeSource(scene).then(data => {
        that.loadElement(data)
      }).catch(error => {
        that.loadElement(options)
      })
    } else {
      that.loadElement(options)
    }
    
  },
  loadElement:function(options){
    var app = getApp()
    // 生命周期函数--监听页面加载
    let shop_id = options.shopId ? options.shopId : 0;
    if (!shop_id) {
      shop_id = options.shop_id ? options.shop_id : 0;
    }
    let shop_name = options.shopName ? options.shopName : '';
    if (!shop_name) {
      shop_name = options.shop_name ? options.shop_name : '';
    }

    if (options.q && options.q.length > 0) {
      let urlstring = decodeURIComponent(options.q)
      let shopIdFromUrl = getShopIdFromUrl(urlstring);
      if (shopIdFromUrl) {
        shop_id = shopIdFromUrl;
      }
    }

    let unionType = options.union_type ? options.union_type : '';
    
    if (options && options.scene && options.scene.length > 0) {
      var scene = decodeURIComponent(options.scene)
      if (scene.length > 0) shop_id = scene;
    }

    //解析 渠道号
    if (unionType) {
      app.updateUnionType(unionType);
    }

    let gender = app.globalData.selectedChannel == 'boy' ? '1,3' : '2,3';
    let from_page_name = options.page_name ? options.page_name : '';
    let from_page_param = options.page_param ? options.page_param : '';
    let current_page_param = shop_id;
    let currentScene = app.globalData.currentScene;
    let isGoApp = currentScene == 1036 ? true : false;
    this.setData({ shop_id, shop_name, gender, from_page_name, from_page_param, current_page_param, isGoApp });
    // new app.WeToast();
    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route

    // let params = {
    //   PAGE_NAME: this.data.current_page_name,
    //   PAGE_PARAM: this.data.current_page_param,
    //   FROM_PAGE_NAME: this.data.from_page_name,
    //   FROM_PAGE_PARAM: this.data.from_page_param,
    //   PV_ID: PV_ID,
    //   PAGE_PATH: url,
    // };
    // logEvent(YB_PAGE_OPEN_L, params);

    this.fetchShopInfo();
    this.fetchShopFloor();
    this.fetchCouponList();
    this.fetchShopList();

    this.initActionSheet()
  },
  initActionSheet:function(){
    let actionsheet = this.data.actionsheet;
    if (app.globalData.user_union_type) {//user_union_type_imageUrl
      let actionsheetParam = [
        {
          name: '分享给朋友',
          className: 'action-class',
          loading: false,
          openType: 'share',
          image_src: '../../images/share_union_wechat@2x.png'
        },
        {
          // name: '生成卡片并分享',
          name: '生成海报分享',
          className: 'action-class',
          loading: false,
          image_src: '../../images/share_union_wxpeng@2x.png'
        }
      ];
      actionsheet.unionUserImageUrl = app.globalData.user_union_type_imageUrl
      actionsheet.actions = actionsheetParam;
    } else if (app.globalData.userInfo.uid) {
      //登录状态
      actionsheet.unionUserImageUrl = app.globalData.user_nounion_type_imageUrl
    }
    this.setData({
      actionsheet,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    if (this.data.shop_name !== null && this.data.shop_name !== undefined && this.data.shop_name !== '') {
      let shopName = this.data.shop_name
      tt.setNavigationBarTitle({
        title: shopName,
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    let shareInfo = app.globalData.shareInfo;
    // console.log(shareInfo)
    if (app.globalData.user_union_type){      
      let default_image = that.data.snapshootShareData.shop_logo ? that.data.snapshootShareData.shop_logo:''

      //组装相关参数
      var shareData = {
        title: this.data.snapshootShareData.shop_name ? this.data.snapshootShareData.shop_name:shareInfo.title,
        content: '我在有货发现了一个不错的店铺,赶快来看看吧!',
        default_image,
        page_name: this.data.current_page_name,
        shop_id: this.data.shop_id,
        sharePath: '/pages/brandStore/brandStore?shopId=' + that.data.shop_id + '&union_type=' + app.globalData.user_union_type,//联盟分享页面 分享路径
      }

      let params = {
        PAGE_PATH: shareData.sharePath,
        SHARE_TYPE: 1,
      };
      // logEvent(YB_SHARE_PROMOTION_C, params);


      let param = {
        FROM: res.from,
        SHARE_RESUIL: 0,
        TITLE: '创建推广',
        DESC: shareData.content,
        PATH: shareData.sharePath,
        SHARE_TYPE: 1,
      }

      return {
        title: shareData.title, // 分享标题
        imageUrl: shareData.default_image,//分享图片
        desc: shareData.content, // 分享描述
        path: shareData.sharePath, // 分享路径
        success: function (res) {
          // param.SHARE_RESUIL = 1
          // logEvent(YB_SHARE_RESULT_L, param);
        },
        fail: function (res) {
          // param.SHARE_RESUIL = 2
          // logEvent(YB_SHARE_RESULT_L, param);
        }
      }
    }else{
      let param = {
        FROM: res.from,
        SHARE_RESUIL: 0,
        TITLE: that.data.shop_name,
        DESC: '我在有货发现了一个不错的店铺,赶快来看看吧!',
        PATH: '/pages/brandStore/brandStore?shopId=' + that.data.shop_id,
      }

      // 用户点击右上角分享
      return {
        title: param.TITLE?param.TITLE:shareInfo.title?shareInfo.title:param.DESC, // 分享标题
        desc: param.DESC, // 分享描述
        path: param.PATH, // 分享路径
        success: function (res) {
          // param.SHARE_RESUIL = 1
          // logEvent(YB_SHARE_RESULT_L, param);
        },
        fail: function (res) {
          // param.SHARE_RESUIL = 2
          // logEvent(YB_SHARE_RESULT_L, param);
        }
      }
    }
  },


  //上报formid
  formSubmit: function (e) {
    // // console.log('####formID:', e.detail.formId)
    // let formId = e.detail.formId;
    // postFormId(formId,"6")
  },

  //fetch data
  //店铺信息
  fetchShopInfo: function () {
    let currentChannel = app.globalData.selectedChannel
    let yh_channel = getChannelCode(currentChannel);

    let param = {
      method: 'app.shops.getIntro',
      shop_id: this.data.shop_id,
      yh_channel: yh_channel,
      fromePage: "iFP_RedPersonBrand"
    }
    let that = this;
    this.setData({
      info: {
        isLoading: true,
      }
    });
    api.get({data:param})
      .then(data => {
        if (!data || !data.code || data.code != 200) {
          that.setData({
            info: {
              isLoading: false,
              error: { code: data.code, message: data.message },
            }
          });
          return;
        }

        if (data.data.shop_name !== null && data.data.shop_name !== undefined && data.data.shop_name !== '') {
          let shopName = data.data.shop_name
          tt.setNavigationBarTitle({
            title: shopName,
          });
        }

        let shopInfo = data.data;
        let logoUrl = shopInfo.shop_logo.replace(/{width}/g, 200).replace(/{height}/g, 200).replace('{mode}', 2);
        shopInfo.shop_logo = logoUrl;
        var shareData = this.data.snapshootShareData;
        shareData.shop_logo = logoUrl;
        shareData.shop_intro_simple = shopInfo.shop_intro_simple;
        shareData.shop_id = shopInfo.shops_id;
        shareData.shop_name = shopInfo.shop_name;

        // var shop_qrCode = ''
        // if (app.globalData.user_union_type) {
        //   let page_param = {
        //     union_type: app.globalData.user_union_type,
        //     shopId: shopInfo.shops_id,
        //   }
        //   shop_qrCode = config.domains.api + '/wechat/miniapp/img-check.jpg?param=' + encodeURIComponent(JSON.stringify(page_param)) + '&miniQrType=10';
        // } else {
        //   // shop_qrCode = API_HOST + '/wechat/miniapp/img-check.jpg?param=' + shopInfo.shops_id;
        //   shop_qrCode = config.domains.api + '/wechat/miniapp/img-check.jpg?miniQrType=3&param=' + shopInfo.shops_id;
        // }
        // shareData.shop_qrCode = shop_qrCode
        // shareData.page_name = this.data.current_page_name;

        let appParameter = `http://m.yohobuy.com?openby:yohobuy={"action":"go.shop","params":{"shop_id":"${shopInfo.shops_id}","shop_template_type":"${shopInfo.shop_template_type}","shop_name":"${shopInfo.shop_name}","is_red_shop":"${shopInfo.is_red_shop}"}}`;

        that.setData({
          info: {
            isLoading: false,
            data: shopInfo,
          },
          snapshootShareData: shareData,
          appParameter,
        });
      })
      .catch(error => {
        that.setData({
          info: {
            isLoading: false,
            error,
          }
        });
      });
  },

  fetchShopFloor: function() {
    if (this.data.floor.isLoading) {
      return;
    }

    let params = {
      method: 'app.popular.shopsdecorator',
      shop_id: this.data.shop_id,
      fromePage: "iFP_RedPersonBrand"
    }
    let floor = Object.assign(this.data.floor, { isLoading: true});
    this.setData({ floor });

    let that = this;
    api.get({data:params})
      .then(json => {
        if (!json || !json.code || json.code != 200) {
          let floor = Object.assign(this.data.floor, { isLoading: false, error: { code: json.code, message: json.message } });
          that.setData({ floor });
          return;
        }

        let floor = json.data.modules;
        if(floor && floor.length > 0){
            let shopBanner = floor[0];
            if (shopBanner && shopBanner.module_type === "ShopBanner"){
              let module_data = JSON.parse(shopBanner.module_data);
              let data = module_data.data;
              if(data && data.length > 0) {
                  let resource = data[0];
                  var shareData = this.data.snapshootShareData;
                  shareData.bannerUrl = resource.pic;
                  that.setData({
                    bannerUrl: resource.pic,
                    snapshootShareData: shareData
                  })
              }
            }
        }

        that.setData({
          floor: {
            isLoading: false,
            data: json.data
          },
        })
      })
      .catch(error =>{
        that.setData({
          floor: {
            isLoading: false,
            error,
          }
        });
      });
  },

  fetchCouponList: function() {
    let params = {
      method: 'shop.coupons.list',
      shop_id: this.data.shop_id
    }
    let that = this;
    api.get({data:params})
    .then(json =>{
      if (!json || !json.code || json.code != 200) {
        return;
      }
      var couponList = json.data;
      if(couponList) {
        couponList.map((item, index)=> {
          item.money = parseInt(item.money);
          if (item.status == 1) {
            item.statusStr = '立\n即\n领\n取';
          } else if (item.status == 3) {
            item.statusStr = '已\n领\n取';
          } else if (item.status == 2) {
            item.statusStr = '已\n抢\n光';
          } else {
            item.statusStr = '';
          }
        })
        that.setData({
          couponList
        });
      }
    })
    .catch(error =>{
    })
  },

  // 列表信息
  fetchShopList: function() {
    if (this.data.list.isLoading || this.data.list.endReached) {
      return;
    }

    let page = this.data.list.currentPage + 1;
    let currentChannel = app.globalData.selectedChannel
    let yh_channel = getChannelCode(currentChannel);

    let params = {
      method: 'app.search.shop.productList',
      shop_id: this.data.shop_id,
      order: this.data.order,
      page,
      limit: this.data.list.pageSize,
      yh_channel,
      status: 1,
      fromPage: "iFP_RedPersonBrand"
    }

    let param = Object.assign(params, this.data.page_param);
    let list = Object.assign(this.data.list, { isLoading: true });
    this.setData({ list });
    let that = this;
    api.get({data:param})
      .then(json => {
        if (!json || !json.code || json.code != 200) {
          let list = Object.assign(this.data.list, { isLoading: false, error: { code: json.code, message: json.message } });
          that.setData({ list });
          return;
        }

        json = json.data;

        let currentPage = json && json.page ? json.page : 1;
        let pageCount = json && json.page_total ? json.page_total : 0;
        let total = json && json.total ? json.total : 0;
        let endReached = currentPage == pageCount;

        let data = json.product_list;
        data = parseBrandListData(data);

        if (currentPage > 1) {
          let oldList = this.data.list.data;
          data = [...oldList, ...data];
        }

        let newList = {
          isLoading: false,
          data,
          currentPage,
          pageCount,
          total,
          endReached,
        };
        let list = Object.assign(this.data.list, newList);
        that.setData({ list });
      })
      .catch(error => {
        let list = Object.assign(this.data.list, { isLoading: false, error, });
        that.setData({ list });
      });
  },
  // 上拉加载
  loadMore: function () {
    // 页面上拉触底事件的处理函数
    this.fetchShopList();
  },

  //点击筛选
  filterTapped: function (event) {
    let data = event.currentTarget.dataset;
    let key = data.key;

    let item = this.data.filter[key];
    // 单选
    if (item.isRadio) {
      // 已选中 结束
      if (item.selected) {
        return;
      }

      // 选中当前filter
      item.selected = true;

      // 遍历所有filter,设置非当前filter为不选中
      let newFilters = cancelFilterSelectState(this.data.filter, key);
      newFilters[key] = item;

      let newList = Object.assign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
      let page_param = this.data.page_param;
      page_param.order = item.asc;
      this.setData({ filter: newFilters, page_param, list: newList });
      this.fetchShopList();
    } else { // 多选
      // 已选中
      if (item.selected) {

        // 修改当前filter排序
        item.isAsc = !item.isAsc;
        let page_param = this.data.page_param;
        page_param.order = item.isAsc ? item.asc : item.desc;
        let newFilters = this.data.filter;
        newFilters[key] = item;

        let newList = Object.assign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
        this.setData({ page_param, filter: newFilters, list: newList });

        this.fetchShopList();
        // 未选中
      } else {

        // 选中当前filter
        item.selected = true;
        // 读取当前filter默认排序
        let defaultKey = item.defaults;
        let page_param = this.data.page_param;
        page_param.order = item[defaultKey];
        item.isAsc = defaultKey == 'asc';

        // 遍历所有filter,设置非当前filter为不选中
        let newFilters = cancelFilterSelectState(this.data.filter, key);
        newFilters[key] = item;

        let newList = Object.assign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
        this.setData({ page_param, filter: newFilters, list: newList });

        this.fetchShopList();
      }
    }
  },

  hiddenGenderFilter: function (event) {
    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.hiddenGenderSelectView = true;
    this.setData({ filterGenderItem });
  },

  filterGenderItemTapped: function (event) {
    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.hiddenGenderSelectView = !filterGenderItem.hiddenGenderSelectView;
    this.setData({ filterGenderItem });
  },

  maskTouchStart: function (event) {
    this.hiddenGenderFilter();
  },
  genderFilterTapped: function (event) {
    let data = event.currentTarget.dataset;
    let key = data.key;
    let item = this.data.genderFilter[key];

    if (item.selected) {
      this.hiddenGenderFilter();
      return;
    }

    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.name = item.title;
    // 选中当前filter
    item.selected = true;
    // 遍历所有filter,设置非当前filter为不选中
    let newFilters = cancelFilterSelectState(this.data.genderFilter, key);
    newFilters[key] = item;

    let newList = Object.assign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
    let page_param = this.data.page_param;
    page_param.gender = item.value;
    this.setData({ filterGenderItem, genderFilter: newFilters, list: newList, page_param });
    this.hiddenGenderFilter();
    this.fetchShopList();

  },

  jumpToShopIntro: function (event) {

  },

  shareShop: function (event) {
    this.setData({
      'actionsheet.show': true
    })
  },

  scroll: function (e) {
    var { scrollTop } = e.detail;
    var isShow = scrollTop > windowHeight * 2 ? true : false;
    if (isShow != this.data.isShowIndicator) {
      this.setData({
        isShowIndicator: isShow
      });
    }

    let y = scrollTop;
    this.setData({
      fixedFilter: y >= bannerHeight
    });
  },

  backToTop: function () {
    this.setData({
      scrollTop: 0
    })
  },

  handleZanActionsheetCancel({ componentId }) {
    this.setData({
      'actionsheet.show': false
    })
  },
  handlerJumpToUnionUrl() {
    this.setData({
      'actionsheet.show': false
    })
    if (app.globalData.user_union_type) {
      jumpByUrl("https://activity.yoho.cn/feature/2213.html?title=说明详情&openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"title\":\"说明详情\",\"url\":\"https://activity.yoho.cn/feature/2213.html\"}}", this.data.current_page_name)
    } else if (app.globalData.userInfo.uid) {
      jumpByUrl("https://activity.yoho.cn/feature/2601.html?title=有货有赚&openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"title\":\"有货有赚\",\"url\":\"https://activity.yoho.cn/feature/2601.html\"}}", this.data.current_page_name)
    } else {
      //未登录 状态去登录
      this.loginAndRegisterTapped()
    }
  },
  // 当行动按钮中有一个被点击时触发
  // index 代表被点击按钮在传入参数 actions 中的位置
  handleZanActionsheetClick({ componentId, index }) {
    this.setData({
      'actionsheet.show': false
    })
    
    if (index === 1) {
      this.setData({
        isShowSnapshoot: true
      })
    }
  },

  hiddenSheet: function () {
    this.setData({
      isShowSnapshoot: false
    })
  },

  getShopCoupon: function (e) {
    if (!app.isLogin()) {
      tt.showToast({
        title: '请先完成登录/注册,再查看!',
        icon: 'none',
        duration: 1500,
        success (res) {
          tt.navigateTo({
            url: '../bindPhoneNumber/bindPhoneNumber',
          })
        }
      });
      return;
    }

    var item = e.currentTarget.dataset.item;
    var couponId = item.coupon_id;
    var cardId = item.cardId;
    var couponStatus = item.status;

    if(!couponId) return;

    if (couponStatus == 1) {
      let params = {
        method: 'app.promotion.getCoupon',
        couponId
      }
      var that = this;
      this.setData({
        isGettingCoupon: true
      })
      api.get({data:params})
      .then(json => {
          that.setData({
            isGettingCoupon: false
          })

          if (!json || !json.code) {
            return;
          }

          if (json.code != 200) {
            var message = json.message;
            tt.showToast({
              title: message,
              icon: 'none',
              duration: 1500
            });
            return;
          }
          that.fetchCouponList();
          var couponCode = json.data ? json.data : '';
          if (cardId && cardId.length > 0 && tt.addCard) {
            tt.showModal({
              title: '领取成功!',
              content: '是否将优惠券添加到微信卡包?',
              confirmColor: '#444444',
              confirmText: '去添加',
              cancelColor: '#b0b0b0',
              success: function (res) {
                if (res.confirm) {
                  that.fetchSignature(cardId, couponCode);
                } else if (res.cancel) {
                }
              }
            })
          } else {
            var message = json.message;
            tt.showToast({
              title: '领取成功',
              icon: 'none',
              duration: 1500
            });
          }
        })
        .catch(error => {
          that.setData({
            isGettingCoupon: false
          })
        })
    } else  {
      var message = couponStatus == 3 ? '已领取' : '已抢光';
      tt.showToast({
        title: message,
        icon: 'none',
        duration: 1500
      });
    }
  },

  fetchSignature: function(cardId, couponCode) {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    
    let param = {
      url: '/wechat/miniapp/cardSignature',
      timestamp,
      cardId
    }
    var that = this;
    api.get({data:param, url: '/wechat/miniapp/cardSignature'})
    .then(json =>{
      if(!json || !json.code || json.code!=200) {
        tt.showToast({
          title: "领取到微信卡包失败",
          icon: 'none',
          duration: 1500
        });
        return;
      }

      var signature = json.data.signature ? json.data.signature : '';
      var nonce_str = json.data.nonce_str ? json.data.nonce_str : '';

      if(signature.length > 0) {
        var cardExt = {
          timestamp,
          nonce_str,
          signature,
        }
        var cardInfo = {
          cardId: cardId,
          cardExt: JSON.stringify(cardExt)
        }

        tt.addCard({
          cardList: [cardInfo],
          success: function(res) {
            tt.showToast({
              title: "已领取并已成功添加到卡包",
              icon: 'none',
              duration: 1500
            });
            var code = (res.cardList && res.cardList.length > 0) ? res.cardList[0].code : '';
            that.addWechatCoupon(cardId, code, couponCode);
          },
          fail: function(err) {
            tt.showToast({
              title: "已领取",
              icon: 'none',
              duration: 1500
            });
          }
        })
      }
    })
    .catch(error =>{
    })
  },

  addWechatCoupon: function(cardId, cardCode, couponCode) {
    var unionId = app.globalData.WXUnion_ID;
    let params = {
      method: 'app.promotion.addWechatCoupon',
      couponCode,
      cardId,
      cardCode,
      unionId
    }
    // console.log(params);
    api.get({data:params})
    .then(json =>{
    })
    .catch(error =>{
    });
  },

  //跳转到登录页面
  loginAndRegisterTapped: function (needBind) {
    let that = this;
    let app = getApp()
    if (tt.getSetting) {
      tt.getSetting({
        success: (res) => {
          if (res && res.authSetting && res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === false) {
            tt.showModal({
              title: '',
              content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
              confirmText: "去开启",
              confirmColor: "#000000",
              success: function (res) {
                if (res.confirm) {
                  tt.openSetting({
                    success: (res) => {
                      if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === true) {
                        // getUnionID(app.getWechatThirdSession(), function (response) {
                        // })
                      }
                    }
                  })
                } else {
                  tt.navigateTo({
                    url: '../bindPhoneNumber/bindPhoneNumber',
                  })
                }
              }
            })
          } else {
            tt.navigateTo({
              url: '../bindPhoneNumber/bindPhoneNumber',
            })
          }
        }
      })
    } else {
      tt.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }
  },

}))