poster-detail.vue 4.0 KB

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