poster-detail.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(['avatar', "userInfo"]),
  38. },
  39. mounted() {
  40. this.canvasImage();
  41. // uni.setNavigationBarTitle({
  42. // title: "新的标题"
  43. // })
  44. },
  45. methods: {
  46. canvasImage() {
  47. let myCanvas = uni.createCanvasContext('mycanvas', this);
  48. var ww = uni.upx2px("690");
  49. var hh = uni.upx2px("1246");
  50. //画布尺寸
  51. // 坐标(0,0) 表示从此处开始绘制,相当于偏移。
  52. //背景
  53. myCanvas.drawImage(this.posterimg, 0, 0, ww, hh);
  54. //头像
  55. myCanvas.drawImage(this.avatar, ww / 10, hh - ww / 40 * 9, ww / 0 * 7, ww / 7);
  56. //个人信息
  57. let fontSize1 = uni.upx2px(32);
  58. myCanvas.setFillStyle('#fff') //文字样式
  59. myCanvas.font = `${fontSize1}px Arial`; //绘制文字
  60. myCanvas.fillText(this.userInfo.sysUser.name, ww / 10 + ww / 5, hh - ww / 50 * 11);
  61. myCanvas.font = `${fontSize1}px Arial`; //绘制文字
  62. myCanvas.fillText(this.userInfo.sysUser.mobile, ww / 10 + ww / 5, hh - ww / 48 * 7);
  63. //二维码
  64. myCanvas.drawImage(this.recommendImg, ww - ww / 3, hh - ww / 3, ww / 4, ww / 4);
  65. //开始绘画,必须调用这一步,才会把之前的一些操作实施
  66. myCanvas.draw(true, () => {
  67. uni.hideLoading();
  68. uni.canvasToTempFilePath({
  69. canvasId: 'mycanvas',
  70. success: (res) => {
  71. // 在H5平台下,tempFilePath 为 base64
  72. this.imgurl = res.tempFilePath;
  73. this.canvasShow = false;
  74. },
  75. fail: () => {
  76. uni.showToast({
  77. title: '专属海报加载失败',
  78. duration: 2000
  79. });
  80. }
  81. });
  82. });
  83. },
  84. // 保存海报
  85. saveImage() {
  86. let tabList = [];
  87. // #ifdef APP-PLUS
  88. tabList = ['保存图片', '分享到微信'];
  89. // #endif
  90. // #ifndef APP-PLUS
  91. tabList = ['保存图片'];
  92. // #endif
  93. uni.showActionSheet({
  94. itemList: tabList,
  95. success: (res) => {
  96. if (res.tapIndex == 0) {
  97. uni.saveImageToPhotosAlbum({
  98. filePath: this.imgurl, // 图片文件路径,可以是临时文件路径也可以是永久文件路径,不支持网络图片路径
  99. success: () => {
  100. uni.showToast({
  101. title: '保存成功',
  102. duration: 2000
  103. });
  104. },
  105. fail: () => {
  106. uni.showToast({
  107. title: '保存失败',
  108. duration: 2000
  109. });
  110. }
  111. });
  112. } else if (res.tapIndex == 1) {
  113. uni.share({
  114. provider: "weixin",
  115. scene: "WXSceneSession",
  116. type: 2,
  117. imageUrl: this.imgurl
  118. })
  119. }
  120. },
  121. fail: function(res) {
  122. console.log(res.errMsg);
  123. }
  124. });
  125. }
  126. }
  127. };
  128. </script>
  129. <style>
  130. image {
  131. width: 100%;
  132. height: 1246rpx;
  133. }
  134. .pc-container {
  135. width: 690rpx;
  136. margin: 0 auto;
  137. padding-top: 80rpx;
  138. overflow: hidden;
  139. }
  140. #mycanvas {
  141. position: absolute;
  142. top: -10000px;
  143. left: -10000px;
  144. }
  145. </style>