sharePoster.vue 6.5 KB

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