share - 副本.vue 5.6 KB

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