index.js 4.05 KB
import wx from '../../utils/wx';
import Promise from '../../vendors/es6-promise';
import commonModel from '../../models/common';
import indexModel from '../../models/index/index';

// 获取应用实例
const app = getApp();
const router = global.router;

Page({
  data: {
    shopInfo: {},
    shopBanner: '',
    shopProductList: [],
    decorFloors: []
  },
  onLoad: function() {

  },
  onShow: function() {
    const shopId = app.getShopId();

    if (shopId) {
      this.getShopData(shopId);
      this.getShopProducts(shopId);
    } else {
      commonModel.getBindShop({app_id: app.getAppId()}).then(res => {
        if (res.code === 200 && res.data.shopId) {
          const id = res.data.shopId;

          app.globalData.unionShop = res.data;
          app._setSync('unionShop', res.data);

          this.getShopData(id);
          this.getShopProducts(id);
        } else {
          return Promise.reject();
        }
      }).catch(() => {});
    }
  },

  getShopData: function(shopId) {
    this._shopInfo(shopId);
    this._shopDecor(shopId);
    this._shopFavCount(shopId);
  },

  getShopProducts: function() {
    this._productList();
  },

  // 店铺信息
  _shopInfo: function(shopId) {
    const params = {
      shop_id: shopId
    };

    indexModel.shopInfo(params)
      .then(res => {
        if (res.data.shop_name) {
          let shopName = res.data.shop_name;

          wx.setNavigationBarTitle({
            title: shopName,
          });
        }

        let shopInfo = res.data;

        this.setData({
          shopInfo: shopInfo
        });
      });
  },

  // 店铺装修
  _shopDecor: function(shopId) {
    const params = {
      shop_id: shopId
    };

    indexModel.shopDecorator(params)
      .then(res => {

        if (res.code === 200) {
          let floors = [];
          let modules = res.data.modules;

          if (modules && modules.length > 0) {
            floors = modules.map(module => {
              module.data = JSON.parse(module.module_data).data;
              module.properties = JSON.parse(module.module_data).properties;
              delete module.module_data;

              return module;
            });

            const banner = floors[0];

            if (banner && banner.module_type === 'ShopBanner') {
              this.setData({
                shopBanner: banner.data[0] && banner.data[0].pic
              });
            }

            this.setData({
              decorFloors: floors
            });
          }
        }
      })
      .catch(() => {});
  },

  // 店铺收藏
  _shopFavCount: function(shopId) {
    indexModel.shopFavCount({favIds: shopId}).then(res => {
      const favCount = res.data[0].approximateCount || '2.1w';

      this.setData({
        favCount
      });
    });
  },

  // 筛选变更
  filterChange: function(e) {
    let params;
    let {curSort, gender} = e.detail;

    this.data.order = curSort;
    this.data.gender = gender;

    params = {
      gender,
      order: curSort,
      page: 1,
      limit: 20
    };
    this.data.productList = [];

    params.setScrollPos = true;
    this._productList(params);
  },

  // 商品列表
  _productList: function(params) {
    params = params || {page: 1, pageNo: 20};
    params.shop_id = app.getShopId();

    indexModel.shopProductList(params).then(res => {
      if (res.code === 200) {
        const keyAdapter = {
          skn: 'product_skn',
          salesPrice: 'sales_price',
          marketPrice: 'market_price',
          productName: 'product_name',
          defaultImages: 'default_images'
        };
        let list = [];

        (res.data.product_list || []).forEach(product => {
          let item = {};

          Object.keys(keyAdapter).forEach(key => {
            item[key] = product[keyAdapter[key]];
          });
          list.push(item);
        });

        this.setData({
          currentPage: params.page,
          productList: this.data.shopProductList.concat(list),
          totalPage: res.data && res.data.page_total || 0
        });
      }
    }).catch(() => {});
  },

  toSearch() {
    router.go('productSearch');
  }
});