createOrderPoster.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="index">
  3. <mp-swiper operateText="批量保存图片" iconName="download" :list="imgList" @click="savePhoto"></mp-swiper>
  4. <view style="position: fixed; top: 999999999999999999999rpx">
  5. <!-- #ifdef MP-WEIXIN -->
  6. <canvas id="canvas" type="2d" style="width: 670rpx; height: 940rpx" />
  7. <!-- #endif -->
  8. <!-- #ifndef MP-WEIXIN -->
  9. <canvas canvas-id="canvas" id="canvas" style="width: 670rpx; height: 940rpx" />
  10. <!-- #endif -->
  11. </view>
  12. <u-modal :show-cancel-button="true" cancel-text="暂不生成" @cancel="$back" confirm-color="#FF9447"
  13. @confirm="modalConfirm" title="提示" v-model="modalShow">
  14. <view class="slot-content" style="margin: 30rpx;">
  15. <u-form label-width="150" ref="uForm">
  16. <u-form-item :border-bottom="false" label="座位数:">
  17. <u-input placeholder="请输入生成的座位数" type="number" v-model="count" />
  18. </u-form-item>
  19. </u-form>
  20. </view>
  21. </u-modal>
  22. </view>
  23. </template>
  24. <script>
  25. import mpSwiper from "../comps/mp-swiper.vue"
  26. import DrawPoster from "../comps/u-draw-poster/dist/index.js";
  27. import {
  28. drawQrCode
  29. } from "../comps/u-draw-poster/dist/index.js";
  30. DrawPoster.useCtx(drawQrCode);
  31. export default {
  32. components: {
  33. mpSwiper
  34. },
  35. data: () => ({
  36. modalShow: true,
  37. imgUrl: '',
  38. imgList: [],
  39. //生成海报数量
  40. count: '',
  41. //模板下标
  42. index: 1
  43. }),
  44. onLoad(options) {
  45. this.index = options.index
  46. },
  47. methods: {
  48. modalConfirm() {
  49. if (this.$isEmpty(this.count)) {
  50. this.$dialog.showModal('请输入生成座位的数量', false)
  51. this.modalShow = true
  52. return
  53. }
  54. this.createPoster()
  55. },
  56. async createPoster() {
  57. for (var i = 0; i < this.count; i++) {
  58. await this.doCreatePoster(i + 1)
  59. }
  60. },
  61. async doCreatePoster(number) {
  62. // 创建绘制工具
  63. const dp = await DrawPoster.build({
  64. selector: 'canvas',
  65. loading: true,
  66. debugging: true,
  67. });
  68. const w = (dp.canvas.width = 670);
  69. const h = (dp.canvas.height = 940);
  70. //初始化参数
  71. let bgImg = `/pagesPoster/static/poster/wuliao${this.index}.png`
  72. let shopDetail = this.$cache.get('selectedShop')
  73. let text = `${shopDetail.name}【${number}号座】`
  74. let params = {
  75. shopId: this.vuex_shopId,
  76. seatNum: `${number}号座`
  77. }
  78. let qrCodeText = this.$global.ORDER_QR_PATH + this.$u.queryParams(params)
  79. let titleParams = {
  80. x: w / 2 - (text.length / 2 * 34 - 10),
  81. y: h / 2 - 240,
  82. color: "#000",
  83. text: text
  84. }
  85. let qrParams = {
  86. x: w / 2 - 175,
  87. y: h / 2 - 200,
  88. text: qrCodeText,
  89. size: 350,
  90. margin: 5,
  91. }
  92. if (this.index == 2) {
  93. titleParams.y = h / 2 - 90
  94. qrParams.y = h / 2 - 60
  95. }
  96. if (this.index == 3) {
  97. titleParams.color = '#fff'
  98. titleParams.y = h / 2 - 150
  99. qrParams.y = h / 2 - 120
  100. }
  101. if (this.index == 4) {
  102. titleParams.y = h / 2 - 240
  103. qrParams.y = h / 2 - 210
  104. }
  105. // 绘制基本背景
  106. dp.draw((ctx) => {
  107. ctx.fillStyle = '#ffffff';
  108. ctx.fillRoundRect(0, 0, w, h, 20);
  109. ctx.clip();
  110. ctx.fillRect(0, 0, w, h - 160);
  111. });
  112. // 绘制图片内容
  113. dp.draw(async (ctx) => {
  114. await Promise.all([
  115. ctx.drawImage(bgImg, 0, 0, w, h),
  116. ]);
  117. // 用户二维码
  118. await ctx.drawQrCode(qrParams);
  119. });
  120. // 绘制底部文字内容
  121. dp.draw((ctx) => {
  122. ctx.fillStyle = titleParams.color;
  123. ctx.font = '34px PingFang SC';
  124. ctx.fillText(titleParams.text, titleParams.x, titleParams.y);
  125. });
  126. this.imgUrl = await dp.createImagePath();
  127. this.imgList.push(this.imgUrl)
  128. },
  129. async savePhoto() {
  130. this.$dialog.showLoading('图片保存中')
  131. for (var i = 0; i < this.imgList.length; i++) {
  132. await this.$mpi.saveImg(this.imgList[i])
  133. }
  134. uni.hideLoading()
  135. }
  136. }
  137. };
  138. </script>
  139. <style lang="scss">
  140. </style>