createPoster.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 borderImg='/static/icon/border.png'
  49. let shopDetail = this.$cache.get('selectedShop')
  50. let text=shopDetail.name
  51. let qrCodeText = this.$global.QR_PATH + this.$crypto.encrypt(this.vuex_shopId)
  52. console.log(text.length,"/************");
  53. let titleParams={
  54. x:w / 2 - (text.length/2 * 34),
  55. y:h / 2 - 240,
  56. color:"#000",
  57. text:text }
  58. let qrParams = {
  59. x: w / 2 - 175,
  60. y: h / 2 - 200,
  61. text: qrCodeText,
  62. size: 350,
  63. margin: 5,
  64. }
  65. if (index==2) {
  66. titleParams.y=h / 2 - 90
  67. qrParams.y=h / 2 - 60
  68. }
  69. if (index==3) {
  70. titleParams.color='#fff'
  71. titleParams.y=h / 2 - 150
  72. qrParams.y=h / 2 - 120
  73. }
  74. if (index==4) {
  75. titleParams.y=h / 2 - 240
  76. qrParams.y=h / 2 - 210
  77. }
  78. // 绘制基本背景
  79. dp.draw((ctx) => {
  80. ctx.fillStyle = '#ffffff';
  81. ctx.fillRoundRect(0, 0, w, h, 20);
  82. ctx.clip();
  83. ctx.fillRect(0, 0, w, h - 160);
  84. });
  85. // 绘制图片内容
  86. dp.draw(async (ctx) => {
  87. await Promise.all([
  88. ctx.drawImage(bgImg, 0, 0, w, h),
  89. ]);
  90. // 用户二维码
  91. await ctx.drawQrCode(qrParams);
  92. });
  93. // 绘制底部文字内容
  94. dp.draw((ctx) => {
  95. ctx.fillStyle = titleParams.color;
  96. ctx.font = '34px PingFang SC';
  97. ctx.fillText(titleParams.text, titleParams.x, titleParams.y);
  98. });
  99. this.imgUrl = await dp.createImagePath();
  100. },
  101. methods: {
  102. savePhoto() {
  103. uni.saveImageToPhotosAlbum({
  104. filePath: this.imgUrl,
  105. success: function() {
  106. uni.showToast({
  107. title: "保存成功",
  108. icon: "none"
  109. });
  110. },
  111. fail: function() {
  112. uni.showToast({
  113. title: "保存失败,请稍后重试",
  114. icon: "none"
  115. });
  116. }
  117. });
  118. }
  119. }
  120. };
  121. </script>
  122. <style lang="scss">
  123. </style>