gift-detail.js 4.19 KB
import {
  getBindtapData,
  getObjByListKeyValue
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    commonTipsCompVisible: false,
    orderSubmitSuccessCompVisible: true,
    cid: "1", // 1积分不足 2.账号未审核
    innerTitle: "",
    innerText: "",
    innerButton: "",
    indexInfo: {},
    item: {},
    remark: "",
    receiverList: [], // 收货地址
    defaultReceiver: null, // 默认收货地址
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {

  },
  onShow() {
    this.initData();
  },
  initData() {
    app.queryIndex().then((result) => {
      app.post({
        url: app.api.receiver,
        data: {}
      }).then((result2) => {
        let defaultReceiver = getObjByListKeyValue(1, "defaultAddress", result2);
        this.setData({
          indexInfo: app.globalData.indexInfo,
          receiverList: result2,
          defaultReceiver: defaultReceiver,
          item: app.globalData.giftInfo,
        })
      })
    })
  },

  showTips(tips) {
    wx.showToast({
      title: tips,
      icon: "none"
    })
  },

  /**
   * 检查提交
   */
  checkSubmit() {
    return new Promise((resolve, reject) => {
      let {
        item,
        defaultReceiver,
        indexInfo
      } = this.data;
      console.log("indexInfo:", indexInfo);
      console.log("item:", item);
      if (indexInfo.memberPoints < item.commodityPrice) {
        // 积分不足
        this.setData({
          commonTipsCompVisible: true,
          cid: "1",
          innerTitle: "积分不足",
          innerText: "使用推广、签到功能\n可获取更多积分!",
          innerButton: "我知道了",
        });
        reject();
        return;
      } else if (indexInfo.auditStatus != "authorization") {
        // 未验证
        this.setData({
          commonTipsCompVisible: true,
          cid: "2",
          innerTitle: "账号未审核",
          innerText: "通过认证的用户才能兑换礼品哦,快去提交资",
          innerButton: "马上去验证",
        });
        reject();
        return;
      } else if (!defaultReceiver && item.commodityType == "object") {
        // 实物奖品必须添加地址
        this.showTips("请先添加地址")
        reject();
        return;
      } else {
        resolve();
      }

    });
  },


  /**
   * 确认兑换 / 提交订单
   */
  onSubmitHandler(evt) {
    let _this = this;
    this.checkSubmit().then((result) => {
      let {
        item,
        defaultReceiver
      } = this.data;
      wx.showModal({
        title: '兑换确认',
        content: `将花费${item.commodityPrice}兑换${commodityTitle}一份`,
        success(res) {
          if (res.confirm) {
            app.post({
              url: app.api.commodityExchange,
              data: {
                commodityCode: item.commodityCode,
                receiverCode: defaultReceiver.receiverCode,
                remark: this.data.remark
              }
            }).then((result) => {

            })
          }
        }
      })
    })
  },
  /**
   * 收获地址管理
   */
  onAddressManagementHandler() {
    app.router.push({
      path: "addressManagement"
    })
  },

  /**
   * 添加收货地址
   * 同地址管理
   */
  onAddressAddHandler() {
    app.router.push({
      path: "addressManagement"
    })
  },

  bindRemarkInput(e) {
    this.setData({
      remark: e.detail.value
    })
  },

  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
      commonTipsCompVisible: false,
      orderSubmitSuccessCompVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      // 隐藏弹窗
      case "_evt_common_comp_button":
        this.hideMask();
        switch (data.cid) {
          // 积分不足
          case "1":
            this.hideMask();
            break;

            // 未验证
          case "2":
            app.router.push({
              path: "vipLogin"
            })
            break;

          default:
            break;
        }

        break;

      default:
        break;
    }
  },
})