create-wish.js 13.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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
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: false,
    tipsInnerText: "",
    curStatus: 1, // 当前场景
    candidate: [], // 心愿候选列表
    myWishList: [], // 我的心愿列表
    valElasticTotal: 0, // 心愿单累计需要弹力值
    taskTotalElasticValue: 0, // 任务分
    wishBillInfo: {}, // 生成的心愿单
    curWish: {}, // 当前选择心愿
    wishInfo: {}, // 创建后的心愿信息
    wishList: [], // 创建后的心愿单列表
    userInfo: {},
    // canShake:false,
    guideIndex: 0,
  },
  onShareAppMessage() {},
  onShow() {},
  onLoad(options) {
    // console.log("cw -- onLoad");
    this.initData();
  },
  initData() {
    this.hideMask();
    // 还原缓存心愿单
    let myWishList = app.store.getItem("wish") || [];
    this.setData({
      myWishList
    })

    app.queryIndex().then((result) => {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      this.queryWishbillPrizeCandidate().then((result) => {
        this.initBorder();
      })
      // this.checkNeedQuestion();
      this.checkFirst();
    });
    this.initShake();
  },
  initBorder() {
    // 第一幕
    // let productList1 = [
    //   "P000000000000000000000000000001", "P000000000000000000000000000002", "P000000000000000000000000000003"
    // ]
    // let targetProductList = productList1;
    // let randomIndex = targetProductList[Math.floor(Math.random() * targetProductList.length)];
    // this.doShowTipsBorder(randomIndex);
    // this.doShowTipsBorder("P000000000000000000000000000008");
    this.doShowTipsBorder("P000000000000000000000000000008");

  },
  // 添加摇一摇
  initShake() {
    let _this = this;
    wx.onAccelerometerChange(function (res) {
      if (!_this.data.tipsShakeVisible) {
        return
      }
      if (res.x > 1) { //偏移量为2时触发,有的使用1
        _this.setData({
          tipsShakeVisible: false,
          tipsCreateCompleteVisible: true
        })
        _this.playShakeSound();
      }
    });
  },

  /**
   * 播放摇动声音
   */
  playShakeSound() {
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true;
    innerAudioContext.src = "https://kdcdn.oss-cn-shenzhen.aliyuncs.com/weapp/marubi/wish-list/mp3/shake.mp3";
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },

  /**
   * 是否第一次进入当前页面
   */
  checkFirst() {
    let isFirst = app.store.getItem("create-wish-first");
    if (!isFirst) {
      this.setData({
        guideIndex: 1
      })
    }
  },

  /**
   * 检验是否需要先答题 是的话弹答题
   * 1.已经登陆
   * 2.是新会员
   * 3.不是黑名单
   * 4.未答题
   */
  checkNeedQuestion() {
    return new Promise((resolve, reject) => {
      let indexInfo = app.globalData.indexInfo;
      let {
        isCrmLogin,
        isNewMember,
        isBlackList,
        hadAnswerQuestion,
      } = indexInfo;
      if (isCrmLogin == 1 && isNewMember == 1 && isBlackList == 0 && hadAnswerQuestion == 0) {
        this.setData({
          tipsNewMemberVisible: true,
        })
      } else {}
      // this.setData({
      //   tipsNewMemberVisible: true,
      // })
      resolve();
    });
  },

  /**
   * 检查是否有资格玩
   * 1.未已经登陆  => 提示登陆
   * 2.非黑名单  => 弹团购会员弹窗
   * 3.是新会员且未答题 => 弹未答题弹窗
   */
  checkCanPlay() {
    return new Promise((resolve, reject) => {
      let indexInfo = app.globalData.indexInfo;
      let {
        isCrmLogin,
        isNewMember,
        isBlackList,
        hadAnswerQuestion,
      } = indexInfo;
      if (isCrmLogin == 0) {
        this.setData({
          tipsRegisterVisible: true,
        })
        reject();
      } else if (isBlackList == 1) {
        this.setData({
          tipsGroupMemberVisible: true,
        })
        reject();
      } else if (isNewMember == 1 && hadAnswerQuestion == 0) {
        this.setData({
          tipsNewMemberVisible: true,
        })
        reject();
      } else {
        resolve();
      }
    });
  },

  /**
   * 提交前判断状态
   * 判断顺序
   * 1.crm登陆
   * 2.黑名单 (团购会员)
   * 3.新会员
   *
   */
  checkSubmit() {
    return new Promise((resolve, reject) => {
      let indexInfo = app.globalData.indexInfo;
      let {
        isCrmLogin,
        isNewMember,
        isBlackList,
        hadAnswerQuestion,
      } = 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
        // })
        app.router.push({
          openType: "reLaunch",
          path: "wish"
        })
      } else {
        // 老会员,自动助力,出提示页面
        app.post({
          url: app.api.selfAssist,
          data: {}
        }).then((result2) => {
          let wishBillInfo = result2;

          wishBillInfo.elasticValueList.forEach(element => {
            let product = productMap[element.prizeDefineCode + ""];
            element = Object.assign(element, product);
          });

          this.setData({
            tipsShakeVisible: true,
            wishBillInfo: wishBillInfo
          })
          console.log("----->");
          console.log("wishBillInfo:", this.data.wishBillInfo);
          console.log("wishInfo:", this.data.wishInfo);
          console.log("wishList:", this.data.wishList);
          console.log("taskTotalElasticValue:", this.data.taskTotalElasticValue);

        })
      }
    }).catch((err) => {
      console.log("err:", err);
    });
  },
  /**
   * 点击圆点 显示产品提示
   * @param {*} evt
   */
  onShowTipsBorderHandler(evt) {
    let curIndex = getBindtapData(evt, "index");
    this.doShowTipsBorder(curIndex);
  },
  doShowTipsBorder(curIndex) {
    console.log("curIndex:", curIndex);
    let curData = productMap[curIndex + ""];
    let product = getObjByListKeyValue(curIndex, 'prizeDefineCode', this.data.candidate);
    curData = Object.assign(curData, product)
    let curWish = curData;

    console.log("curWish:", curData);
    console.log("top:", curWish.point.top + curWish.tips.point.fixTop);
    console.log("left:", curWish.point.left + curWish.tips.point.fixLeft);
    this.setData({
      curWish: curWish,
      tipsBorderVisible: true,
    });
  },
  /**
   * 隐藏产品提示
   * @param {*} evt
   */
  onHideTipsBorderHandler(evt) {
    console.log("onHideTipsBorderHandler");
    this.setData({
      // curWish: null,
      tipsBorderVisible: false,
    })
  },
  /**
   * 加入心愿单
   * @param {*} evt
   */
  onAddWishHandler(evt) {
    this.checkCanPlay().then((result) => {
      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每款产品只可以加入1次心愿单,换其它试试!",
          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 > 4) {
      curStatus = 1;
    }
    this.setData({
      curStatus: curStatus,
      tipsBorderVisible: false
    })
  },

  // 点击蒙层指引
  onGuideHandler() {
    let guideIndex = this.data.guideIndex;
    guideIndex++;
    if (guideIndex > 2) {
      guideIndex = 0;
      app.store.setItem("create-wish-first", "true");
    }
    this.setData({
      guideIndex
    })
  },

  /**
   * 根据唯一码/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;
    }
  },
})