createPoster.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="index">
  3. <view class="center">
  4. <image :show-menu-by-longpress="true" :src="imgUrl"
  5. style="width: 670rpx; height: 940rpx;margin-top: 50rpx;" />
  6. </view>
  7. <view style="position: fixed; top: 999999999999999999999rpx">
  8. <!-- #ifdef MP-WEIXIN -->
  9. <canvas id="canvas" type="2d" style="width: 670rpx; height: 940rpx" />
  10. <!-- #endif -->
  11. <!-- #ifndef MP-WEIXIN -->
  12. <canvas canvas-id="canvas" id="canvas" style="width: 670rpx; height: 940rpx" />
  13. <!-- #endif -->
  14. </view>
  15. <view class="" style="position: fixed;bottom: 150rpx;width: 100%;">
  16. <view class="center">
  17. <view @click="savePhoto" class="cu-btn flex round text-lg"
  18. style="padding: 46rpx 0;background-color: #EF9944;color: #FFFFFF;width: 76%;">
  19. <u-icon name="download"></u-icon>
  20. <text class="margin-left-20">保存到相册</text>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import DrawPoster from "u-draw-poster";
  28. import {
  29. drawQrCode
  30. } from "u-draw-poster";
  31. DrawPoster.useCtx(drawQrCode);
  32. export default {
  33. data: () => ({
  34. imgUrl: '',
  35. }),
  36. async onLoad(options) {
  37. // 创建绘制工具
  38. const dp = await DrawPoster.build({
  39. selector: 'canvas',
  40. loading: true,
  41. debugging: true,
  42. });
  43. const w = (dp.canvas.width = 670);
  44. const h = (dp.canvas.height = 940);
  45. //初始化参数
  46. let index = options.index
  47. let bgImg = `/static/poster/wuliao${index}.png`
  48. let shopDetail = this.$cache.get('selectedShop')
  49. let qrCodeText = this.$global.QR_PATH + this.vuex_shopId
  50. let titleParams={
  51. x:w / 2 - 60,
  52. y:h / 2 - 240,
  53. color:"#000",
  54. text:shopDetail.name
  55. }
  56. let qrParams = {
  57. x: w / 2 - 175,
  58. y: h / 2 - 200,
  59. text: qrCodeText,
  60. size: 350,
  61. margin: 5,
  62. }
  63. if (index==2) {
  64. titleParams.y=h / 2 - 90
  65. qrParams.y=h / 2 - 60
  66. }
  67. if (index==3) {
  68. titleParams.color='#fff'
  69. titleParams.y=h / 2 - 150
  70. qrParams.y=h / 2 - 120
  71. }
  72. if (index==4) {
  73. titleParams.y=h / 2 - 240
  74. qrParams.y=h / 2 - 210
  75. }
  76. // 绘制基本背景
  77. dp.draw((ctx) => {
  78. ctx.fillStyle = '#ffffff';
  79. ctx.fillRoundRect(0, 0, w, h, 20);
  80. ctx.clip();
  81. ctx.fillRect(0, 0, w, h - 160);
  82. });
  83. // 绘制图片内容
  84. dp.draw(async (ctx) => {
  85. await Promise.all([
  86. ctx.drawImage(bgImg, 0, 0, w, h),
  87. ]);
  88. // 绘制用户二维码
  89. await ctx.drawQrCode(qrParams);
  90. });
  91. // 绘制底部文字内容
  92. dp.draw((ctx) => {
  93. ctx.fillStyle = titleParams.color;
  94. ctx.font = '34px PingFang SC';
  95. ctx.fillText(titleParams.text, titleParams.x, titleParams.y);
  96. });
  97. this.imgUrl = await dp.createImagePath();
  98. },
  99. methods: {
  100. savePhoto() {
  101. uni.saveImageToPhotosAlbum({
  102. filePath: this.imgUrl,
  103. success: function() {
  104. uni.showToast({
  105. title: "保存成功",
  106. icon: "none"
  107. });
  108. },
  109. fail: function() {
  110. uni.showToast({
  111. title: "保存失败,请稍后重试",
  112. icon: "none"
  113. });
  114. }
  115. });
  116. }
  117. }
  118. };
  119. </script>
  120. <style lang="scss">
  121. </style>