share.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="">
  3. <uni-popup ref="sharePopupRef" :maskClick="!imgURL" :key="maskBg" :maskBg="maskBg" @change="popupChange">
  4. <view class="share-content">
  5. <view class="close-winning-icon" @click.stop="onClose()">
  6. <image v-if="imgURL" src="@/static/Lottery/close.png" mode="aspectFit"></image>
  7. </view>
  8. <template v-if="showQrcode && codeurl">
  9. <uqrcode ref="uqrcode" :hide="true" canvas-id="qrcode" :options="options" :value="codeurl"
  10. :size="codeSize" @complete="getCode" />
  11. </template>
  12. <view class="canvas-box">
  13. <!-- :class="{'show-anvas':!showCanvas}" -->
  14. <canvas :class="{'show-anvas':!showCanvas}" canvas-id="canvasId" id="canvasId"></canvas>
  15. <!-- v-if="imgURL" -->
  16. <view class="share-btns" v-if="imgURL">
  17. <view class="share-btn seve-share" @click="saveShare()">保存到本地</view>
  18. <view class="share-btn share-user" @click="ShareWX()">分享好友</view>
  19. </view>
  20. </view>
  21. </view>
  22. </uni-popup>
  23. </view>
  24. </template>
  25. <script>
  26. import CONFIG from "@/config/global.config.js"
  27. import UQRCode from '@/uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';
  28. import {
  29. setShareRecord
  30. } from "@/api/Lottery.js"
  31. export default {
  32. props: {
  33. posterUrl: {
  34. type: String,
  35. default: null
  36. },
  37. activityId: {
  38. type: [String, Number],
  39. default: null
  40. },
  41. },
  42. data() {
  43. return {
  44. showQrcode: false,
  45. codeurl: null,
  46. codeSize: uni.upx2px(1000),
  47. options: {
  48. margin: uni.upx2px(30),
  49. useDynamicSize: true
  50. },
  51. codeImgUrl: null,
  52. imgURL: null,
  53. showCanvas: false,
  54. maskBg: 'rgba(0, 0, 0,.1)'
  55. }
  56. },
  57. created() {
  58. // console.log('111')
  59. },
  60. mounted() {
  61. // this.$refs.sharePopupRef.open()
  62. },
  63. methods: {
  64. open() {
  65. this.showCanvas = false;
  66. this.codeurl = `${CONFIG.hostUrl}?URLType=BigTurnplate&activityId=${this.activityId}`
  67. this.imgURL = '';
  68. this.maskBg = 'rgba(0, 0, 0,.1)';
  69. this.$refs.sharePopupRef.open()
  70. uni.showLoading({
  71. title: '分享海报生成中'
  72. });
  73. this.showQrcode = true;
  74. },
  75. onClose() {
  76. this.showQrcode = false;
  77. uni.hideLoading();
  78. this.$refs.sharePopupRef.close();
  79. },
  80. getCode(e) {
  81. this.$refs.uqrcode.toTempFilePath({
  82. success: res => {
  83. this.codeImgUrl = res.tempFilePath;
  84. this.init()
  85. }
  86. });
  87. },
  88. init() {
  89. let textStr = 0;
  90. let that = this;
  91. uni.getImageInfo({
  92. src: this.posterUrl,
  93. success: image => {
  94. var context = uni.createCanvasContext('canvasId');
  95. context.drawImage(image.path, 0, 0, uni.upx2px(630), uni.upx2px(1111));
  96. context.drawImage(that.codeImgUrl, uni.upx2px(231), uni.upx2px(698), uni.upx2px(166),
  97. uni.upx2px(166));
  98. context.draw(false, () => {
  99. // 返回canvas图片信息
  100. uni.canvasToTempFilePath({
  101. canvasId: 'canvasId',
  102. success: (res) => {
  103. that.imgURL = res.tempFilePath;
  104. // console.log('res.tempFilePath =', res.tempFilePath)
  105. // console.log('that.imgURL =', that.imgURL)
  106. uni.hideLoading();
  107. this.maskBg = 'rgba(0, 0, 0,.6)';
  108. this.showCanvas = true;
  109. // this.$refs.sharePopupRef.open()
  110. },
  111. fail: function(err) {
  112. // console.log(err)
  113. uni.hideLoading();
  114. uni.showToast({
  115. title: '生成海报失败',
  116. icon: 'none'
  117. })
  118. }
  119. })
  120. })
  121. },
  122. fail: err => {
  123. uni.hideLoading();
  124. uni.showToast({
  125. title: '生成海报失败',
  126. icon: 'none'
  127. })
  128. }
  129. })
  130. },
  131. popupChange(obj) {
  132. console.log('popupChange = = ', obj)
  133. if (!obj.show) {
  134. uni.hideLoading()
  135. }
  136. },
  137. saveShare() {
  138. var _this = this;
  139. uni.saveImageToPhotosAlbum({
  140. filePath: _this.imgURL,
  141. success() {
  142. uni.showModal({
  143. title: "保存成功",
  144. content: "图片已成功保存到相册",
  145. showCancel: false
  146. })
  147. setShareRecord({
  148. activityId: _this.activityId,
  149. shareMode: 0
  150. }).then(res => {
  151. console.log("success:1 图片已成功保存到相册");
  152. })
  153. },
  154. fail(e) {
  155. console.log(e)
  156. }
  157. })
  158. },
  159. ShareWX() {
  160. var _this = this;
  161. uni.share({
  162. provider: "weixin", //分享服务提供商(即weixin|qq|sinaweibo)
  163. scene: "WXSceneSession", //分享场景,此代表分享到聊天
  164. type: 2, //分享类型纯图片
  165. imageUrl: _this.imgURL, //分享图片链接,图片大小不要太大,不然会报错,推荐小于20kb
  166. success: function(res) {
  167. },
  168. fail: function(err) {
  169. // console.log("fail:" + JSON.stringify(err));
  170. }
  171. });
  172. setShareRecord({
  173. activityId: _this.activityId,
  174. shareMode: 1
  175. }).then(res => {})
  176. }
  177. },
  178. watch: {
  179. }
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. .poster-template {
  184. position: fixed;
  185. left: 1000000px;
  186. top: 1000000px;
  187. .share-code {
  188. width: 166rpx;
  189. height: 166rpx;
  190. }
  191. }
  192. .share-content {
  193. width: 630rpx;
  194. .close-winning-icon {
  195. width: 100%;
  196. height: calc(65rpx + 20rpx);
  197. // transform: translateX(-50%);
  198. image {
  199. display: block;
  200. margin: 0 auto;
  201. width: 65rpx;
  202. height: 65rpx;
  203. }
  204. }
  205. .canvas-box {
  206. position: relative;
  207. width: 630rpx;
  208. height: 1111rpx;
  209. .share-btns {
  210. position: absolute;
  211. left: 50%;
  212. bottom: 70rpx;
  213. transform: translateX(-50%);
  214. .share-btn {
  215. width: 345rpx;
  216. height: 60rpx;
  217. line-height: 60rpx;
  218. font-size: 28rpx;
  219. font-family: PingFang SC, PingFang SC-Regular;
  220. font-weight: 400;
  221. text-align: center;
  222. margin-top: 15rpx;
  223. }
  224. .seve-share {
  225. color: #721202;
  226. background: linear-gradient(180deg, #f7dea4, #d8a759);
  227. border-radius: 30rpx;
  228. }
  229. .share-user {
  230. color: #ffffff;
  231. background: linear-gradient(180deg, #3db8d3, #45c7d5);
  232. border-radius: 30rpx;
  233. }
  234. }
  235. // <view class="share-btn seve-share">保存到本地</view>
  236. // <view class="share-btn share-user">分享好友</view>
  237. }
  238. }
  239. #canvasId {
  240. width: 630rpx;
  241. height: 1111rpx;
  242. }
  243. .share-box {
  244. width: 630rpx;
  245. height: 1111rpx;
  246. image {
  247. width: 630rpx;
  248. height: 1111rpx;
  249. }
  250. }
  251. .show-anvas {
  252. margin-left: 200vw;
  253. }
  254. </style>