poster-detail.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="pc-container" @longpress="saveImage">
  3. <!-- 公共组件-每个页面必须引入 -->
  4. <public-module></public-module>
  5. <image :src="imgurl" mode="widthFix" v-show="!canvasShow"></image>
  6. <canvas id="mycanvas" ref="mycanvas" canvas-id="mycanvas" style="width: 690rpx;height: 1246rpx;"></canvas>
  7. </view>
  8. </template>
  9. <script>
  10. import {
  11. mapState,
  12. mapMutations
  13. } from "vuex"
  14. import QR from "@/common/wxqrcode.js"
  15. export default {
  16. data() {
  17. return {
  18. imgurl: '',
  19. canvasShow: true,
  20. posterimg: "",
  21. recommendImg: "",
  22. }
  23. },
  24. onLoad(params) {
  25. var that = this;
  26. if (!!params.img) {
  27. this.posterimg = params.img;
  28. } else {
  29. this.posterimg = "/static/image/poster/baoxian/poster1.jpg";
  30. }
  31. var id = that.userInfo.sysUser.id;
  32. this.recommendImg = QR.createQrCodeImg(this.$base.h5BaseUrl + "/#/pages/register/register?id=" + id, {
  33. size: parseInt(220) //二维码大小
  34. })
  35. },
  36. computed: {
  37. ...mapState(["userInfo"]),
  38. },
  39. mounted() {
  40. this.canvasImage();
  41. },
  42. methods: {
  43. canvasImage() {
  44. let myCanvas = uni.createCanvasContext('mycanvas', this);
  45. var ww = uni.upx2px("690");
  46. var hh = uni.upx2px("1246");
  47. //画布尺寸
  48. // 坐标(0,0) 表示从此处开始绘制,相当于偏移。
  49. //背景
  50. myCanvas.drawImage(this.posterimg, 0, 0, ww, hh);
  51. //头像
  52. // myCanvas.drawImage(this.avatar, ww - hh / 2, hh - ww / 3.8, ww / 8, ww / 8);
  53. //个人信息
  54. let fontSize1 = uni.upx2px(32);
  55. myCanvas.setFillStyle('#fff') //文字样式
  56. myCanvas.font = `${fontSize1}px Arial`; //绘制文字
  57. myCanvas.fillText(this.userInfo.sysUser.name, ww / 10 + ww / 5, hh - ww / 50 * 11);
  58. myCanvas.font = `${fontSize1}px Arial`; //绘制文字
  59. myCanvas.fillText(this.userInfo.sysUser.mobile, ww / 10 + ww / 5, hh - ww / 48 * 7);
  60. //二维码
  61. myCanvas.drawImage(this.recommendImg, ww - ww / 3, hh - ww / 3, ww / 4, ww / 4);
  62. //开始绘画,必须调用这一步,才会把之前的一些操作实施
  63. myCanvas.draw(true, () => {
  64. uni.hideLoading();
  65. uni.canvasToTempFilePath({
  66. canvasId: 'mycanvas',
  67. success: (res) => {
  68. // 在H5平台下,tempFilePath 为 base64
  69. this.imgurl = res.tempFilePath;
  70. this.canvasShow = false;
  71. },
  72. fail: () => {
  73. uni.showToast({
  74. title: '专属海报加载失败',
  75. duration: 2000
  76. });
  77. }
  78. });
  79. });
  80. },
  81. // 保存海报
  82. saveImage() {
  83. let tabList = [];
  84. // #ifdef APP-PLUS
  85. tabList = ['保存图片', '分享到微信'];
  86. // #endif
  87. // #ifndef APP-PLUS
  88. tabList = ['保存图片'];
  89. // #endif
  90. uni.showActionSheet({
  91. itemList: tabList,
  92. success: (res) => {
  93. if (res.tapIndex == 0) {
  94. uni.saveImageToPhotosAlbum({
  95. filePath: this.imgurl, // 图片文件路径,可以是临时文件路径也可以是永久文件路径,不支持网络图片路径
  96. success: () => {
  97. uni.showToast({
  98. title: '保存成功',
  99. duration: 2000
  100. });
  101. },
  102. fail: () => {
  103. uni.showToast({
  104. title: '保存失败',
  105. duration: 2000
  106. });
  107. }
  108. });
  109. } else if (res.tapIndex == 1) {
  110. uni.share({
  111. provider: "weixin",
  112. scene: "WXSceneSession",
  113. type: 2,
  114. imageUrl: this.imgurl
  115. })
  116. }
  117. },
  118. fail: function(res) {
  119. console.log(res.errMsg);
  120. }
  121. });
  122. }
  123. }
  124. };
  125. </script>
  126. <style>
  127. image {
  128. width: 100%;
  129. height: 1246rpx;
  130. }
  131. .pc-container {
  132. width: 690rpx;
  133. margin: 0 auto;
  134. padding-top: 80rpx;
  135. overflow: hidden;
  136. }
  137. #mycanvas {
  138. position: absolute;
  139. top: -10000px;
  140. left: -10000px;
  141. }
  142. </style>