giftPage.js 8.66 KB
// giftPage.js
import api from '../../common/api.js';
import { shouldDiscardTap, getImageUrlWithWH } from '../../utils/util';

//获取应用实例
let app = getApp();

const screenHeight = app.globalData.systemInfo.screenHeight;
const screenWidth = app.globalData.systemInfo.screenWidth;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    promotion_ids: '',
    is_gift: false,
    status: "0",
    screen_width: screenWidth,
    current_section_title: '.',
    promotion_data_ary: [],

    current_skn: '',
    selectedSKU: '',
    //picker 需要的数据
    pickerShow: false,
    pickerData: {
      view: {
        isShow: false,
        isSoldOutSoon: false, //是否显示即将售罄
        goodsList: [],
        sizeList: [],
        image: '',
        goodPrice: '',
        price: '',
        buyNumber: 1,
        minusButtonEnable: false,
        plusButtonEnable: false,
        buyButtonEnable: true,
      },
      sourceType: 'gift',
    },


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let promotion_ids = options.promotion_id;
    let isgift = options.is_gift;
    let is_gift = false;
    let status = options.status;
    if (isgift == 'true') {
      is_gift = true;
      console.log('进入领取赠品页面-promotion_ids = ' + promotion_ids);
    } else {
      console.log('进入换购页面-promotion_ids = ' + promotion_ids);
    }
    this.setData({ promotion_ids, is_gift, status });

    this.queryGiftPageData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let navTitle = '换购商品';
    if (this.data.is_gift) {
      navTitle = '领取赠品';
    }
    tt.setNavigationBarTitle({
      title: navTitle,
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  onPressProduct: function (event) {
    let data = event.currentTarget.dataset;
    let product_skn = data.productSkn;
    this.setData({ current_skn: product_skn });
    let param = {
      method: 'app.product.gift',
      uid: app.getUid(),
      product_skn,
      promotion_id: data.item.promotion_id,
    };
    var that = this

    api.get({data: param})
      .then(data => {
        // console.log(data);
        if (!data || data.code != 200)
          return;
        
        let colorSelected = false;
        let color_length = 0;
        if (data.data.goods_list) {
          color_length = data.data.goods_list.length;
        }
        let ImgUrls = [];
        data.data.goods_list.map((item, index) => {
          let images = getImageUrlWithWH(item.color_image, 75, 100);
          ImgUrls = [...ImgUrls, images];
          if (index == 0 && color_length == 1) {
            item.selected = true;
            colorSelected = true;
          }
        });

        let isSoldOutSoon = false;
        let tags = data.data.tags;
        if (tags) {
          tags.map((item) => {
            if (item == "is_soon_sold_out") {
              isSoldOutSoon = true;
            }
          });
        }

        //pickerData
        var temData = that.data.pickerData;
        temData.view.goodsList = data.data.goods_list;
        temData.view.isSoldOutSoon = isSoldOutSoon;

        //默认显示第一种颜色的尺码
        temData.view.sizeList = data.data.goods_list.length > 0 ? data.data.goods_list[0].size_list : [];

        //根据库存设置该尺码是否可以选择
        let size_length = 0;
        if (temData.view.sizeList) {
          size_length = temData.view.sizeList.length;
        }
        temData.view.sizeList && temData.view.sizeList.map((item, index) => {
          item.enable = item.storage_number > 0 ? true : false;

          if (index == 0 && size_length == 1 && colorSelected) {
            //当颜色被选中 并且只有一个size 时 才会自动 做选中尺码操作
            temData.view.buyButtonEnable = item.enable;
            if (item.enable) {
              item.selected = true;
              that.setData({
                selectedSKU: item.product_sku,
              });
            }
          }

        });

        temData.view.image = ImgUrls.length > 0 ? ImgUrls[0] : '';
        temData.view.price = data.data.format_market_price;
        temData.view.goodPrice = data.data.format_sales_price;

        that.setData({
          pickerData: temData,
        });

        // Picker.pickerShow(event, that);
        this.setData({pickerShow: true});
      })
      .catch(error => {

      });
  },

  pickerTap: function (event) {
    if (event.target.id == "picker-bg") {
      var that = this
      // Picker.pickerHide(event, that);
      this.setData({pickerShow: false});

      this.resetPickerData();
    }
  },

  resetPickerData: function (event) {
    let pickerData = this.data.pickerData;
    pickerData.view.buyNumber = 1;
    this.setData({
      pickerData,
      selectedSKU: 0,
      storageNumber: 0,
      curProcessGoodsItem: {},
    })
  },

  // 添加加价购商品到购物车
  addShopCart: function (event) {

    this.setData(event.detail);
    
    let select_sku = that.data.selectedSKU;
    let select_skn = that.data.current_skn;
    let goods_type = that.data.is_gift ? 1 : 2;

    if (!select_sku) {
      tt.showToast({
        title: '请选择颜色或尺码',
        icon: 'none',
        duration: 1000
      });
      return;
    }

    // Picker.pickerHide(event, that);
    this.setData({pickerShow: false});

    that.resetPickerData();

    let param = {};
    if (that.data.status == '10') {
      param = {
        method: 'app.Shopping.add',
        product_sku: select_sku,
        promotion_id: promotion_id,
        buy_number: 1,
        selected: 'Y',
        goods_type: goods_type,
        edit_product_sku: 0,
      };
    } else if (that.data.status == '30') {
      param = {
        method: 'app.Shopping.swapGift',
        new_product_skn: select_skn,
        new_product_sku: select_sku,
        promotion_id: promotion_id,
        goods_type: goods_type,
        buy_number: 1,
      }
    } else {
      tt.navigateBack({

      });
      return;
    }
    // console.log(param);
    api.get({data: param})
      .then(json => {
        if (json && json.code) {
          tt.navigateBack({

          });
        }
      })
      .catch(error => {

      });
  },

  chooseSizeColorCompleted:function(event){

    console.log('====================================');
    console.log(event);
    console.log('====================================');


    this.setData(event.detail);
    
    if (!this.data.selectedSKU) {
      this.wetoast.toast({
        title: '请选择颜色或尺码',
        titleClassName: 'wetoast-title',
        duration: 1000
      });
      return;
    }
  },

  //点击背景,隐藏picker
  showChange:function(){
    let pickerShow = !this.data.pickerShow
    this.setData({pickerShow});
  },

  errorToast: function(event) {
    console.log('====================================');
    console.log(event);
    console.log('====================================');
    let detail = event.detail;
    this.wetoast.toast({
      title: detail.title,
      titleClassName: detail.titleClassName,
      duration: 1000
    });
  },

  /**
   * 查询购物车数据,包括普通购物车和预售购物车
   */
  queryGiftPageData: function () {

    let param = {
      method: 'app.Shopping.queryPromotionGifts',
      uid: app.getUid(),
      promotion_ids: this.data.promotion_ids,
    };

    api.get({data: param})
      .then(json => {
        if (json && json.code && json.code == 200) {
          let data = json.data
          let promotion_data_ary = data.arrays;
          if (promotion_data_ary.length) {

            promotion_data_ary.map((item, index) => {
              item.goods_list.map((obj, index) => {
                let default_images = getImageUrlWithWH(obj.goods_images, 75, 100);
                let default_last_price = obj.last_price ? obj.last_price : 0;
                obj.last_price = '¥' + default_last_price.toFixed(2);
                obj.goods_images = default_images;
              });
            });

            let title = promotion_data_ary[0].promotion_title;
            this.setData({ current_section_title: title });
          }
          this.setData({ promotion_data_ary });

          // console.log(this.data);
        }
      })
      .catch(error => {

      });
  },

})