create-wish.js 9.47 KB
import {
  getBindtapData,
  getObjByListKeyValue
} from '../../utils/util';

import {
  productMap
} from '../../const/custom-data';

let app = getApp();
Page({
  data: {
    tipsBorderVisible: false,
    tipsRegisterVisible: false,
    tipsGroupMemberVisible: false,
    tipsNewMemberVisible: false,
    tipsWishVisible: false,
    tipsShakeVisible: false,
    tipsCommonVisible: false,
    tipsCreateCompleteVisible: true,
    tipsInnerText: "",
    curStatus: 1, // 当前场景
    candidate: [], // 心愿候选列表
    myWishList: [], // 我的心愿列表
    valElasticTotal: 0, // 心愿单累计需要弹力值
    taskTotalElasticValue: 0, // 任务分
    wishBillInfo: {}, // 生成的心愿单
    curWish: {}, // 当前选择心愿
    wishInfo: {}, // 创建后的心愿信息
    wishList: [], // 创建后的心愿单列表
    userInfo: {},
    isPageVisible: false,
  },
  onShareAppMessage() {},
  onShow() {
    this.isPageVisible = true;
  },
  onHide() {
    this.isPageVisible = false;
  },
  onLoad(options) {
    console.log("cwish------");
    this.initData();
  },
  initData() {
    // 还原缓存心愿单
    let myWishList = app.store.getItem("wish") || [];
    this.setData({
      myWishList
    })

    app.queryIndex().then((result) => {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      this.queryWishbillPrizeCandidate();
      this.queryWishbillDetail();
    });
    this.initShake();
  },
  // 添加摇一摇
  initShake() {
    let _this = this;
    wx.onAccelerometerChange(function (res) {
      if (this.data.isPageVisible && this.data.isPageVisible) {
        if (res.x > 2) { //偏移量为2时触发,有的使用1
          wx.showModal({
            title: '提示',
            content: '触发摇一摇',
            success: res => {
              _this.setData({
                tipsShakeVisible: false,
                tipsCreateCompleteVisible: true
              })
            }
          })
        }
      }
    });
  },
  /**
   * 提交前判断状态
   * 判断顺序
   * 1.crm登陆
   * 2.黑名单 (团购会员)
   * 3.新会员
   *
   */
  checkSubmit() {
    return new Promise((resolve, reject) => {
      let indexInfo = app.globalData.indexInfo;
      let {
        isCrmLogin,
        isNewMember,
        isBlackList,
        hadAnswerQuestion,
        isSelfAssist,
        wishBillCode
      } = indexInfo;
      // 未登陆 出注册弹窗提示
      // isCrmLogin = 1;
      if (isCrmLogin == 0) {
        this.setData({
          tipsRegisterVisible: true
        })
        reject();
        return;
      }
      // 黑名单
      if (isBlackList == 1) {
        this.setData({
          tipsGroupMemberVisible: true
        })
        reject();
        return;
      }
      // 新老会员
      // isNewMember
      // hadAnswerQuestion = 1; //硬编码答题
      if (isNewMember == 1) {
        // 新会员
        if (hadAnswerQuestion) {
          // 已经答题,提示创建心愿单
          resolve();
        } else {
          // 未答题,提示答题
          this.setData({
            tipsNewMemberVisible: true
          })
          reject();
        }
      } else {
        // 老会员 直接答题
        resolve();
      }
    });
  },
  /**
   * 点击提交心愿单按钮
   * 首先要根据自身状态判断
   */
  onSubmitHandler() {
    this.checkSubmit().then((result) => {
      let myWishList = this.data.myWishList;
      let valElasticTotal = 0;
      myWishList.forEach(element => {
        valElasticTotal += element.conditionElasticValue
      });
      this.setData({
        tipsWishVisible: true,
        valElasticTotal: valElasticTotal
      })
      console.log("wish:", this.data.myWishList);
    }).catch((err) => {});;
  },
  /**
   * 创建心愿单 prizeDefineCode
   * 创建完毕后
   *
   * -新会员
   *   1.请求创建表单     设置 wishBillInfo
   *   2.显示心愿单完成
   * -老会员
   *   1.请求创建表单
   *   2.显示摇一摇提示页面, 摇手机后,调用自我助力接口  设置 wishBillInfo
   *   3.显示心愿单完成
   */
  queryWishbillCreate() {
    let myWishList = this.data.myWishList
    let wishBillCodeList = [];
    myWishList.forEach(element => {
      wishBillCodeList.push(element.prizeDefineCode);
    });
    console.log("wishBillCodeList:", wishBillCodeList);
    app.post({
      url: app.api.wishbillCreate,
      data: wishBillCodeList,
    }).then((result) => {
      if (app.globalData.indexInfo.isNewMember == 1) {
        // 新会员
        this.setData({
          tipsCreateCompleteVisible: true,
          wishBillInfo: result
        })
      } else {
        // 老会员,自动助力,出提示页面
        app.post({
          url: app.api.selfAssist,
          data: {}
        }).then((result2) => {
          this.setData({
            tipsShakeVisible: true,
            wishBillInfo: result2
          })
        })
      }
    }).catch((err) => {
      console.log("err:", err);
    });
  },
  /**
   * 点击圆点 显示产品提示
   * @param {*} evt
   */
  onShowTipsBorderHandler(evt) {
    let curIndex = getBindtapData(evt, "index");
    console.log("curIndex:", curIndex);
    let curData = productMap[curIndex + ""];
    // let product = this.getProductByCode(curIndex);
    let product = getObjByListKeyValue(curIndex, 'prizeDefineCode', this.data.candidate);
    curData = Object.assign(curData, product)
    console.log("product:", product);
    console.log("curData:", curData);
    this.setData({
      curWish: curData,
      tipsBorderVisible: true,
    });
  },
  /**
   * 隐藏产品提示
   * @param {*} evt
   */
  onHideTipsBorderHandler(evt) {
    console.log("onHideTipsBorderHandler");
    this.setData({
      // curWish: null,
      tipsBorderVisible: false,
    })
  },
  /**
   * 加入心愿单
   * @param {*} evt
   */
  onAddWishHandler(evt) {
    let myWishList = this.data.myWishList;
    let curWish = this.data.curWish;

    // 检查愿望数是否超出
    if (myWishList.length >= 3) {
      this.setData({
        tipsInnerText: "许愿数超出上限啦~",
        tipsCommonVisible: true
      })
      return;
    }

    // // 查重
    let uni = myWishList.some((item) => {
      return item.prizeDefineCode == curWish.prizeDefineCode
    })
    if (uni) {
      this.setData({
        tipsInnerText: "心愿重复啦~\n换个其他心愿试试!",
        tipsCommonVisible: true
      })
      return;
    }

    // 写入愿望单
    myWishList.push(curWish);
    this.setData({
      myWishList
    })
    app.store.setItem("wish", myWishList);
  },
  /**
   * 取消心愿单
   * @param {*} evt
   */
  onCancelWishHandler(evt) {
    let curData = getBindtapData(evt);
    console.log("curData:", curData);
    let myWishList = this.data.myWishList;
    myWishList = myWishList.filter((item) => {
      return item.prizeDefineCode != curData.prizeDefineCode
    })
    console.log("myWishList:", myWishList);
    this.setData({
      myWishList
    })
    app.store.setItem("wish", myWishList);
  },
  /**
   * 换一批
   * @param {*} evt
   */
  onChangeStatusHandler(evt) {
    let curStatus = this.data.curStatus;
    curStatus++;
    if (curStatus > 3) {
      curStatus = 1;
    }
    this.setData({
      curStatus: curStatus,
      tipsBorderVisible: false
    })
  },
  /**
   * 根据唯一码/code 索引
   */
  getProductByCode(code) {
    let result = null;
    let candidate = this.data.candidate;
    candidate.forEach(element => {
      if (element.prizeDefineCode == code) {
        result = element;
      }
    });
    return result;
  },
  // 心愿单产品候选列表
  queryWishbillPrizeCandidate() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.wishbillPrizeCandidate,
        data: {}
      }).then((result) => {
        this.setData({
          candidate: result
        })
        console.log("candidate:", this.data.candidate);
        resolve();
      }).catch((err) => {
        reject();
      });
    });
  },
  // 获取心愿单详情
  queryWishbillDetail() {
    return new Promise((resolve, reject) => {
      app.queryIndex().then((result) => {
        app.post({
          url: app.api.wishbillDetail,
          data: {
            billCode: app.globalData.indexInfo.wishBillCode
          }
        }).then((result) => {
          let wishList = result.wishGifts;
          wishList.forEach(element => {
            let product = productMap[element.prizeDefineCode + ""];
            element = Object.assign(element, product);
          });
          console.log("wishList:", wishList);
          this.setData({
            wishInfo: result,
            wishList: wishList
          })
        })
      })
    });
  },
  // 隐藏所有蒙层
  hideMask() {
    this.setData({
      ruleTipsVisible: false,
      tipsGroupMemberVisible: false,
      tipsNewMemberVisible: false,
      tipsWishVisible: false,
      tipsWishVisible: false,
      tipsCreateCompleteVisible: false,
      tipsCommonVisible: false
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {
      // 隐藏蒙层
      case "_evt_hide_mask":
        this.hideMask();
        break;



        // 创建心愿单
      case "_evt_create_wish":
        this.hideMask();
        this.queryWishbillCreate();
        break;

        // 模拟摇一摇
      case "_evt_shake":
        this.setData({
          tipsShakeVisible: false,
          tipsCreateCompleteVisible: true
        })
        break;

      default:
        break;
    }
  },
})