小程序分享(好友和海报)功能

点击分享按钮展示分享的弹框内容

<!-- 分享 -->
  <view class="share-modal" wx:if="{{isShare}}" catchtouchmove="true">
    <view class='close_mask' bindtap="closeMask">×</view>
    <view wx:if="{{!sharePoster}}" class="goods-info" style="width:{{windowW * 0.7}}px;margin-left:{{windowW * 0.1}}px">
      <image class="image" style="height:{{ windowW * 0.9 }}px;width:{{windowW * 0.6}}px;margin-left:{{windowW * 0.05}}px" src='{{goods_images[0].large}}' mode='aspectFit'></image>
      <view class="name" style="margin-left:{{windowW * 0.05}}px">{{goodsName}}</view>
      <view class="price" style="margin-left:{{windowW * 0.05}}px">¥{{price}}</view>
      <view class="share-list">
        <button open-type="share" class="item">
          <image class="icon" src='/resources/image/wechat.png'></image>
          <view>分享给朋友</view>
        </button>
        <view class="item" bindtap="sharePoster">
          <image class="icon" src='/resources/image/timeline.png'></image>
          <view>分享到朋友圈</view>
        </view>
      </view>
    </view>
    <view wx:else>
      <canvas style="width:99.9%;height:{{windowW * 1.1 + 40}}px;top:10px;" canvas-id="canvasBox" />
      <view class="share-poster" bindtap="saveShareImg">
        <button>点击保存相册分享朋友圈</button>
      </view>
    </view>
  </view>
  <view class="mask" catchtouchmove="preventTouchMove" wx:if="{{isShare}}"></view>
/* 点击分享弹窗 */

.mask {
 width: 100%;
 height: 100%;
 position: fixed;
 top: 0;
 left: 0;
 background: #000;
 z-index: 9000;
 opacity: 0.7;
}

.share-modal {
 width: 90%;
 position: fixed;
 top: 50%;
 transform: translateY(-50%);
 left: 0;
 z-index: 9999;
 margin: 0 5%;
 background-color: #fff;
 padding-top: 16px;
}

.share-poster {
 margin-top: 32px;
 text-align: center;
}
.share-poster button {
 display: inline-block;
 padding: 0 38px;
 background: #fff;
 font-size: 12px;
 border-radius: 0;
 border: 1px solid #333333;
}
.goods-info .image {
 display: flex;
 align-items: center;
}
.goods-info .name,
.goods-info .price {
 font-size: 12px;
 margin: 4px 0;
}
.share-list {
 display: flex;
 margin: 12px 0;
}
.share-list .item {
 flex: 1;
 text-align: center;
 font-size: 12px;
 color: #c4c3c3;
 line-height: normal;
 background: #ffffff;
}
.share-list .icon {
 width: 40px;
 height: 40px;
}
.close_mask {
 color: #c4c3c3;
 position: absolute;
 right: 6px;
 top: 0px;
 font-size: 20px;
}

/*弹窗结束 */
page({
  data:{
  	// 分享
    isShare: false, // 点击分享按钮展示弹框
    sharePoster: false, // 是否展示海报
    windowW: 0,
    isSquare: false
  },
  // 点击分享弹窗
  share: function() {
    this.setData({
      isShare: true
    })
  },
  // 点击关闭弹框按钮
  closeMask: function() {
    this.setData({
      isShare: false,
      sharePoster: false
    })
  },
  // 分享到朋友圈
  sharePoster: function() {
    this.getMiniQRCode()
  },
  // 获取小程序二维码
  getMiniQRCode: function() {
    var that = this
    net.ckGetRequest('/share/getWxCode', param, function(data) {
      if (data.Status == 'false') {
        wx.showToast({
          title: data.Msg,
          icon: 'none'
        })
        return
      }
      var result = data.Result
      var url = result.url
      that.getAvaterInfo(url)
    })
  },

  //下载商品图片
  getAvaterInfo: function(url) {
    var that = this
    var goodsImage = ''
    wx.showLoading({
      title: '生成中...',
      mask: true
    })
    wx.getImageInfo({
      src: that.data.goods_images[0].large,
      success: function(res) {
        var isSquare = false
        if (res.width == res.height) {
          isSquare = true
        }
        that.setData({
          isSquare: isSquare,
          sharePoster: true
        })
      }
    })
    wx.downloadFile({
      url: that.data.goods_images[0].large, //图片路径
      success: function(res) {
        if (res.statusCode === 200) {
          goodsImage = res.tempFilePath
          wx.downloadFile({
            url: url, //图片路径
            success: function(res) {
              wx.hideLoading()
              if (res.statusCode === 200) {
                var image = res.tempFilePath //下载成功返回结果
                that.sharePosteCanvas(goodsImage, image) //生成海报
              } else {
                wx.showToast({
                  title: '图片下载失败!',
                  icon: 'none',
                  duration: 2000,
                  success: function() {
                    var image = ''
                    that.sharePosteCanvas(goodsImage, image)
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  //将金额绘制到canvas的固定
  setPrice: function(ctx) {
    let windowW = wx.getSystemInfoSync().windowWidth
    let price = this.data.price
    ctx.setFontSize(12)
    ctx.setFillStyle('#000')
    ctx.fillText('¥' + price, windowW * 0.15, windowW * 0.9 + 50)
    ctx.stroke()
  },
  //将标题绘制到canvas的固定
  setName: function(ctx) {
    let windowW = wx.getSystemInfoSync().windowWidth
    let name = this.data.goodsName
    ctx.setFontSize(12)
    ctx.setFillStyle('#000')
    ctx.fillText(name, windowW * 0.15, windowW * 0.9 + 30)
    ctx.stroke()
  },
  //将说明绘制到canvas固定
  setText: function(ctx) {
    let windowW = wx.getSystemInfoSync().windowWidth
    var text = '长按识别小程序码访问'
    ctx.setFontSize(12)
    ctx.setFillStyle('#000')
    ctx.fillText(text, windowW * 0.15, windowW * 0.9 + 100)
    ctx.stroke()
  },
  // 绘制canvas
  //将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
  sharePosteCanvas: function(image, qrcode) {
    let windowW = wx.getSystemInfoSync().windowWidth
    let windowH = wx.getSystemInfoSync().screenHeight
    const ctx = wx.createCanvasContext('canvasBox', this)
    // 设置背景颜色
    ctx.setFillStyle('#FFF')
    console.log(windowW, windowH)
    ctx.fillRect(0, 0, windowW, windowH)
    if (this.data.isSquare) {
      ctx.drawImage(
        image,
        windowW * 0.15,
        10 + 0.15 * windowW,
        windowW * 0.6,
        windowW * 0.6
      ) //这里是商品图片
    } else {
      ctx.drawImage(image, windowW * 0.15, 10, windowW * 0.6, windowW * 0.9) //这里是商品图片
    }
    ctx.drawImage(
      qrcode,
      windowW * 0.55,
      windowW * 0.9 + 20,
      windowW * 0.2,
      windowW * 0.2
    ) //这里是商品图片
    this.setName(ctx)
    this.setPrice(ctx)
    this.setText(ctx)
    ctx.draw()
  },
  //点击保存到相册
  saveShareImg: function() {
    var that = this
    wx.showLoading({
      title: '正在保存',
      mask: true
    })
    setTimeout(function() {
      wx.canvasToTempFilePath({
        canvasId: 'canvasBox',
        success: function(res) {
          wx.hideLoading()
          var tempFilePath = res.tempFilePath
          that.checkWritePhotosAlbum(tempFilePath)
        },
        fail: function(err) {
          console.log(err)
        }
      })
    }, 1000)
  },
  // 检查是否有报错相册权限
  checkWritePhotosAlbum: function(filePath) {
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              that.saveToPhoto(filePath)
            },
            fail() {
              wx.openSetting({
                success(res) {
                  if (res.authSetting['scope.writePhotosAlbum']) {
                    that.saveToPhoto(filePath)
                  }
                },
                fail(res) {
                  wx.showToast({
                    title: '您没有授权,无法保存到相册',
                    icon: 'none'
                  })
                }
              })
            }
          })
        } else {
          that.saveToPhoto(filePath)
        }
      }
    })
  },
  // 保存到相册
  saveToPhoto: function(filePath) {
    wx.saveImageToPhotosAlbum({
      filePath: filePath,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册,赶紧晒一下吧~',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#333',
          success: function(res) {
            that.closeMask()
          },
          fail: function(res) {
            console.log(res)
          }
        })
      },
      fail: function(res) {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值