customPromotionCode.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <u-popup
  3. :show="show"
  4. mode="center"
  5. @close="close"
  6. @open="open"
  7. bgColor="rgba(0,0,0,0)"
  8. >
  9. <view class="promotion-code">
  10. <!-- #ifdef MP -->
  11. <view class="promotion-code-info">
  12. <canvas canvas-id="canvas" class="canvas"></canvas>
  13. </view>
  14. <!-- #endif -->
  15. <!-- #ifdef H5 -->
  16. <view class="promotion-code-info">
  17. <canvas canvas-id="canvas" class="canvas"></canvas>
  18. </view>
  19. <!-- #endif -->
  20. <view class="promotion-code-but" @click="saveImage()">
  21. 保存至相册
  22. </view>
  23. </view>
  24. </u-popup>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. show: false,
  31. shareShow:false,
  32. posterShow:false,
  33. posterSrc:'',
  34. posterSrcType:null,
  35. performInfo:{
  36. posterImg: 'https://img1.baidu.com/it/u=3302184040,3713353210&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1703178000&t=3902211e35b62703882329c63cf27129',//'../../static/customPromotionCode/bgm1.png', // 剧目背景
  37. appletQrcode: 'https://t7.baidu.com/it/u=1127963623,72784497&fm=218&app=126&size=f242,150&n=0&f=JPEG&fmt=auto?s=361C1DC71A5A34D6877C593B0300C002&sec=1703178000&t=cc1d7c4413380f1e8769bae6ac5a66af', // '../../static/customPromotionCode/bgm2.png', // 二维码
  38. },//节目详情
  39. // firstGet:0,
  40. }
  41. },
  42. methods: {
  43. initData(data) {
  44. console.log(data)
  45. this.show = true
  46. this.getPoster()
  47. },
  48. open() {
  49. // console.log('open');
  50. },
  51. close() {
  52. this.show = false
  53. // console.log('close');
  54. },
  55. // 海报相关开始
  56. getPoster(item) {
  57. this.posterShow = true;
  58. this.shareShow = false;
  59. //前端生成海报
  60. uni.showLoading({
  61. title: '生成海报中'
  62. });
  63. // 前端生成海报开始
  64. let that = this;
  65. const ctx = uni.createCanvasContext('canvas', this);
  66. // 加载海报背景图
  67. uni.getImageInfo({
  68. src: this.performInfo.posterImg,
  69. success: res1 => {
  70. console.log("res1===",res1)
  71. const img1 = res1.path;
  72. // 加载海报二维码
  73. uni.getImageInfo({
  74. src: this.performInfo.appletQrcode,
  75. success: res2 => {
  76. const img2 = res2.path;
  77. // 绘制海报背景图
  78. ctx.drawImage(img1, 0, 0, 544, 988);
  79. // 绘制海报二维码
  80. ctx.drawImage(img2, 10, 400, 100, 600);
  81. // 绘制完成后导出图片并显示
  82. ctx.draw(false, () => {
  83. uni.canvasToTempFilePath({
  84. canvasId: 'canvas',
  85. success: res3 => {
  86. this.posterSrc = res3.tempFilePath;
  87. this.posterSrcType = 'local';
  88. },
  89. fail: err => {
  90. console.error(err);
  91. },
  92. }, this);
  93. });
  94. },
  95. fail: err2 => {
  96. console.error(err2);
  97. },
  98. });
  99. },
  100. fail: err1 => {
  101. console.error("err1===",err1);
  102. },
  103. complete: () => {
  104. uni.hideLoading()
  105. }
  106. });
  107. // 前端生成海报结束
  108. },
  109. saveImage() {
  110. let that = this;
  111. uni.showLoading({
  112. title: '保存中'
  113. });
  114. // if (this.posterSrcType == 'local') {
  115. // let params = {
  116. // tempFilePath: that.posterSrc
  117. // }
  118. // that.saveImageToPhotosAlbum(params);
  119. // return
  120. // }
  121. uni.downloadFile({
  122. url: this.posterSrc,
  123. success(res) {
  124. if (res.statusCode === 200) {
  125. // #ifdef MP
  126. that.saveImageToPhotosAlbum(res)
  127. // #endif
  128. // #ifdef H5
  129. uni.showToast({
  130. title: '下载成功',
  131. icon: 'none'
  132. });
  133. // #endif
  134. } else {
  135. uni.showToast({
  136. title: '下载图片失败',
  137. icon: 'none'
  138. });
  139. }
  140. },
  141. fail(e) {
  142. console.log('下载图片失败', e);
  143. console.log('posterSrc', that.posterSrc);
  144. uni.showToast({
  145. title: '下载图片失败',
  146. icon: 'none'
  147. });
  148. }
  149. });
  150. },
  151. saveImageToPhotosAlbum(res) {
  152. let that = this;
  153. uni.saveImageToPhotosAlbum({
  154. filePath: res.tempFilePath,
  155. success() {
  156. uni.showToast({
  157. title: '保存到相册成功',
  158. icon: 'success'
  159. });
  160. },
  161. fail(err) {
  162. console.log('保存图片失败', err);
  163. if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
  164. uni.getSetting({
  165. success(res) {
  166. if (!res.authSetting['scope.writePhotosAlbum']) {
  167. uni.showModal({
  168. title: '提示',
  169. content: '您还没有授权访问相册,请前往设置页面打开权限。',
  170. confirmText: '去设置',
  171. success(res) {
  172. if (res.confirm) {
  173. uni.openSetting();
  174. }
  175. }
  176. });
  177. } else {
  178. uni.showToast({
  179. title: '保存图片失败',
  180. icon: 'none'
  181. });
  182. }
  183. }
  184. });
  185. } else {
  186. uni.showToast({
  187. title: '保存图片失败',
  188. icon: 'none'
  189. });
  190. }
  191. },
  192. complete() {
  193. uni.hideLoading();
  194. that.posterShow = false;
  195. }
  196. });
  197. },
  198. }
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. .promotion-code {
  203. display: flex;
  204. flex-direction: column;
  205. align-items: center;
  206. .promotion-code-info {
  207. width: 544rpx;
  208. height: 988rpx;
  209. flex-shrink: 0;
  210. .canvas {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. }
  215. .promotion-code-but {
  216. margin-top: 40rpx;
  217. width: 422rpx;
  218. height: 80rpx;
  219. background: var(--gd-but-color);
  220. border-radius: 40rpx;
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. color: #fff;
  225. font-size: 28rpx;
  226. font-family: SourceHanSansCN, SourceHanSansCN;
  227. font-weight: 500;
  228. }
  229. }
  230. </style>