groupPurchase.js 18.7 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
import config from '../../common/config'
let plugin = requirePlugin("yoho-utils");
import { jumpByUrl } from '../../libs/urlRoute';
import { getQRCodeSource } from '../../libs/miniQRCodeRoute.js'
import {
  logEvent,
  YB_PAGE_OPEN_L,
  YB_GROUP_BUY_BANNER_C,
} from '../../libs/analytics.js'


let app = getApp();
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const DEVICE_WIDTH_RATIO = windowWidth / 320;
let listWidth = Math.ceil(137.5 * DEVICE_WIDTH_RATIO);
let listHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);
const screenHeight = app.globalData.systemInfo.screenHeight;

const IMAGE_WIDTH = 145;
const IMAGE_HEIGHT = 193;
const IMAGE_RATIO = IMAGE_HEIGHT / IMAGE_WIDTH;
let listImageWidth = listWidth;
let listImageHeight = Math.ceil(listWidth * IMAGE_RATIO);

const shareViewWidth = windowWidth;
// const ratio = windowWidth / windowWidth;
const shareViewHeight = screenHeight;


const SC = screenHeight / shareViewHeight;

Page({

  data: {
    loading: false,
    loginFailded: false,
    activityId: '',
    activityList: [],
    bannerHeight:windowHeight * 0.182,
    tabHeight:windowHeight * 0.068,
    selectorHeight:windowHeight * 0.06,
    tabLineWidth:windowWidth * 0.24,
    tabLineHeight:windowHeight*0.003,

    isLoadingMore:false,
    currentPage:1,
    canLoadMore:false,
    banner:{
      imageUrl:'',
      imagePath:''
    },

    isHome:true,

    sharerData: {
      isShow: false,
      defaultImage:'',
      showPage:1, // 1 活动列表页 2 拼团结果页
      showType: 1, // 1 : 分享页  2 : 截图页
      // goodDetail: {},
      shareViewWidth: shareViewWidth,
      shareViewHeight: shareViewHeight,
      windowWidth,
      screenHeight,
      SC: SC,
      qrCode: '',
      shareTitle:'',
      shareContent:'',
      buttonIcon: {
        wechat: '../../images/share_wechat@2x.png',
        wechatP: '../../images/share_wxpeng@2x.png'
      },
  },
  },
  onReachBottom:function(){
    let canLoad = this.data.canLoadMore
    let isLoadingMore = this.data.isLoadingMore
    if(canLoad && !isLoadingMore){
      this.setData({
        isLoadingMore:true
      })
      this.fetchactivityList();
    }
  },
  onLoad: function (options) {
    let that = this;
    if (options.scene && options.scene.length == 32) {
      getQRCodeSource(options.scene)
      .then(json=>{
          if(json){
            // console.log('zjj-get',json)
            that.loadElement(json);
          }else{
            that.loadElement(options);
          }
      })
      .catch(error=>{
        that.loadElement(options);
      })
    }else{
      that.loadElement(options);
    }
  },
  loadElement:function(options){
    let activityId = options.activityId;
    if(!activityId || activityId == undefined || activityId.length<0){
      activityId = options.activity_id;
    }
    if (options.title && options.title.length > 0) {
      wx.setNavigationBarTitle({
        title: options.title,
      })
    }
    this.setData({
      activityId,
    });

    let unionType = options.union_type ? options.union_type : '';
    //解析 渠道号
    if (unionType) {
      app.updateUnionType(unionType);
    }

    this.fetchactivityList();
    // this.fetchSourceData();
    this.getShareInfo();

    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route
    let params = {
      PAGE_NAME: 'groupPurchase',
      PAGE_PARAM: this.data.activityId ? this.data.activityId : 0,
      FROM_PAGE_NAME: 'home',
      PAGE_PATH: url
    };

    logEvent(YB_PAGE_OPEN_L, params);
  },
  onShow:function(){
    if (app.globalData.user_union_type) {
      let buttonIcon = {
        wechat: '../../images/share_union_wechat@2x.png',
        wechatP: '../../images/share_union_wxpeng@2x.png'
      }
      let sharerData = this.data.sharerData;
      sharerData.buttonIcon = buttonIcon
      this.setData({
        sharerData
      })
    }
    try{
      let cache = wx.getStorageSync("from_detail");
      if (cache && cache =='groupPurchaseDetail'){
        console.log('跳转回来')
        this.fetchactivityList(true);
        // this.fetchSourceData();
        wx.clearStorageSync("from_detail")
      }
    }catch(error){

    }
  },
  getShareInfo:function(){
    // console.error('--zjj--切换线上测试share_id 切换线上测试share_id 切换线上测试share_id')
    let share_id = 5409; //线上:5409 ;测试:1124
    let param = {
      share_id,
    }
    let that = this;
    plugin.get({
      url: '/operations/api/v5/webshare/getShare',
      data: param
    }).then(data => {
        // console.log('--getShareInfo--',data)
        if (data && data.code == 200 && data.data) {
          // that.globalData.shareInfo = data.data;
          let netData = data.data;
          let sharerData = that.data.sharerData;
          sharerData.defaultImage = netData.bigImage ? netData.bigImage : netData.pic;
          sharerData.shareTitle = netData.title;
          sharerData.shareContent = netData.content;
          that.setData({
            sharerData
          })
        }else{
          let sharerData = that.data.sharerData;
          let shareInfo = app.globalData.shareInfo;
          let defaultImage = shareInfo.bigImage ? shareInfo.bigImage : shareInfo.pic;
          sharerData.shareTitle = shareInfo.title;
          sharerData.shareContent = shareInfo.content;
          if (!defaultImage) {
            defaultImage = 'http://img12.static.yhbimg.com/taobaocms/2018/07/02/10/02c8621044d03d0ae0dd87fef9e124436d.jpg'
          }
          sharerData.defaultImage = defaultImage;
          that.setData({
            sharerData
          })
        }
      })
      .catch(error => {
        // console.log('--getShareInfo--',error)
        let sharerData = that.data.sharerData;
        let shareInfo = app.globalData.shareInfo;
        let defaultImage = shareInfo.bigImage ? shareInfo.bigImage : shareInfo.pic;
        sharerData.shareTitle = shareInfo.title;
        sharerData.shareContent = shareInfo.content;
        if(!defaultImage){
          defaultImage = 'http://img12.static.yhbimg.com/taobaocms/2018/07/02/10/02c8621044d03d0ae0dd87fef9e124436d.jpg'
        }
        sharerData.defaultImage = defaultImage;
        that.setData({
          sharerData
        })
      });
  },
  fetchactivityList: function (clearList = false) {
    var that = this
    let currentPage = this.data.currentPage;
    let param = {
      // method: 'app.collage.productList',
      method:'app.collage.productList.page',
      limit:20,
      page: currentPage,
      activityId: this.data.activityId,
      // debug:'XYZ',
    }
    
    plugin.get({data: param})
      .then(function (data) {
        if (!data ||!data.data){
          wx.showToast({
            title: '未查询到商品信息,请稍后再试',
            icon: 'none',
            duration: 3000
          })
          return
        }
        if (data.data.banner && data.data.banner.length > 0){
          let imagePath = data.data.banner.replace(/{width}/g, 750).replace(/{height}/g, 243).replace('{mode}', 2);
          let bannerHeight = windowWidth * 243 / 750;
          let imageUrl = data.data.jumpUrl;
          let banner = {
            imageUrl,
            imagePath
          };
          that.setData({
            banner,
            bannerHeight
          })
        }
        // data.data && data.data.map((item, index) => {
        data.data && data.data.collageProductVoList && data.data.collageProductVoList.map((item, index) => {
          item.defaultImages = item.defaultImages ? item.defaultImages.replace(/{width}/g, listImageWidth * 2).replace(/{height}/g, listImageHeight * 2).replace('{mode}', 2) : '';
          // item.salesPrice = item.salesPrice ? item.salesPrice.toFixed(2) : '';
          // item.marketPrice = item.marketPrice ? item.marketPrice.toFixed(2): '';
          // item.collagePrice = item.collagePrice ? item.collagePrice.toFixed(2): '';
        });
        let pageTotal = data.data.page_total
        let currentPage = that.data.currentPage
        let canLoadMore = currentPage < pageTotal
        if(canLoadMore){
          currentPage = currentPage + 1
        }
        let activityList = that.data.activityList;
        if(clearList){
          activityList = activityList.clear();
        }
        if (data.data.collageProductVoList && data.data.collageProductVoList != undefined){
          activityList = activityList.concat(data.data.collageProductVoList)
        }
        that.setData({
          activityList,
          canLoadMore,
          currentPage,
          isLoadingMore:false
        });
      })
      .catch(function (error) {
        that.setData({
          isLoadingMore:false
        })
        this.wetoast.toast({
          title: error.code + error.message + '',
          titleClassName: 'wetoast-title',
          duration: 1000
        });
      });
  },

  fetchSourceData:function(){
    let that = this
    //content_code 测试环境:f99fa98a01fdadb09012bbd2b4a28e91
    //             正式环境:ab4ca2161bada40fe7335dce51b8e196
    let param = {
      content_code:'ab4ca2161bada40fe7335dce51b8e196',
    }
    plugin.get({
      url: '/operations/api/v5/resource/get',
      data: param
    }).then(function(data){
        if(data.code == 200 && data.data.length>0 && data.data[0].data){
          let image= data.data[0].data;
          if(image.list && image.list.length >0){
            let imageData = image.list[0]
            let imagePath = imageData.src ? imageData.src.replace(/{width}/g, image.imageWidth).replace(/{height}/g, image.imageHeight).replace('{mode}', 2):'';
            let bannerHeight = windowWidth * image.imageHeight/image.imageWidth
            let banner= {
              imageUrl: imageData.url,
              imagePath
            };
            that.setData({
              banner,
              bannerHeight
            })
          }
        }
      });
  },
  bannerTab:function(event){
    let url = this.data.banner.imageUrl
    jumpByUrl(url,'groupPurchase')
    let param={
      TO_PATH:url
    }
    logEvent(YB_GROUP_BUY_BANNER_C,param)
  },
  groupListCellTapped: function (event) {
    let data = event.currentTarget.dataset;
    let productSkn = data.productSkn;
    let activityId = data.activityId;

    wx.navigateTo({
      url: './groupPurchaseDetail?productSkn=' + productSkn + '&activityId=' + activityId + '&page_name=' + 'groupPurchase' + '&page_param=' + activityId,
    });
  },
  jumpMyGroup:function(event){
    if (!app.isLogin()) {
      wx.showToast({
        title: '请先完成登录/注册,再查看!',
        duration:1500,
        icon:'none'
      })
      return;
    }
    wx.navigateTo({
      url: '../orders/groupOrders?page_name=groupPurchase',
    })
  },
  share:function(options){
    // console.log('--share--')
    let that = this;
    let sharerData = that.data.sharerData;

    let param = {
      activityId: this.data.activityId,
    }
    if(app.globalData.user_union_type){
      param.union_type = app.globalData.user_union_type
    }
    let qrCode = config.domains.api + '/wechat/miniapp/img-check.jpg?param=' + encodeURIComponent(JSON.stringify(param)) + '&miniQrType=15';
    // console.log('--qrcode--',qrCode)
    // let shareInfo = app.globalData.shareInfo;
    // let defaultImage = shareInfo.bigImage ? shareInfo.bigImage : shareInfo.pic;
    // if(!defaultImage){
    //   defaultImage = 'http://img12.static.yhbimg.com/taobaocms/2018/07/02/10/02c8621044d03d0ae0dd87fef9e124436d.jpg'
    // }
    sharerData.isShow = true;
    sharerData.qrCode = qrCode;
    // sharerData.defaultImage = defaultImage;
    that.setData({
      sharerData
    })
  },

  shareHide: function (e) {
    // console.log('shareHide');
    let sharerData = this.data.sharerData;
    sharerData.isShow = false;
    sharerData.showType = 1;

    this.setData({
      sharerData,
    });
  },
  showImage: function (e) {
    let that = this;
    if (wx.getSetting) {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: function (res) {
                that.saveSnapShoot();
              },
              fail: function (error) {
                if (res.authSetting['scope.writePhotosAlbum'] == false) {
                  wx.showModal({
                    title: '',
                    content: '需要打开小程序的设置,重新授权访问您的系统相册',
                    confirmText: "去开启",
                    confirmColor: "#000000",
                    success: function (res) {
                      if (res.confirm) {
                        that.shareHide();
                        wx.openSetting({
                          success: function (res) {

                          }
                        });
                      }
                    }
                  });
                }
              },
            })
          } else {
            that.saveSnapShoot();
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }
   
  },
  saveSnapShoot: function (e) {
    let sharerData = this.data.sharerData;
    sharerData.showType = 2;

    this.setData({
      sharerData,
    });

    this.drawShareSnap('originalCanvasenlargeList', 3)
  },

  drawShareSnap: function (canvasId, num) {
    let that = this;
    let sharerData = that.data.sharerData;

    let scale = SC * num;
    let snapWidth = (shareViewWidth) * scale;
    let snapHeight = (shareViewHeight) * scale;
    const ctx = wx.createCanvasContext(canvasId);
    ctx.setFillStyle('black');
    ctx.fillRect(0,0,snapWidth,snapHeight);
    let contentWidth = snapWidth*0.973;
    let contentHeight = snapHeight * 0.985;
    let contentLeft = (snapWidth - contentWidth)/2;
    let contentTop = (snapHeight - contentHeight)/2;
    ctx.setFillStyle('white');
    ctx.fillRect(contentLeft,contentTop,contentWidth,contentHeight);

    let logoWidth = snapWidth * 0.299;
    let logoHeight = snapHeight * 0.033;
    let logoTop = snapHeight * 0.055;
    let logoLeft = (snapWidth - logoWidth)/2;

    let bigImageWidth = snapWidth * 0.867;
    let bigImageHeight = 0.649 * snapHeight;
    let bigImageTop = logoTop + logoHeight + 0.036 * snapHeight;
    let bigImageLeft = (snapWidth - bigImageWidth) / 2;

    let qrCodeWidth = snapWidth * 0.213;
    if(qrCodeWidth < snapHeight * 0.12){
      qrCodeWidth = snapHeight * 0.12;
    }
    let qrCodeLeft = bigImageLeft;
    let qrCodeTop = bigImageHeight + bigImageTop + snapHeight * 0.045;

    // let tipLeft = qrCodeLeft + qrCodeWidth + 0.072 * scale;
    let tipLeft =contentLeft + snapWidth * 0.339;
    let tip1FontSize = 14 * scale;
    let tip2FontSize = 12 * scale;

    // let tip1Top = qrCodeTop + qrCodeWidth - tip1FontSize - tip2FontSize - 8 * scale)

    let tip1Top = bigImageHeight + bigImageTop + snapHeight * 0.069;
    let tip2Top = bigImageHeight + bigImageTop + snapHeight * 0.111;

    ctx.setFillStyle('#444444');
    ctx.setFontSize(tip1FontSize);
    ctx.setTextBaseline('top');
    ctx.fillText("有货友拼团,潮人的专属福利团",tipLeft,tip1Top);

    ctx.setFillStyle("#b0b0b0");
    ctx.setFontSize(tip2FontSize);
    ctx.setTextBaseline('top');
    ctx.fillText('长按图片识别小程序查看详情',tipLeft,tip2Top);

    ctx.drawImage('./images/logo.png',logoLeft,logoTop,logoWidth,logoHeight);
    ctx.draw(true);

    var defaultImage = sharerData.defaultImage;
    if (defaultImage.indexOf("https://") == -1) {
      defaultImage = defaultImage.replace('http://', 'https://');
    }
    wx.getImageInfo({
      src: defaultImage,
      success:function(result){
          ctx.drawImage(result.path,bigImageLeft,bigImageTop,bigImageWidth,bigImageHeight);
          ctx.draw(true);
      }
    })

    let qrCode = sharerData.qrCode;
    if (qrCode.indexOf("https://") == -1) {
      qrCode = qrCode.replace('http://', 'https://');
    }
    wx.getImageInfo({
      src: qrCode,
      success: function (result) {
        ctx.drawImage(result.path, qrCodeLeft, qrCodeTop, qrCodeWidth, qrCodeWidth);
        ctx.draw(true);
      }
    })
    ctx.draw(true);
  },
  shareSaveImage: function (e) {
    // console.log('shareSaveImage');

    // let params = {
    //   PRD_SKN: this.data.yourJoinItem ? this.data.yourJoinItem.productSkn : '0',
    //   GRP_ID: this.data.group_no ? this.data.group_no + '' : '0',
    // };
    // logEvent(YB_INVITE_PICTURE_SAVE_C, params);
    wx.showLoading({
      title: '图片保存中...',
    })
    var that = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 10000,
      width: (shareViewWidth ) * SC * 3,
      height: (shareViewHeight ) * SC* 3,
      destWidth: (shareViewWidth ) * SC * 6,
      destHeight: (shareViewHeight) *  SC * 6,
      canvasId: 'originalCanvasenlargeList',
      success: function (result) {
        wx.saveImageToPhotosAlbum({
          filePath: result.tempFilePath,
          success(res) {
            // console.log("保存图片成功");
            // let params = {
            //   PRD_SKN: that.data.yourJoinItem ? that.data.yourJoinItem.productSkn : '0',
            //   GRP_ID: that.data.group_no ? that.data.group_no + '' : '0',
            // };
            // logEvent(YB_SHARE_PICTURE_RESULT_L, params);
            wx.hideLoading();
            wx.showToast({
              title:
                '保存成功',
              icon:
                'success',
              duration:
                2000
            })
            that.shareHide();
          },
          fail(err) {
            wx.hideLoading();
            that.shareHide();
          }
        });
      },
    });
  },

  onShareAppMessage: function (options) {//options.from 判断来源
    var that = this;
    let shareInfo = that.data.sharerData;
    let path = '/pages/groupPurchase/groupPurchase?activityId=' + that.data.activityId;
    if (app.globalData.user_union_type) {
      path = path + "&union_type=" + app.globalData.user_union_type;
    }
    // 用户点击右上角分享
    return {
      title: shareInfo.shareTitle, // 分享标题
      //imageUrl: imageUrl,
      desc: shareInfo.shareContent ? shareInfo.shareContent:'', // 分享描述
      path, // 分享路径
    }
  },
})