小程序分享海报的生成

在这里插入图片描述

文档地址

https://developers.weixin.qq.com/miniprogram/dev/extended/component-plus/wxml-to-canvas.html

bug

1、该弹窗要一直存在于页面上:
1.1 弹窗的show属性要一直为true,不能通过变量控制
1.2 不能通过wx:if 控制元素显示隐藏
解决方法:利用变量控制z-index和背景色透明度是否为0来隐藏弹窗,点击分享时候显示
2、海报上所有图片的域名都必须是配置过的合法域名
3、海报上所有图片的域名都必须是https开头
4、海报会导致背景为灰黑色
解决方法:海报不显示的时候给弹窗一个透明
<van-popup
class=“posterPop {{showPoster>0?’’:‘opacity’}}”
show="{{ true }}"

5、海报会压着页面按钮点击动
解决方法:判断海报不显示的时候,提高按钮的层级,加相对定位和z-index {{showPoster>0?‘海报显示’:‘不显示’}}
在这里插入图片描述
2、json文件要引入 wxml-to-canvas 组件
3、在小程序组件中使用canvas渲染会报错,还没有找到哦原因

//HTML
//点击分享按钮
<image
  src="https://file.lojangcc.com/weapp/common/share-btn.png"
  class="fixed share-poster"
  bind:tap="drawPoster"
/>

<van-popup
  class="posterPop"
  show="{{ true }}"
  custom-class="{{showPoster>0?'':'opacity'}}"
  close-icon="clear"
  z-index="{{showPoster}}"
  closeable
  bind:close="onPosterClose"
>
  <wxml-to-canvas class="widget" width="{{310}}" height="{{450}}" />
  <view wx:if="{{drawOver}}" class="flex flex-around pd-32 mt-24 bg-white radius-16">
    <button class="text-center unbutton" open-type="share">
      <image class="actionimg" src="https://file.lojangcc.com/weapp/common/wechat.png" />
      <view>微信好友</view>
    </button>
    <view class="text-center" bind:tap="posterToFile">
      <image class="actionimg" src="https://file.lojangcc.com/weapp/common/down-poster.png" />
      <view>保存至相册</view>
    </view>
  </view>
</van-popup>
//json
{
  "usingComponents": {
    "wxml-to-canvas": "wxml-to-canvas"
  },
  "navigationBarTitleText": "商品详情"
}
//css
.opacity{
	opacity:0;
}
.van-popup {
  overflow-y: visible !important;
}
.goods_img {
  top: -60rpx;
}
.item {
  padding: 10rpx 20rpx;
  border-radius: 8rpx;
}
.btn-solo {
  height: 80rpx;
  border-radius: 40rpx;
}
.detail_img {
  width: 38rpx;
  height: 16rpx;
}

.actionimg {
  width: 45px;
  height: 45px;
}
.unbutton {
  margin: 0;
  padding: 0;
  background-color: transparent;
  color: inherit;
  font-size: inherit;
  line-height: inherit;
}

.unbutton:after {
  border: none;
}

.posterPop .van-popup {
  background-color: transparent;
}

.share-poster {
  width: 100rpx;
  height: 100rpx;
  bottom: 140rpx;
  right: 24rpx;
}
let widget = null

Component({
  data: {
    showPoster: -1000,
    drawOver: false,
  },
//点击分享按钮,调起海报
async drawPoster() {
      this.setData({
        showPoster: 1000,
      })
      if (this.data.drawOver) {
        return
      } else {
        wx.showLoading({
          title: '绘制海报中',
        })
      }
      const { detail, size, id } = this.data
      try {
        const {
          data: { qrcode },
          // TODO: 商品海报接口
        } = await http.get('/goods/getQrcode', {
          params: {
            goods_id: id,
          },
        })
        const wxml = `<view class="poster">
          <view class="thumb-wrap">
            <image class="thumb" src="${detail.cover_Image}"></image>
          </view>
          <view class="price-wrap">
            <text class="disprice">优惠价 ¥${size.price}</text>
            <text class="price">¥${size.market_price}</text>
          </view>
          <view class="content">
            <view class="title">
              <text class="line">${detail.name.slice(0, 15)}</text>
              <text class="line">${detail.name.slice(15)}</text>
            </view>
            <image class="qrcode" src="${qrcode}"></image>
          </view>
        </view>`
        const style = {
          poster: {
            width: 310,
            height: 450,
            flexDirection: 'column',
            backgroundColor: '#fff',
            borderRadius: 8,
          },
          thumbWrap: {
            width: 310,
            height: 310,
            borderRadius: 8,
          },
          thumb: {
            width: 310,
            height: 310,
            borderRadius: 8,
          },
          priceWrap: {
            width: 120,
            height: 65,
          },
          disprice: {
            width: 117,
            height: 18,
            marginTop: 15,
            marginRight: 15,
            marginBottom: 10,
            marginLeft: 15,
            color: '#F22424',
            fontSize: 18,
            lineHeight: 18,
          },
          price: {
            width: 52,
            height: 12,
            marginLeft: 15,
            color: '#000',
            fontSize: 12,
            lineHeight: 12,
          },
          content: {
            width: 290,
            height: 65,
            marginLeft: 15,
            marginRight: 15,
            flexDirection: 'row',
          },
          title: {
            width: 215,
            height: 42,
            fontSize: 14,
            lineHeight: 21,
            flexWrap: 'wrap',
            flexDirection: 'column',
            color: '#333',
          },
          line: {
            width: 210,
            height: 21,
          },
          qrcode: {
            width: 65,
            height: 65,
          },
        }
        wx.nextTick(async () => {
          widget = this.selectComponent('.widget')
          await this.renderToCanvas(wxml, style)
          this.setData({
            drawOver: true,
          })
          wx.hideLoading()
        })
      } catch (error) {
        wx.showToast({
          title: '绘制错误,请稍后重试',
          icon: 'none',
        })
        this.setData({
          showPoster: -1000,
        })
      }
    },
    renderToCanvas(wxml, style) {
      widget.renderToCanvas({ wxml, style }).then(res => {
        this.container = res
      })
    },
    posterToFile() {
      widget.canvasToTempFilePath().then(res => {
        this.setData({
          src: res.tempFilePath,
          width: this.container.layoutBox.width * 2,
          height: this.container.layoutBox.height * 2,
        })
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success() {
            wx.showToast({
              title: '保存成功',
            })
          },
        })
      })
    },
    onPosterClose() {
      this.setData({
        showPoster: -1000,
      })
    },
    onShareAppMessage: function (res) {
      const {
        detail: { name, goods_id },
        memberInfo,
      } = this.data
      if (res.from == 'button') {
        //menu右上角转发
        return {
          title: name,
          path: `/pages/shop/goods-detail/index?id=${goods_id}&mud=${
            memberInfo.user_id || ''
          }`, //点击分享打开首页,在首页判断分享路径,再跳转到详情页
          imageUrl: this.data.swiper[0], //分享的图片路径
        }
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值