snapshootShare.js 7 KB
let app = getApp();
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const rawScreenHeight = app.globalData.systemInfo.screenHeight;
let screenHeight = app.globalData.systemInfo.screenHeight;

const canvasWidth = 375;
const canvasHeight = 667;

const scale = windowWidth / canvasWidth;

screenHeight = windowWidth / (canvasWidth / canvasHeight);

function Point(x, y) {
  return {
    x,
    y
  };
}

Page({
  /**
   * 组件的初始数据
   */
  data: {
    rawScreenHeight,
    screenHeight,
    windowWidth,
    windowHeight,
    scale,
    product_name: '',
    default_image: '',
    product_qrCode: '',
    product_date: '',
    product_price: ''
  },

  onLoad(options) {
    let product_name = decodeURIComponent(options.product_name ? options.product_name : '');
    let default_image = decodeURIComponent(options.default_image ? options.default_image : '');
    let product_qrCode = decodeURIComponent(options.product_qrCode ? options.product_qrCode : '');
    let product_date = decodeURIComponent(options.product_date ? options.product_date : '');
    let product_price = decodeURIComponent(options.product_price ? options.product_price : '');

    this.setData({
      product_name,
      default_image,
      product_qrCode,
      product_price,
      product_date
    });

    this.creatSnapshoot();
  },

  creatSnapshoot() {
    this.creatProductDetailSnapShoot();
  },

  saveToAlbum() {
    let that = this;

    if (wx.getSetting) {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: function() {
                that.saveSnapShoot();
              },
              fail: function(error) {
                if (res.authSetting['scope.writePhotosAlbum'] === false) {
                  wx.showModal({
                    title: '',
                    content: '需要打开小程序的设置,重新授权访问您的系统相册',
                    confirmText: '去开启',
                    confirmColor: '#000000',
                    success: function(r) {
                      if (r.confirm) {
                        wx.openSetting({
                          success: function() {}
                        });
                      }
                    }
                  });
                }
              },
            });
          } else {
            that.saveSnapShoot();
          }
        }
      });
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      });
    }
  },

  saveSnapShoot() {
    var that = this;
    var canvasId = 'productDetailCanvas';

    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: windowWidth,
      height: screenHeight,
      destWidth: windowWidth * 2,
      destHeight: screenHeight * 2,
      quality: 1.0,
      canvasId: canvasId,
      success: function(result) {
        wx.saveImageToPhotosAlbum({
          filePath: result.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            });
          },
        });
      }
    }, this);
  },

  creatProductDetailSnapShoot() {
    let that = this;
    let pCtx = wx.createCanvasContext('productDetailCanvas', this);

    pCtx.setFillStyle('white');
    pCtx.fillRect(0, 0, windowWidth, screenHeight);

    this.drawRect(pCtx, windowWidth, screenHeight);

    pCtx.drawImage('./images/share-logo@2x.png', 131 * scale, 42 * scale, 112 * scale, 22 * scale);

    let defaultImage = this.data.default_image;

    if (defaultImage && defaultImage.indexOf('https://') === -1) {
      defaultImage = defaultImage.replace('http://', 'https://');
    }


    wx.getImageInfo({
      src: defaultImage,
      success: function(res) {
        pCtx.drawImage(res.path, 25 * scale, 89 * scale, 325 * scale, 202 * scale);
        that.drawRoundedRect(that.Rect(25 * scale, 87 * scale, 324 * scale, 435 * scale), 0, pCtx);
        pCtx.draw(true);
      }
    });

    pCtx.drawImage('./images/group@2x.png', 14 * scale, 321 * scale, 320 * scale, 75 * scale);

    this.drawLongText(this.data.product_name, pCtx, 187.5 * scale, 415 * scale, '#444444', 18, 15);

    pCtx.drawImage('./images/shape@2x.png', 41 * scale, 464 * scale, 12 * scale, 12 * scale);

    this.drawLongText(this.data.product_date, pCtx, 140 * scale, 475 * scale, '#444444', 14, 20);
    this.drawLongText(this.data.product_price, pCtx, 287.5 * scale, 475 * scale, '#D0021B', 18, 20);

    wx.getImageInfo({
      src: this.data.product_qrCode,
      success: function(res) {
        pCtx.drawImage(res.path, 30 * scale, 551 * scale, 80 * scale, 80 * scale);
        pCtx.draw(true);
      }
    });

    pCtx.setFillStyle('black');
    pCtx.setFontSize(15);
    pCtx.fillText('长按扫码查看详情', 217 * scale, 580 * scale);
    pCtx.setFillStyle('#b0b0b0');
    pCtx.setFontSize(12);
    pCtx.fillText('还有更多夯货等你来抢', 217 * scale, 608 * scale);
    pCtx.draw();
  },

  Rect(x, y, w, h) {
    return {
      x: x,
      y: y,
      width: w,
      height: h
    };
  },

  drawLongText(longtext, cxt, begin_width, begin_height, color, fontSize, numsForLine) {
    var linelenght = 20 * scale; // 行间距
    var numberOfLine = 2;
    var text = '';
    var count = 0;
    var lineNumber = 1;
    var stringLenght = longtext.length;
    var newtext = longtext.split('');

    cxt.setFillStyle(color);
    cxt.setFontSize(fontSize * scale);
    cxt.setTextAlign('center');

    for (let i = 0; i <= stringLenght; i++) {
      if (count === numsForLine) {
        if (lineNumber === numberOfLine) {
          let t = text + '...';

          cxt.fillText(t, begin_width, begin_height);
        } else {
          cxt.fillText(text, begin_width, begin_height);
        }
        begin_height = begin_height + linelenght;
        text = '';
        count = 0;
        lineNumber++;
      }

      if (i === stringLenght) {
        cxt.fillText(text, begin_width, begin_height);
      }
      text = text + newtext[0];
      count++;
      newtext.shift();
      if (lineNumber > numberOfLine) {
        break;
      }
    }
  },
  hidden() {
    wx.navigateBack();
  },
  drawRoundedRect: function(rect, r, ctx) {
    ctx.setLineWidth(1);
    ctx.setStrokeStyle('rgba(0,0,0,0.1)');
    ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);

    // ctx.stroke();
  },
  drawRect: function(ctx, width, height) {
    ctx.beginPath();
    ctx.setStrokeStyle('black');
    const lineWidth = 6;

    ctx.setLineWidth(lineWidth);
    ctx.moveTo(0, lineWidth / 2);
    ctx.lineTo(width, lineWidth / 2);

    ctx.moveTo(width - lineWidth / 2, 0);
    ctx.lineTo(width - lineWidth / 2, height);

    ctx.moveTo(width, height - lineWidth / 2);
    ctx.lineTo(0, height - lineWidth / 2);

    ctx.moveTo(lineWidth / 2, height);
    ctx.lineTo(lineWidth / 2, 0);

    ctx.stroke();
  },
});