giftPage.js 10.2 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
// giftPage.js
import api from '../../common/api.js';
import { shouldDiscardTap, getImageUrlWithWH } from '../../utils/util';

//获取应用实例
let app = getApp();
// var Picker = require('../goodsDetail/picker/picker.js')

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 需要的数据
    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();
    var that = this;
    // Picker.init(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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 });
    // Picker.promotion_id = data.item.promotion_id;
    // console.log(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);
      })
      .catch(error => {

      });
  },

  //点击选择颜色
  colorTap: function (event) {
    //更新各个颜色的选中状态
    var that = this
    var tempData = that.data.pickerData;
    //先遍历将所有颜色、尺码置为未选中状态
    // console.log('color')
    // console.log(tempData.view.goodsList)

    tempData.view.goodsList && tempData.view.goodsList.map((item, index) => {
      item.selected = false
      item.size_list.map((subItem, index) => {
        subItem.selected = false
        subItem.enable = subItem.storage_number > 0 ? true : false
      });
    });

    //将当前颜色置为选中状态
    var tempItem = event.target.dataset.key

    //遍历颜色尺码,根据库存切换显示状态
    tempItem && tempItem.size_list && tempItem.size_list.map((item, index) => {
      item.enable = item.storage_number > 0 ? true : false
    })

    tempItem.selected = true

    //重置库存、当前选中sku的值
    tempData.storageNumber = 0
    tempData.selectedSKU = 0

    tempData.view.goodsList[Number(event.target.id)] = tempItem
    tempData.view.sizeList = tempItem.size_list

    //将购买数量设为初始状态
    tempData.view.buyNumber = 1

    this.setData({
      pickerData: tempData,
    });
  },

  //点击选择尺码
  sizeTap: function (event) {
    var that = this
    var tempData = that.data.pickerData

    //现遍历将所有尺码置为未选中状态
    tempData.view.sizeList && tempData.view.sizeList.map((item, index) => {
      item.selected = false
    });

    //将当前尺码置为选中状态
    var tempItem = event.currentTarget.dataset.key
    tempItem.selected = true
    tempData.view.sizeList[parseInt(event.currentTarget.id)] = tempItem

    //设置数量加减按钮启用状态
    tempData.view.buyButtonEnable = true
    //将购买数量设为初始状态
    tempData.view.buyNumber = 1
    // console.log(event)
    if (tempItem.storage_number <= 0) {
      tt.showToast({
        title: '该尺码已经售罄',
        icon: 'none',
        duration: 1000
      });

      tempData.view.buyButtonEnable = false
    }
    this.setData({
      pickerData: tempData,
      selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0,
      storageNumber: tempItem.storage_number,
    });
  },

  //增加购买数量
  bindPlus: function (event) {

  },

  //减少购买数量
  bindMinus: function (event) {

  },

  pickerTap: function (event) {
    if (event.target.id == "picker-bg") {
      var that = this
      // Picker.pickerHide(event, that);
      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) {
    var that = this

    let select_sku = that.data.selectedSKU;
    let select_skn = that.data.current_skn;
    let goods_type = that.data.is_gift ? 1 : 2;
    let promotion_id = Picker.promotion_id;

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

    // Picker.pickerHide(event, that);
    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 => {

      });
  },

  /**
   * 查询购物车数据,包括普通购物车和预售购物车
   */
  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 => {

      });
  },

})