poster.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view>
  3. <view></view>
  4. <tui-loading v-if="loadding" text="海报生成中"></tui-loading>
  5. <canvas :style="{ width: winWidth + 'px', height: winHeight + 'px' }" canvas-id="posterId" id="posterId"
  6. class="tui-poster__canvas"></canvas>
  7. <view class="tui-poster__box">
  8. <image :src="posterImg" v-if="posterImg" class="tui-poster__img"></image>
  9. <tui-button v-if="buttonshow" class="tui-poster__button" type="danger" width="460rpx" height="80rpx"
  10. shape="circle" @click="savePic">保存图片
  11. </tui-button>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. const poster = require('@/components/common/tui-poster/tui-poster.js');
  17. export default {
  18. components: {
  19. },
  20. data() {
  21. return {
  22. loadding: true,
  23. buttonshow: false,
  24. posterImg: '',
  25. winWidth: uni.upx2px(690 * 2),
  26. winHeight: uni.upx2px(1063 * 2),
  27. }
  28. },
  29. onLoad: function() {
  30. },
  31. onShow() {
  32. var thas = this
  33. uni.getStorage({
  34. key: 'userinfo',
  35. success: function(res) {
  36. thas.userinfo = res.data;
  37. }
  38. })
  39. thas.createPoster()
  40. },
  41. methods: {
  42. async createPoster() {
  43. const _this = this;
  44. if (this.posterImg) {
  45. this.modalShow = true;
  46. return;
  47. }
  48. uni.showLoading({
  49. mask: true,
  50. title: '图片生成中...'
  51. });
  52. var posterdata = await _this.$request.get('agent.poster', {
  53. samkey: (new Date()).valueOf()
  54. });
  55. let mainPic = await poster.getImage(posterdata.data.poster);
  56. let qrcode = await poster.getImage(posterdata.data.qrcode);
  57. // #ifdef MP-WEIXIN
  58. await poster.removeSavedFile();
  59. // #endif
  60. if (mainPic && qrcode) {
  61. const imgs = {
  62. mainPic: mainPic,
  63. qrcode: qrcode
  64. };
  65. let text = posterdata.data.nickName;
  66. let name = posterdata.data.sitename;
  67. poster.drawPoster('posterId', this.winWidth, this.winHeight, imgs, text,
  68. name,
  69. res => {
  70. uni.hideLoading();
  71. this.loadding = false;
  72. this.buttonshow = true;
  73. if (res) {
  74. this.posterImg = res;
  75. setTimeout(() => {
  76. this.modalShow = true;
  77. }, 60);
  78. } else {
  79. this.tui.toast('生成海报失败,请稍后再试');
  80. }
  81. });
  82. } else {
  83. uni.hideLoading();
  84. this.tui.toast('生成海报图片下载失败,请稍后再试');
  85. }
  86. },
  87. savePic() {
  88. if (this.posterImg) {
  89. // #ifdef H5
  90. uni.previewImage({
  91. urls: [this.posterImg]
  92. });
  93. // #endif
  94. // #ifndef H5
  95. poster.saveImage(this.posterImg);
  96. // #endif
  97. }
  98. }
  99. },
  100. /**
  101. * 页面相关事件处理函数--监听用户下拉动作
  102. */
  103. onPullDownRefresh: function() {
  104. setTimeout(() => {
  105. uni.stopPullDownRefresh()
  106. }, 200);
  107. },
  108. }
  109. </script>
  110. <style>
  111. image {
  112. width: 100%;
  113. min-height: 1163rpx;
  114. display: block;
  115. margin: 0 auto;
  116. }
  117. .tui-poster__canvas {
  118. background-color: #fff;
  119. position: absolute;
  120. top: -9999px;
  121. }
  122. .tui-poster__box {
  123. width: 100%;
  124. position: relative;
  125. display: flex;
  126. justify-content: center;
  127. align-items: center;
  128. flex-direction: column;
  129. }
  130. .tui-poster__img {
  131. width: 99%;
  132. }
  133. .tui-poster__button {
  134. margin-top: 40rpx;
  135. }
  136. </style>