Blame view

pages/zeroSell/originalPriceSell.js 19 KB
lea guo authored
1 2
import ZeroSellService from './service/zero-sell';
import CommonService from './service/common';
lea guo authored
3 4 5 6
import {
  decodePhoneNumber,
  getUnionID,
  decodeUnionId,
lea guo authored
7
  openAuthorizeSettings,
lea guo authored
8
} from '../../utils/login';
lea guo authored
9
import { getQRCodeSource } from '../../libs/miniQRCodeRoute';
lea guo authored
10
import { Toast } from '../../vendors/zanui/index';
lea guo authored
11
import router from './router/router';
lea guo authored
12
import { formatImageUrl, fomartCountdownTime } from '../../utils/util';
lea guo authored
13
import { jumpByUrl } from '../../libs/urlRoute';
lea guo authored
14
import { yasReport, YB_PAGE_OPEN_L, YB_LUCK_DT_JOIN_C } from '../../libs/yas';
lea guo authored
15 16 17 18 19 20 21

const ACTIVITY = {
  UNKNOWN: 0,
  READY: 1, // 活动未开始
  START: 2, // 活动开始
  END: 3, // 活动结束
  LUCK: 4, // 活动已开奖、抽奖结束
lea guo authored
22 23
  END_LESS_PEOPLE: 5, // 人数不足
};
lea guo authored
24 25 26 27 28 29 30 31 32

const ACTION_BAR_STATUS = {
  UNKNOWN: 0,
  READY: 1, // 未开始
  START: 2, // 参加
  ALEADY: 3, // 已参加活动
  WAIT: 4, // 等待抽奖中
  LUCK: 5, // 抽奖结束
  END: 6, // 活动结束
lea guo authored
33 34
  PEOPLE_LIMIT_WAIT: 7, // 人数达到活动限制、已满员、人数不足统一为待开奖状态
};
lea guo authored
35 36

let app = getApp();
lea guo authored
37
let timeoutId = null; // 定时器ID
lea guo authored
38
lea guo authored
39 40 41 42
/**
 * YOHOOD 原价购 抽奖
 */
lea guo authored
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
Page(
  Object.assign(
    {
      /**
       * 页面的初始数据
       */
      data: {
        isGoApp: false,
        tabIdx: '',
        showAuth: false,
        actPrizeId: '',
        shareUid: 0,
        scene: '',
        appParameter: '',
        myPrizeCount: 0,
        product: {},
        avatars: [],
        recommends: [],
        actionStatus: ACTION_BAR_STATUS.UNKNOWN,
        snapData: {},
        hasUnionID: false,
        uid: 0,
        isLogin: false,
        bannerSrc: null,
        bannerUrl: null,
lea guo authored
68
        loading: false,
lea guo authored
69 70 71 72 73 74
        page: 1,
        codeList: [],
        animation: null,
        shareFlag: false,
        notice: {},
        lottery: {},
lea guo authored
75
        formatTimeList: [],
lea guo authored
76
        isCountdownEnd: false,
lea guo authored
77
        activityId: ''
lea guo authored
78
      },
lea guo authored
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function(options) {
        app = getApp();
        wx.hideShareMenu();

        this.service = new ZeroSellService();
        this.commonService = new CommonService();

        let data = {};

        let currentScene = app.globalData.currentScene;
        let isGoApp =
          currentScene == 1036 || currentScene == 1069 ? true : false;
        data.isGoApp = isGoApp;

        if (options.shareUid) {
          data.shareUid = options.shareUid;
lea guo authored
99
        }
lea guo authored
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

        if (options.tabIdx) {
          data.tabIdx = options.tabIdx;
        }

        if (options.actPrizeId) {
          data.actPrizeId = options.actPrizeId;
        }

        let appParameter = `https://m.yohobuy.com?openby:yohobuy={"action":"go.h5","params":{"param":{},"url":"https://m.yohobuy.com/activity/yoluck/${
          data.actPrizeId
        }.html"}}`;
        data.appParameter = appParameter;

        if (options.scene && options.scene.length === 32) {
          data.scene = options.scene;
        }

        this.setData(data);

        new app.WeToast();

        this._getDetailBanner();
        yasReport(YB_PAGE_OPEN_L);
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function() {},

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function() {
        wx.showLoading({
          mask: true,
lea guo authored
137
        });
lea guo authored
138 139 140
        setTimeout(() => {
          app = app || getApp();
          let uid = app.getUid();
lea guo authored
141
          this.setData({
lea guo authored
142 143 144 145 146 147 148 149 150 151
            hasUnionID:
              app.globalData.WXUnion_ID !== null &&
              app.globalData.WXUnion_ID !== '' &&
              app.globalData.WXUnion_ID !== undefined
                ? true
                : false,
            isLogin: app.isLogin(),
            uid,
          });
          this._getUser();
lea guo authored
152
          // this._getGroupList();
lea guo authored
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

          if (this.data.shareUid && this.data.scene) {
            this._init();
          } else if (this.data.scene && !this.data.shareUid) {
            this._fetchParams(this.data.scene).then(() => {
              return this._init();
            });
          } else {
            this._init();
          }
        }, 1100);
      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function() {
        wx.hideLoading();
      },

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

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function() {
        wx.showLoading({
          mask: true,
        });
lea guo authored
185
        const {product = {}} = this.data;
lea guo authored
186
        if(product.status === ACTIVITY.END || product.status === ACTIVITY.LUCK) {
lea guo authored
187 188 189 190 191 192
          wx.navigateTo({url: '/pages/zeroSell/originalPriceSell'})
        }else {
          this._init({isPulldown: true}).then(() => {
            wx.stopPullDownRefresh();
          });
        }
lea guo authored
193 194 195 196 197 198 199 200 201 202 203 204 205
      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function() {
        if (this.data.loading) return;
        let page = this.data.page || 1;
        page = page + 1;
        this.setData({
          page,
          loading: true,
        });
lea guo authored
206
        // this._loadGroupList(page);
lea guo authored
207 208
      },
lea guo authored
209 210
      _getMineCode(actPrizeId) {
        if (this.data.uid) {
lea guo authored
211 212 213
          this.service
            .fetchMyPrizeList({
              uid: this.data.uid,
lea guo authored
214
              actPrizeId,
lea guo authored
215 216
            })
            .then(res => {
lea guo authored
217
              let len = res.data.length;
lea guo authored
218 219 220 221 222 223 224 225 226 227 228
              if (len) {
                res.data.map(value => {
                  value.user_thumb = formatImageUrl(value.user_thumb, 60, 60);
                });
                this.setData({
                  codeList: res.data,
                });
                wx.getSystemInfo({
                  success: sys => {
                    let animation = wx.createAnimation({
                      duration: 400 * len,
lea guo authored
229
                    });
lea guo authored
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
                    setTimeout(
                      function() {
                        wx.createSelectorQuery()
                          .select('.code-list-item-container')
                          .boundingClientRect()
                          .exec(r => {
                            if (r && r[0] && r[0].height) {
                              animation
                                .height(
                                  r[0].height - (65 * sys.screenWidth) / 375,
                                )
                                .step();
                              this.setData({
                                animation: animation.export(),
                              });
                            }
                          });
                      }.bind(this),
                      800,
                    );
                  },
                });
              }
            });
        }
      },
      _getGroupList() {
        this.commonService
          .getGroupList({
            limit: 20,
            page: 1,
          })
          .then(res => {
            if (res.code === 200 && res.data) {
              this.setData({
                groupList: res.data.list,
              });
lea guo authored
267 268
            }
          });
lea guo authored
269
      },
lea guo authored
270
lea guo authored
271 272 273 274 275
      _loadGroupList(page) {
        this.commonService
          .getGroupList({
            limit: 20,
            page,
lea guo authored
276
          })
lea guo authored
277 278 279 280 281 282 283 284 285 286 287 288 289 290
          .then(res => {
            if (res.code === 200 && res.data) {
              let groupList = this.data.groupList || [];
              if (res.data.list.length > 0) {
                this.setData({
                  groupList: groupList.concat(res.data.list),
                  loading: false,
                });
              } else {
                this.setData({
                  loading: false,
                });
              }
            }
lea guo authored
291
          })
lea guo authored
292 293 294 295 296 297 298
          .catch(error => {
            this.setData({
              loading: false,
            });
          });
      },
lea guo authored
299
      _init(options = {}) {
lea guo authored
300
        const { isPulldown, id } = options
lea guo authored
301
        let params = {
lea guo authored
302
          actPrizeId: isPulldown ? '' : this.data.actPrizeId || id,
lea guo authored
303
        };
lea guo authored
304
lea guo authored
305 306 307
        return this.service
          .getYohoodDetail(params)
          .then(r1 => {
lea guo authored
308 309 310 311
            wx.hideLoading();
            if (r1.code === 200 && r1.data) {
              let product = r1.data;
lea guo authored
312
              this._getMineCode(product.id);
lea guo authored
313
              this._formatCountdownTime(product);
lea guo authored
314
lea guo authored
315 316
              this.setData({
                product: product,
lea guo authored
317
                activityId: product.id,
lea guo authored
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
                myPrizeCount: r1.code === 200 ? r1.data.myCodeNum : 0,
                notice: JSON.parse(product.notice || '{}'),
                lottery: JSON.parse(product.lottery_info || '{}'),
              });

              this._setActionStatus();
            } else {
              return Promise.reject();
            }
          })
          .catch(e => {
            wx.hideLoading();
            console.error(e);
          });
      },
lea guo authored
334 335
      _formatCountdownTime(product) {
        let { remaining, id} = product;
lea guo authored
336 337 338 339 340
        // 初始化倒计时时间
        const formatTimeList = fomartCountdownTime(remaining);
        this.setData({
          formatTimeList,
        });
lea guo authored
341 342 343 344 345 346
        // 清除定时器
        clearInterval(timeoutId)
        if(!remaining) {
          return 
        }
        timeoutId = setInterval(() => {
lea guo authored
347 348
          remaining--;
          const formatTimeList = fomartCountdownTime(remaining);
lea guo authored
349
          const isCountdownEnd = formatTimeList.join('') === '000000';
lea guo authored
350
          if (isCountdownEnd) {
lea guo authored
351 352
            // 延时500毫秒调接口
            const timeId = setTimeout(()=> {
lea guo authored
353
              this.changeActionStatus({id});
lea guo authored
354 355
              clearTimeout(timeId)
            },500)
lea guo authored
356
            clearInterval(timeoutId);
lea guo authored
357
          }
lea guo authored
358 359 360
          this.setData({
            formatTimeList
          });
lea guo authored
361 362 363
        }, 1000);
      },
lea guo authored
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
      _fetchParams(code) {
        return getQRCodeSource(code).then(result => {
          this.setData({
            shareUid: +result.shareUid,
            actPrizeId: result.actPrizeId,
          });
        });
      },
      _setActionStatus() {
        let status = ACTION_BAR_STATUS.START;
        let activityStatus = this.data.product.status;

        if (activityStatus === ACTIVITY.UNKNOWN) {
          status = ACTION_BAR_STATUS.END;
        } else if (activityStatus === ACTIVITY.READY) {
          status = ACTION_BAR_STATUS.READY;
        } else if (activityStatus >= ACTIVITY.END) {
          if (this._isInActivity()) {
            if (activityStatus === ACTIVITY.LUCK) {
              status = ACTION_BAR_STATUS.LUCK;
            } else if (
              activityStatus === ACTIVITY.END_LESS_PEOPLE ||
              activityStatus === ACTIVITY.END
            ) {
              status = ACTION_BAR_STATUS.WAIT;
            } else {
              status = ACTION_BAR_STATUS.WAIT;
            }
          } else {
            if (
              activityStatus === ACTIVITY.END_LESS_PEOPLE ||
              activityStatus === ACTIVITY.END
            ) {
              status = ACTION_BAR_STATUS.PEOPLE_LIMIT_WAIT;
            } else {
              status = ACTION_BAR_STATUS.END;
            }
          }
        } else {
          if (this._isInActivity()) {
            status = ACTION_BAR_STATUS.ALEADY;
          } else {
            status = ACTION_BAR_STATUS.START;
          }
lea guo authored
408
        }
lea guo authored
409 410 411

        this.setData({
          actionStatus: status,
lea guo authored
412
        });
lea guo authored
413
      },
lea guo authored
414
lea guo authored
415 416 417 418
      _isInActivity() {
        return this.data.myPrizeCount > 0;
      },
lea guo authored
419
      changeActionStatus(options = {}) {
lea guo authored
420 421 422
        wx.showLoading({
          mask: true,
        });
lea guo authored
423
        this._init(options);
lea guo authored
424
      },
lea guo authored
425
lea guo authored
426
      share(event) {
lea guo authored
427
        this.setData({
lea guo authored
428
          'actionsheet.show': true,
lea guo authored
429
        });
lea guo authored
430
      },
lea guo authored
431
lea guo authored
432 433 434
      _jumpBanner() {
        if (this.data.bannerUrl == null) {
          return;
lea guo authored
435
        }
lea guo authored
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
        jumpByUrl(this.data.bannerUrl);
      },
      _getDetailBanner() {
        let commonService = this.commonService;
        commonService
          .getResourceCode('ccc32dbedf164a52b4efa34383878860')
          .then(data => {
            this.setData({
              bannerSrc: formatImageUrl(data.src, 750, 140, 2),
              bannerUrl: data.url,
            });
          })
          .catch(error => {});
      },

      _getUser() {
        const userInfo = app.getUserInfo();

        if (app.isLogin() && (!userInfo || !userInfo.nickName)) {
          return this.service
            .getUserProfile({ uid: app.getUid() })
            .then(json => {
              if (json && json.code && json.code === 200) {
                userInfo.nickName = json.data.nickname || '';
                userInfo.avatarUrl = json.data.head_ico || '';
                app.setUserInfo(userInfo);
              }
            });
lea guo authored
464
        } else {
lea guo authored
465
          return Promise.resolve();
lea guo authored
466
        }
lea guo authored
467 468 469
      },

      handleZanActionsheetCancel({ componentId }) {
lea guo authored
470
        this.setData({
lea guo authored
471 472 473 474 475 476 477 478 479 480 481
          'actionsheet.show': false,
        });
      },

      handleZanActionsheetClick({ componentId, index }) {
        this.setData({
          'actionsheet.show': false,
        });

        if (index === 1) {
          router.go('snapShare', this.data.snapData);
lea guo authored
482
        }
lea guo authored
483 484 485
      },
      onShareAppMessage(res) {
        let params = {
lea guo authored
486
          TITLE: `有货UFOYOHOOD现场搞事情,点一下原价购买${
lea guo authored
487 488
            this.data.product.name
          }`,
lea guo authored
489
          DESC: '我在YOHOOD发现一个不错的商品赶快来看看吧!',
lea guo authored
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
        };
        this.setData({
          shareFlag: true,
        });
        setTimeout(
          function() {
            this.setData({ shareFlag: false });
          }.bind(this),
          600,
        );
        if (res.from === 'menu') {
          // 用户点击右上角分享
          return {
            title: params.TITLE, // 分享标题
            desc: params.DESC, // 分享描述
lea guo authored
505 506 507
            path: `/pages/zeroSell/originalPriceSell?actPrizeId=${
              this.data.product.id
            }`,
lea guo authored
508 509 510 511 512 513
          };
        } else if (res.from === 'button') {
          // 用户点击分享按钮
          return {
            title: params.TITLE, // 分享标题
            desc: params.DESC, // 分享描述
lea guo authored
514
            path: `/pages/zeroSell/originalPriceSell?actPrizeId=${
lea guo authored
515 516 517
              this.data.product.id
            }&shareUid=${app.getUid()}`,
          };
lea guo authored
518
        }
lea guo authored
519 520 521 522 523 524 525 526 527 528 529 530 531 532
      },
      reportSuccess({ detail }) {
        yasReport(YB_LUCK_DT_JOIN_C, { ACTIVITY_ID: detail });
      },

      getUserInfo: function(e) {
        var that = this;
        if (e.detail.errMsg === 'getUserInfo:ok') {
          decodeUnionId(app.getWechatThirdSession(), e, function(response) {
            if (response.isHaveUnionID) {
              that.setData({
                hasUnionID: true,
              });
            }
lea guo authored
533 534
          });
        } else {
lea guo authored
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
          openAuthorizeSettings(function(response) {
            if (response.isHaveUnionID) {
              that.setData({
                hasUnionID: true,
              });
            }
          });
        }
      },

      getPhoneNumber: function(e) {
        this.setData({
          showAuth: false,
        });

        var that = this;
        if (e.detail.errMsg === 'getPhoneNumber:ok') {
          decodePhoneNumber(e.detail.iv, e.detail.encryptedData, '', function(
            result,
          ) {
            if (result.code != 200) {
              that.wetoast.toast({
                title: result.message,
                titleClassName: 'wetoast-title',
                duration: 1000,
                success: function() {
                  that.loginAndRegisterTapped();
                }.bind(that),
              });
            } else {
              if (result.is_register) {
                that.showZanToast(
                  {
                    title:
                      '欢迎加入Yoho!Family!新人礼包已发放到个人中心-优惠券,请注意查收',
                    success: function() {
                      let uid =
                        app.globalData.userInfo.uid > 0
                          ? app.globalData.userInfo.uid
                          : 0;
                      that.setData({
                        uid,
                        isLogin: app.isLogin(),
                      });
                    },
                  },
                  1500,
                );
              } else {
                let uid =
                  app.globalData.userInfo.uid > 0
                    ? app.globalData.userInfo.uid
                    : 0;
lea guo authored
588 589 590 591 592
                that.setData({
                  uid,
                  isLogin: app.isLogin(),
                });
              }
lea guo authored
593 594 595 596 597 598 599 600 601 602 603
            }
          });
        } else {
          let needBind = true;
          that.setData({
            needBind,
          });
          // that.loginAndRegisterTapped(true);
          wx.navigateTo({
            url: '/pages/bindPhoneNumber/bindPhoneNumber',
          });
lea guo authored
604
        }
lea guo authored
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      },
      launchAppError: function(e) {
        wx.showToast({
          title: '打开失败!您可能未安装Yoho!Buy官方APP,请下载后再尝试。',
          icon: 'none',
          duration: 3000,
        });
      },
      //跳转到登录页面
      loginAndRegisterTapped(needBind) {
        let that = this;
        let app = getApp();
        if (wx.getSetting) {
          wx.getSetting({
            success: res => {
              if (
                res &&
                res.authSetting &&
                res.authSetting['scope.userInfo'] !== null &&
                res.authSetting['scope.userInfo'] !== undefined &&
                res.authSetting['scope.userInfo'] === false
              ) {
                wx.showModal({
                  title: '',
                  content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
                  confirmText: '去开启',
                  confirmColor: '#000000',
                  success: function(res) {
                    if (res.confirm) {
                      wx.openSetting({
                        success: res => {
                          if (
                            res.authSetting['scope.userInfo'] !== null &&
                            res.authSetting['scope.userInfo'] !== undefined &&
                            res.authSetting['scope.userInfo'] === true
                          ) {
                            getUnionID(app.getWechatThirdSession(), function(
                              response,
                            ) {});
                          }
                        },
                      });
                    } else {
                      wx.navigateTo({
                        url: '/pages/bindPhoneNumber/bindPhoneNumber',
                      });
lea guo authored
651
                    }
lea guo authored
652 653 654 655 656 657
                  },
                });
              } else {
                wx.navigateTo({
                  url: '/pages/bindPhoneNumber/bindPhoneNumber',
                });
lea guo authored
658
              }
lea guo authored
659 660 661 662 663 664 665 666
            },
          });
        } else {
          wx.showModal({
            title: '提示',
            content:
              '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
          });
lea guo authored
667
        }
lea guo authored
668 669 670 671 672
      },
    },
    Toast,
  ),
);