index.js 16.3 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 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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
import config from '../../common/config';
import api from '../../common/api';
import event from '../../common/event';
import LimitModel from '../../models/limit/index';
import helper from '../../utils/helper';
import wx from '../../utils/wx';
import {Actionsheet} from '../../vendors/zanui/index';
import {stringify} from '../../vendors/query-stringify';
import commonModel from '../../models/common';
import Yas from '../../common/yas';

let app = getApp();
let router = global.router;
let yas;

/**
 * method=app.limitProduct.queryHelpInfo(助力信息)
 method=app.limitProduct.addhelpUserInfo(添加助力信息)
 method=app.drawline.getMyQueueList 获取我的排队列表
 */
/**
 * 接口:获取用户排队信息
 * method=app.drawline.getUserActivityInfo
 * activityId=179
 *
 *
 * 接口:获取排队用户信息(列表)
 * method=app.drawline.getQueueList
 * activityId=179
 * limit=50
 * sort=0开始位置
 *
 *
 * 接口:排队活动信息
 * method=app.drawline.getActivityInfo
 * activityId=179
 *
 *
 * 接口:中奖列表
 * method=app.drawline.getLuckyUserList
 * activityId=179
 * limit=20
 * page=1
 *
 */

Page(Object.assign({

  /**
     * 页面的初始数据
     */
  data: {
    userInfo: {}, // 用户信息
    activityInfo: {}, // 活动信息
    shareInfo: {
      default_image: '',
      product_name: '',
      product_qrCode: '',
      product_skn: '',
      limitProductCode: 0
    },
    test_uid: [349709, // 测试用
      500030924,
      500030922,
      500030918,
      500030916,
      500030914,
      500030912,
      500030910,
      500030908,
      500030906,
      500030904,
      500030890,
      500030888,
      500030886,
      500030884,
      500030876,
      500030830,
      500030694,
      500030690,
      500030688
    ],
    now_test_uid_index: 15,
    h5PageUrl: [
      {
        title: '排队活动说明',
        url: 'https://activity.yoho.cn/feature/249.html?nodownload=1'
      }
    ],
    bgStyle: [ // 排队图片
      '../../static/images/limit/queue_bg_1.png',
      '../../static/images/limit/queue_bg_2.png',
      '../../static/images/limit/queue_bg_3.png',
      '../../static/images/limit/queue_bg_4.png',
      '../../static/images/limit/queue_bg_5.png',
      '../../static/images/limit/queue_bg_6.png',
      '../../static/images/limit/queue_bg_7.png',
      '../../static/images/limit/queue_bg_8.png'
    ],
    vipImage: [ // 3种用户vip等级小图标
      '',
      '../../static/images/limit/level-1.png',
      '../../static/images/limit/level-2.png',
      '../../static/images/limit/level-3.png'
    ],
    defaultAvatar: '../../static/images/icons/default-avatar.png',
    resultHead: [
      '../../static/images/limit/fail-head.png',
      '../../static/images/limit/success-head.png'
    ],
    api_status: 0, // 用于防止连续点击参加活动 0:没调用接口 1:正在调用接口
    giftImage: '../../static/images/limit/gift.png',
    queueList: [], // 排队列表
    scrollTop: 0, // 控制scroll-view滚动的实际位置
    tempScrollTop: 0, // 手动滚动页面时保存当前滚动的位置
    showMsg: 0, // 0不显示提示框, 1显示提示框
    hidePopup: 0, // 用于显示头像旁边的气泡 0:显示 1:不显示
    hideInviteButton: 1, // 用户显示邀请按钮
    actionSheet: {
      componentId: 'shareActionSheet',
      show: false,
      closeOnClickOverlay: true,
      cancelText: '取消',
      actions: [{
        name: '邀请好友助力',
        className: 'action-class',
        loading: false,
        openType: 'share'
      }, {
        name: '生成图片分享到朋友圈',
        className: 'action-class',
        loading: false,
      }]
    },
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function(options) {
    console.log(options);
    let actId = options.actId;
    let uid = app.getUid();

    if (actId) {
      this.getActivityInfo(actId).then(res => {
        console.log('res', res);
        if (res.code === 200) {
          this.getQueueList(actId, 0);
        }
        if (uid) {
          this.getUserActivityInfo(actId, uid);
        }
      }); // 获取活动信息

      for (let i in options) {
        options[i] = decodeURIComponent(options[i]);
      }

      this.setData({
        shareInfo: Object.assign(this.data.shareInfo, options)
      });
    }

    yas = new Yas();
    yas.pageOpenReport();
  },

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

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function(options) {
    let actId = this.data.activityInfo.activityId;
    let uid = app.getUid();

    if (actId && uid) {
      this.getUserActivityInfo(actId, uid);
    }
    setTimeout(this.hidePopup, 4000);
    if (app.getUid()) {
      this.setData({
        hideInviteButton: 0
      });
    }
  },

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

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

  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function() {
    let actId = this.data.activityInfo.activityId;

    if (actId) {
      this.getActivityInfo(actId).then(res => {
        console.log('res', res);
        if (res.code === 200) {
          this.getQueueList(actId, 0);
          wx.stopPullDownRefresh();
        }
      }); // 获取活动信息
    }
  },

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function() {
    console.log('到底部啦');
    let actId = this.data.activityInfo.activityId;
    let sort = this.data.queueList[this.data.queueList.length - 1].sort;
    let limit = 50;

    if (sort > 1) {
      if (sort - 1 < limit) {
        limit = sort - 1;
      }
      this.getQueueList(actId, sort - 1, limit, true);
    }
  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function(res) {
    let path = `/pages/product/detail/detail?limitProductCode=${this.data.shareInfo.limitProductCode}`;

    if (this.data.userInfo.isDrawline) {
      path = `/pages/product/detail/detail?limitProductCode=${this.data.shareInfo.limitProductCode}&originUid=${app.getUid()}&originUnionID=${app.getUnionID()}`; // eslint-disable-line
    }
    let params = {
      FROM: res.from,
      SHARE_RESUIL: 0,
      TITLE: decodeURIComponent(this.data.shareInfo.product_name),
      DESC: '我在有货限定频道发现一个不错的商品赶快来看看吧!',
      PATH: path,
      IMG: decodeURIComponent(this.data.shareInfo.default_image)
    };

    // 用户点击右上角分享
    return {
      title: params.TITLE, // 分享标题
      desc: params.DESC, // 分享描述
      path: params.PATH, // 分享路径
      imageUrl: params.IMG,
      success: function() {
        params.SHARE_RESUIL = 1;
      },
      fail: function() {
        params.SHARE_RESUIL = 2;
      }
    };
  },
  share: function() {
    this.setData({
      'actionSheet.show': true
    });
  },
  handleZanActionsheetCancel() {
    this.setData({
      'actionSheet.show': false
    });
  },

  // 当行动按钮中有一个被点击时触发
  // index 代表被点击按钮在传入参数 actions 中的位置
  handleZanActionsheetClick({index}) {
    this.setData({
      'actionSheet.show': false
    });

    let uid = this.data.originUid || this.data.uid;

    if (index === 1) {
      router.go('snapShare', this.data.shareInfo);
    }
  },

  /**
     * 邀请好友助力排队按钮
     */
  inviteFriends: function(e) {
    this.share();
  },

  /**
     * scroll-view滚动事件
     */
  scroll: function(e) {
    console.log(e);
    let scrollTop = e.detail.scrollTop;

    this.setData({
      tempScrollTop: scrollTop
    });
  },

  /**
     * 跳转到我的排队列表
     */
  navigateToMyList: function(e) {
    router.go('myList', Object.assign({actId: this.data.activityInfo.activityId}, this.data.shareInfo));
  },

  /**
     * 点击用户头像,跳转到用户对应位置<已取消该功能>
     */
  userTap: function(e) {
    console.log(this.data.userInfo);
    let uid = this.data.userInfo.uid;
    let sort = this.data.userInfo.sort || 0;
    let limit = sort >= 50 ? 50 : sort;
    let actId = this.data.activityInfo.activityId;
    let list_length = this.data.queueList.length > 200 ? 200 : this.data.queueList.length;

    for (let i = 0; i < list_length; i++) {
      let item = this.data.queueList[i];

      if (item.uid === uid) {
        this.scrollPage('item_' + item.uid);
        console.log(item.uid);
        break;
      } else if (i === list_length - 1) {
        console.log(i);
        this.getQueueList(actId, sort, limit).then(res => {
          console.log('111', res);
          if (res.code === 200) {
            let item_id = 'item_' + uid;

            this.scrollPage(item_id);
          }
        });
      }
    }
  },

  /**
     * 关闭提示框
     */
  closeMsg: function(e) {
    this.setData({
      showMsg: 0
    });
  },

  /**
     * 隐藏气泡
     */
  hidePopup: function(e) {
    this.setData({
      hidePopup: 1
    });
  },

  /**
     * 点击参加活动按钮
     */
  joinInQueue: function(e) {
    let uid = app.getUid();
    let activityId = this.data.activityInfo.activityId;
    let isEnd = this.data.activityInfo.isEnd === 0 ? 0 : 1;

    if (activityId && uid && !isEnd) {
      this.addQueue(this.data.activityInfo.activityId, uid);
    } else if (isEnd) {
      this.setData({
        showMsg: 1
      });
    } else if (!uid) {
      let pages = getCurrentPages();
      let referer = pages[pages.length - 1].route + '?' + decodeURIComponent(stringify(this.data.shareInfo));

      router.go('userCenter', {referer});
    }
  },

  /**
     * 获取活动信息
     */
  getActivityInfo: function(actId) {
    return LimitModel.getActivityInfo(actId).then(res => { // 获取活动信息
      console.log(res);
      if (res.code === 200) {
        let topBanner = helper.image(res.data.topBanner, 750, 352, 1);
        let isBegin = res.data.isBegin;
        let isDrawEnd = res.data.isDrawEnd;
        let isEnd = res.data.isEnd;
        let prizeType = res.data.prizeType;
        let activityName = res.data.activityName;
        let activityId = res.data.activityId;
        let bgStyle = res.data.bgStyle && res.data.bgStyle.length > 0 ? res.data.bgStyle : this.data.bgStyle;

        this.setData({
          activityInfo: {
            topBanner: topBanner,
            isBegin: isBegin, // 是否已开始 0-未开始 1-进行中 2-已结束
            isDrawEnd: isDrawEnd, // 是否已开奖 1-开奖 0-未开奖
            isEnd: isEnd, // 是否已结束 1-结束 0-未结束
            prizeType: prizeType, // 奖品类型1-有货币 2-优惠券 3-限购码
            activityId: activityId,
            activityName: activityName
          },
          bgStyle: bgStyle
        });
        return Promise.resolve({code: res.code});
      } else {
        return Promise.reject({code: res.code});
      }
    });
  },

  /**
     * 加入排队
     */
  addQueue: function(actId, uid) {
    if (!this.data.api_status) {
      this.setData({
        api_status: 1
      });
      LimitModel.addQueue(actId).then((res) => {
        console.log('加入排队', res);
        if (res.code === 200 && res.data) {
          let sort = res.data.sort;

          this.setData({
            userInfo: Object.assign(this.data.userInfo, {sort: sort, isDrawline: 1}),
            showMsg: 1,
            api_status: 0
          });

          yas.report('YB_QUEUE_SUCCESS_L', {LIMIT_CODE: this.data.shareInfo.limitProductCode});
          setTimeout(this.getQueueList, 1000);

          // this.getQueueList(this.data.activityInfo.activityId, 0);
        }
      }).catch(() => {
        console.log('错误');
        this.setData({
          api_status: 0
        });
      });
    }
  },

  /**
     * 获取用户参与排队的信息
     */
  getUserActivityInfo: function(actId) {
    LimitModel.getUserActivityInfo(actId).then(res => {
      console.log('getUserActivityInfo:', res);
      this.setData({
        userInfo: {
          uid: res.data.uid,
          headIco: helper.image(res.data.headIco, 128, 128, 1) || this.data.defaultAvatar,
          isDrawline: res.data.isDrawline,
          isLucky: res.data.isLucky,
          sort: res.data.sort
        }
      });

      let showMsg = wx.getStorageSync('queueMsgShow_' + actId) === 0 ? 0 : 1;

      if (this.data.activityInfo.isDrawEnd) {
        this.setData({
          showMsg: showMsg
        });
        wx.setStorageSync('queueMsgShow_' + actId, 0);
      }
    });
  },

  /**
     * 获取排队列表
     */
  getQueueList: function(actId, start = 0, limit = 50, isAdd = false) {
    let that = this;

    actId = actId || this.data.activityInfo.activityId;

    return LimitModel.getQueueList(actId, start, limit).then(res => {
      console.log(res);
      if (res.data && res.data.length > 0) {
        let result = [];

        res.data.forEach((item, index) => {
          if (!item.headIco) {
            item.headIco = that.data.defaultAvatar;
          } else {
            item.headIco = helper.image(item.headIco, 100, 100, 1);
          }
          item.nickName = item.nickName || '用户' + item.uid;
          item.nickName = item.nickName.length > 12 ? item.nickName.substring(0, 12) + '...' : item.nickName;
          item.queueTime = this.formatTimeText(item.queueTime);
          item.vipLevel = item.vipLevel || 0;
          item.vipImage = this.data.vipImage[item.vipLevel];
          let bgStyle = this.data.bgStyle;
          let bgIndex = index % bgStyle.length;

          item.background = helper.imgView(bgStyle[bgIndex], 340, 162, 1);
          result.push(item);
        });
        if (isAdd) { // 如果是下拉则添加数据
          this.setData({
            queueList: this.data.queueList.concat(result)
          });
        } else { // 普通情况下覆盖
          this.setData({
            queueList: result
          });
        }
        return Promise.resolve({code: 200, data: this.data.queueList});
      } else {
        return Promise.reject({code: 203, message: '获取失败'});
      }
    });
  },
  scrollPage: function(id) { // 滚动页面到对应位置
    if (id) {
      let item_query = wx.createSelectorQuery();

      item_query.select('#' + id).boundingClientRect();
      item_query.selectViewport().scrollOffset();
      item_query.exec(res => {
        console.log('scroll:', res);
        if (res.length > 0 && this.data.queueList.length > 6 && res[0].top !== 0) {
          wx.pageScrollTo({
            scrollTop: res[0].top + res[1].scrollTop,
            duration: 500
          });

          // this.setData({
          //     scrollTop: res[0].top + this.data.tempScrollTop
          // })
        }
      });
    }

  },

  /**
     * 跳转到活动说明的H5页面
     */
  navigateToDescPage: function() {
    router.goUrl(this.data.h5PageUrl[0].url + '&title=' + this.data.h5PageUrl[0].title);
  },

  /**
     *跳转到中奖名单页面
     */
  navigateToPrizeUserList: function() {
    if (this.data.activityInfo.isDrawEnd) {
      router.go('prizeUserList', {actId: this.data.activityInfo.activityId});
    }
  },

  /**
     * 跳转到个人中心限购码页面
     */
  navigateToUserCodePage: function() {
    if (this.data.userInfo.isLucky) {
      router.go('userCenter');
    }
  },

  /**
     * 提交formId消息模板
     */
  submitFormId: function(e) {
    console.log('submitFormId:', { formId: e.detail.formId, uid: app.getUid() || 0, openId: app.getOpenID() || '' });
    commonModel.addWechatFormId({
      formId: e.detail.formId,
      uid: app.getUid() || 0,
      openId: app.getOpenID() || '',
      miniapp_type: app.getMiniappType()
    });
  },

  /**
     * 处理接口返回的图片url
     */
  formatImgUrl: function(url, mode, width, height) {
    return url.replace(/{mode}/g, mode).replace(/{width}/g, width).replace(/{height}/g, height);
  },

  formatTimeText: function(time) {
    let new_time;

    if (time < 60) {
      new_time = time + 'sec';
    } else if (time >= 60 && time < 60 * 60) {
      new_time = parseInt(time / 60, 10) + 'min';
    } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      new_time = parseInt(time / (60 * 60), 10) + 'hour';
    } else if (time >= 60 * 60 * 24) {
      new_time = parseInt(time / (60 * 60 * 24), 10) + 'day';
    }
    return new_time;
  }
}, Actionsheet));