小程序端工程代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.5 KiB

  1. <template>
  2. <view v-if="posterImageStatus" class="poster-first">
  3. <div class="posterCanvasWarp">
  4. <canvas class="posterCanvas" canvas-id="myCanvas"></canvas>
  5. </div>
  6. <!-- <view class="poster-pop" v-show="!canvasStatus">
  7. <image
  8. :src="`${$VUE_APP_RESOURCES_URL}/images/poster-close.png`"
  9. class="close"
  10. @click="posterImageClose"
  11. />
  12. <view class="canvas" ref="poster">
  13. <image class="image" :src="posterData.image" alt="商品图片" />
  14. <view class="text black">
  15. <text v-text="posterData.title"></text>
  16. </view>
  17. <view class="text rad">
  18. <text v-text="'¥' + posterData.price"></text>
  19. </view>
  20. <view class="code">
  21. <view class="code-img">
  22. <image :src="posterData.code" show-menu-by-longpress mode="widthFix" alt="二维码" />
  23. </view>
  24. <view class="code-text">
  25. <text>长按识别二维码 立即购买</text>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="save-poster" @click="savePosterPath">生成图片</view>
  30. </view>-->
  31. <view class="poster-pop" v-show="canvasStatus">
  32. <img :src="`${$VUE_APP_RESOURCES_URL}/images/poster-close.png`" class="close" @click="posterImageClose" mode="widthFix" />
  33. <image :src="posterImage" alt="tp" class="poster-image" show-menu-by-longpress mode="widthFix" />
  34. <view class="save-poster" @click="saveImg">保存海报</view>
  35. </view>
  36. <view class="mask"></view>
  37. </view>
  38. </template>
  39. <script>
  40. // import html2canvas from "html2canvas";
  41. import { PosterCanvas } from '@/utils'
  42. import { getProductPoster } from '@/api/store'
  43. export default {
  44. name: 'StorePoster',
  45. props: {
  46. posterImageStatus: Boolean,
  47. posterData: Object,
  48. goodId: String,
  49. },
  50. data: function() {
  51. return {
  52. canvasStatus: false,
  53. posterImage: '',
  54. }
  55. },
  56. watch: {
  57. posterImageStatus: function() {
  58. var that = this
  59. if (that.posterImageStatus === true) {
  60. that.$nextTick(function() {
  61. that.savePosterPath()
  62. })
  63. }
  64. },
  65. },
  66. mounted: function() {},
  67. methods: {
  68. posterImageClose: function() {
  69. this.posterImageStatus = false
  70. this.canvasStatus = false
  71. this.$emit('setPosterImageStatus')
  72. },
  73. saveImg: function() {
  74. this.downloadFile(this.posterImage)
  75. },
  76. downloadFile(url) {
  77. uni.downloadFile({
  78. url,
  79. fail: function(res) {
  80. console.log(res)
  81. uni.showModal({
  82. title: '提示',
  83. content: '保存失败',
  84. })
  85. },
  86. success: function(res) {
  87. console.log(res)
  88. uni.showModal({
  89. title: '提示',
  90. content: '保存成功',
  91. })
  92. },
  93. })
  94. },
  95. savePosterPath: function() {
  96. const that = this
  97. uni.showLoading({
  98. title: '海报生成中',
  99. mask: true,
  100. })
  101. getProductPoster(this.goodId, {
  102. from: this.$deviceType == 'weixin' || this.$deviceType == 'weixinh5' ? 'uniappH5' : this.$deviceType,
  103. })
  104. .then(res => {
  105. this.canvasStatus = true
  106. this.posterImage = res.data
  107. })
  108. .finally(() => {
  109. uni.hideLoading()
  110. })
  111. // return;
  112. // //清空图片重新生成
  113. // that.posterImage = "";
  114. // uni.showLoading({ title: "海报生成中", mask: true });
  115. // console.log(this);
  116. // var prodId = that.$yrouter.currentRoute.query.id;
  117. // uni.downloadFile({
  118. // url:
  119. // this.$VUE_APP_API_URL +
  120. // "/shareImg/" +
  121. // prodId +
  122. // "?shareImgName=" +
  123. // this.posterData.code,
  124. // fail: function(res) {},
  125. // success: function(res) {
  126. // that.canvasStatus = true;
  127. // that.posterImage = res.tempFilePath;
  128. // uni.hideLoading();
  129. // }
  130. // });
  131. },
  132. },
  133. }
  134. </script>
  135. <style scoped lang="less" lang="less">
  136. .poster-first {
  137. overscroll-behavior: contain;
  138. }
  139. .poster-pop {
  140. width: 4.5 * 100rpx;
  141. height: 8 * 100rpx;
  142. position: fixed;
  143. left: 50%;
  144. transform: translateX(-50%);
  145. z-index: 99;
  146. top: 50%;
  147. margin-top: -4.6 * 100rpx;
  148. }
  149. .poster-pop .canvas {
  150. background-color: #ffffff;
  151. height: 8 * 100rpx;
  152. }
  153. .poster-pop .poster-image {
  154. width: 100%;
  155. height: auto;
  156. }
  157. .poster-pop .canvas .image {
  158. width: 4.5 * 100rpx;
  159. height: 4.5 * 100rpx;
  160. display: block;
  161. }
  162. .poster-pop .canvas .text {
  163. text-align: center;
  164. color: #000000;
  165. margin-top: 0.32 * 100rpx;
  166. }
  167. .poster-pop .canvas .text.black {
  168. height: 0.68 * 100rpx;
  169. }
  170. .poster-pop .canvas .text.rad {
  171. color: #ff0000;
  172. }
  173. .poster-pop .canvas .code {
  174. height: 1.4 * 100rpx;
  175. display: flex;
  176. }
  177. .poster-pop .canvas .code .code-img {
  178. width: 33%;
  179. padding: 0.06 * 100rpx;
  180. }
  181. .poster-pop .canvas .code .code-img image {
  182. width: 100%;
  183. }
  184. .poster-pop .canvas .code .code-text {
  185. width: 60%;
  186. font-size: 0.12 * 100rpx;
  187. line-height: 1.64 * 100rpx;
  188. }
  189. .poster-pop .close {
  190. width: 0.46 * 100rpx;
  191. height: 0.75 * 100rpx;
  192. position: fixed;
  193. right: 0;
  194. top: -0.73 * 100rpx;
  195. display: block;
  196. }
  197. .poster-pop .save-poster {
  198. background-color: #df2d0a;
  199. font-size: 0.22 * 100rpx;
  200. color: #fff;
  201. text-align: center;
  202. height: 0.76 * 100rpx;
  203. line-height: 0.76 * 100rpx;
  204. width: 100%;
  205. margin-top: -0.1 * 100rpx;
  206. border-radius: 0 0 10rpx 10rpx;
  207. }
  208. .poster-pop .keep {
  209. color: #fff;
  210. text-align: center;
  211. font-size: 0.25 * 100rpx;
  212. margin-top: 0.1 * 100rpx;
  213. }
  214. .mask {
  215. position: fixed;
  216. top: 0;
  217. left: 0;
  218. right: 0;
  219. bottom: 0;
  220. background-color: rgba(0, 0, 0, 0.6);
  221. z-index: 9;
  222. }
  223. </style>