GoodsShare.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view class="share-container">
  3. <uni-popup ref="popup" type="center">
  4. <view class="wrapper" v-if="popShow">
  5. <canvas canvas-id="shareCanvas" class="canvas" style="width: 250px;height:430px;"></canvas>
  6. <view class="bottom">
  7. <button class="bottom-btn" open-type="share">
  8. <image :src="static + 'wechat.png'"></image>
  9. <view class="text">分享好友</view>
  10. </button>
  11. <view class="bottom-btn" @click="saveImageToPhotos('shareCanvas')">
  12. <image :src="static + 'download.png'"></image>
  13. <view class="text">保存图片</view>
  14. </view>
  15. </view>
  16. </view>
  17. </uni-popup>
  18. </view>
  19. </template>
  20. <script lang="ts">
  21. // @ts-nocheck
  22. import {
  23. Component,
  24. Prop,
  25. Vue,
  26. Emit,
  27. Mixins
  28. } from 'vue-property-decorator';
  29. import {
  30. types
  31. } from '@/common/store';
  32. import {
  33. OSS_STATIC
  34. } from '@/common/constants';
  35. import canvasMixin from '@/common/mixins/canvasMixin.ts';
  36. import {
  37. namespace
  38. } from 'vuex-class';
  39. const canvasModule = namespace('canvas');
  40. const baseModule = namespace('base');
  41. @Component({})
  42. export default class ShopShare extends Mixins(canvasMixin) {
  43. @canvasModule.Getter('_canvasData') _canvasData: any;
  44. @baseModule.Getter('_userInfo') _userInfo: any;
  45. @baseModule.Getter('_goods') goodsInfo: any;
  46. popShow: boolean = false;
  47. static: string = OSS_STATIC;
  48. closePop(): void {
  49. (this.$refs.popup as any).close();
  50. this.popShow = false;
  51. }
  52. openPop(): void {
  53. (this.$refs.popup as any).open();
  54. this.popShow = true;
  55. uni.showLoading({
  56. title: '加载中'
  57. });
  58. this.drawPoster();
  59. }
  60. mounted() {}
  61. drawPoster(): void {
  62. // 绘制海报
  63. // 注意:page页面canvas-id获取ctx,组件需传this
  64. const that = this;
  65. const ctx = uni.createCanvasContext('shareCanvas', this);
  66. // 绘制白色底
  67. this.roundRect(ctx, 0, 0, 250, 460, '#ffffff', 8, 8, 8, 8);
  68. // 绘制头像
  69. this.drawCircular(ctx, 24, 24, 13, 15, that._canvasData.avatar);
  70. this.ellipsisText(
  71. ctx,
  72. that._userInfo.nickname,
  73. // '爱润妍',
  74. 200,
  75. 43,
  76. 13,
  77. '#999',
  78. '10px',
  79. 14
  80. );
  81. // 绘制宣传语
  82. this.ellipsisText(
  83. ctx,
  84. '推荐给你一个好物',
  85. 200,
  86. 43,
  87. 27,
  88. '#454545',
  89. '10px',
  90. 14
  91. );
  92. this.textSet(ctx, '”', '45px', 135, 20, '#ddd');
  93. // 绘制店铺图片
  94. this.drawImgInfo(
  95. ctx,
  96. 10,
  97. 50,
  98. 230,
  99. 230,
  100. that.goodsInfo.goodImg,
  101. that.goodsInfo.width,
  102. that.goodsInfo.height
  103. );
  104. // 商品标题
  105. this.ellipsisText(
  106. ctx,
  107. that.goodsInfo.name,
  108. 180,
  109. 11,
  110. 291,
  111. '#000',
  112. '14px',
  113. 20,
  114. 1,
  115. 'bold '
  116. );
  117. // 商品副标题
  118. this.ellipsisText(
  119. ctx,
  120. that.goodsInfo.tagline,
  121. 150,
  122. 11,
  123. 311,
  124. '#999',
  125. '10px',
  126. 14,
  127. 1
  128. );
  129. // 划线价
  130. // let linePrice:string = '¥0.00';
  131. // if (that.goodsInfo.linePrice) {
  132. // linePrice = '¥' + Number(that.goodsInfo.linePrice).toFixed(2);
  133. // }
  134. // this.textSet(ctx, linePrice, '12px', 11, 340, '#999');
  135. // let lineH = ctx.measureText(linePrice).width;
  136. // ctx.moveTo(14, 347);
  137. // ctx.lineTo(lineH + 20, 347);
  138. // ctx.strokeStyle = '#999999';
  139. // ctx.stroke();
  140. // 商品价格
  141. this.textSet(ctx, '¥', '12px', 11, 362, '#000', 'bold ');
  142. this.textSet(
  143. ctx,
  144. Number(that.goodsInfo.minPrice).toFixed(2),
  145. '19px',
  146. 19,
  147. 356,
  148. '#000',
  149. 'bold '
  150. );
  151. // // 小程序码
  152. ctx.drawImage(that._canvasData.goodQr, 150, 295, 90, 90);
  153. this.textSet(ctx, '长按识别进店铺', '10px', 90, 400, '#999999');
  154. ctx.draw(true, () => {
  155. console.log('draw,true');
  156. uni.hideLoading();
  157. });
  158. }
  159. }
  160. </script>
  161. <style lang="scss" scoped>
  162. .wrapper {
  163. @include flex-y();
  164. width: 250px;
  165. }
  166. .canvas {
  167. border-radius: vw(5);
  168. overflow: hidden;
  169. margin-top: vw(50);
  170. }
  171. .bottom {
  172. width: 230px;
  173. margin-top: vw(15);
  174. @include flex-x();
  175. .text {
  176. @include word-vw(14, white);
  177. line-height: vw(20);
  178. margin-top: vw(5);
  179. }
  180. image {
  181. width: vw(60);
  182. height: vw(60);
  183. }
  184. }
  185. button {
  186. background: none;
  187. line-height: 1;
  188. padding: 0;
  189. margin: 0;
  190. }
  191. button::after {
  192. border: 0px;
  193. }
  194. .button-hover {
  195. background: none;
  196. }
  197. </style>