customPromotionCode.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 v-if="!posterSrc" canvas-id="canvas" class="canvas promotion-canvas"></canvas>
  18. <image class="promotion-code-info-img" mode="scaleToFill" v-else :src="posterSrc"></image>
  19. </view>
  20. <!-- #endif -->
  21. <view class="promotion-code-but" @click="saveImage()">
  22. 保存至相册
  23. </view>
  24. <view v-if="!posterShow" class="promotion-code-clear" @click="show=false">
  25. <u-icon name="close-circle-fill" color="var(--gd-bgm-color)" size="28"></u-icon>
  26. </view>
  27. </view>
  28. </u-popup>
  29. </template>
  30. <script>
  31. import { nextTick } from "vue"
  32. export default {
  33. data() {
  34. return {
  35. show: false,
  36. shareShow:false,
  37. posterShow:false,
  38. posterSrc:'',
  39. posterSrcType:null,
  40. performInfo:{
  41. 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', // 剧目背景
  42. 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', // 二维码
  43. },//节目详情
  44. // firstGet:0,
  45. }
  46. },
  47. methods: {
  48. initData(data) {
  49. console.log(data)
  50. this.show = true
  51. this.performInfo.posterImg = data.posterImg
  52. this.performInfo.appletQrcode = data.url
  53. this.posterSrc = ''
  54. this.$nextTick(()=>{
  55. this.getCanvasInfo()
  56. })
  57. },
  58. open() {
  59. // console.log('open');
  60. },
  61. close() {
  62. this.show = false
  63. // console.log('close');
  64. },
  65. getCanvasInfo(){
  66. let that = this
  67. //.box获取class为box的元素,如果使用的id= 'box' 则使用'#box'
  68. const query = uni.createSelectorQuery();
  69. query.select('.promotion-canvas').boundingClientRect(data => {
  70. that.getPoster(data.width,data.height)
  71. }).exec();
  72. },
  73. // 海报相关开始
  74. getPoster(w,h) {
  75. console.log(w,h)
  76. this.posterShow = true;
  77. this.shareShow = false;
  78. //前端生成海报
  79. uni.showLoading({
  80. title: '生成海报中'
  81. });
  82. // 前端生成海报开始
  83. let that = this;
  84. const ctx = uni.createCanvasContext('canvas', this);
  85. // 加载海报背景图
  86. uni.getImageInfo({
  87. src: this.performInfo.posterImg,
  88. success: res1 => {
  89. console.log("res1===",res1)
  90. const img1 = res1.path;
  91. // 加载海报二维码
  92. uni.getImageInfo({
  93. src: this.performInfo.appletQrcode,
  94. success: res2 => {
  95. console.log("res2===",res2)
  96. const img2 = res2.path;
  97. // 绘制海报背景图
  98. ctx.drawImage(img1, 0, 0, w, h);
  99. // 绘制海报二维码
  100. ctx.drawImage(img2, w/2-40, h/2+40, 70, 70);
  101. // 绘制完成后导出图片并显示
  102. ctx.draw(false, () => {
  103. uni.canvasToTempFilePath({
  104. canvasId: 'canvas',
  105. success: res3 => {
  106. this.posterShow = false
  107. this.posterSrc = res3.tempFilePath;
  108. this.posterSrcType = 'local';
  109. },
  110. fail: err => {
  111. this.posterShow = false
  112. console.error(err);
  113. },
  114. }, this);
  115. });
  116. },
  117. fail: err2 => {
  118. this.posterShow = false
  119. console.error(err2);
  120. },
  121. });
  122. },
  123. fail: err1 => {
  124. this.posterShow = false
  125. console.error("err1===",err1);
  126. },
  127. complete: () => {
  128. this.posterShow = false
  129. uni.hideLoading()
  130. }
  131. });
  132. // 前端生成海报结束
  133. },
  134. saveImage() {
  135. let that = this;
  136. uni.showLoading({
  137. title: '保存中'
  138. });
  139. // if (this.posterSrcType == 'local') {
  140. // let params = {
  141. // tempFilePath: that.posterSrc
  142. // }
  143. // that.saveImageToPhotosAlbum(params);
  144. // return
  145. // }
  146. uni.downloadFile({
  147. url: this.posterSrc,
  148. success(res) {
  149. if (res.statusCode === 200) {
  150. const systemInfo = uni.getSystemInfoSync(); // 调用 getSystemInfoSync() 函数获取系统信息
  151. console.log(systemInfo); // 打印输出系统信息对象
  152. if (systemInfo.ua.indexOf('MicroMessenger') != -1 ) {
  153. uni.showToast({
  154. title: "微信浏览器不可下载,可长按图片保存",
  155. icon: 'none',
  156. duration: 4000
  157. })
  158. return
  159. }
  160. // #ifdef MP
  161. that.saveImageToPhotosAlbum(res)
  162. // #endif
  163. // #ifdef H5
  164. var alink = document.createElement("a");
  165. alink.href = res.tempFilePath;
  166. alink.download = "pic"; //图片名
  167. alink.click();
  168. uni.showToast({
  169. title: '下载成功',
  170. icon: 'none'
  171. });
  172. // #endif
  173. } else {
  174. uni.showToast({
  175. title: '下载图片失败',
  176. icon: 'none'
  177. });
  178. }
  179. },
  180. fail(e) {
  181. console.log('下载图片失败', e);
  182. uni.hideLoading();
  183. uni.showToast({
  184. title: '下载图片失败',
  185. icon: 'none'
  186. });
  187. }
  188. });
  189. },
  190. saveImageToPhotosAlbum(res) {
  191. let that = this;
  192. uni.saveImageToPhotosAlbum({
  193. filePath: res.tempFilePath,
  194. success() {
  195. uni.showToast({
  196. title: '保存到相册成功',
  197. icon: 'success'
  198. });
  199. },
  200. fail(err) {
  201. console.log('保存图片失败', err);
  202. if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
  203. uni.getSetting({
  204. success(res) {
  205. if (!res.authSetting['scope.writePhotosAlbum']) {
  206. uni.showModal({
  207. title: '提示',
  208. content: '您还没有授权访问相册,请前往设置页面打开权限。',
  209. confirmText: '去设置',
  210. success(res) {
  211. if (res.confirm) {
  212. uni.openSetting();
  213. }
  214. }
  215. });
  216. } else {
  217. uni.showToast({
  218. title: '保存图片失败',
  219. icon: 'none'
  220. });
  221. }
  222. }
  223. });
  224. } else {
  225. uni.showToast({
  226. title: '保存图片失败',
  227. icon: 'none'
  228. });
  229. }
  230. },
  231. complete() {
  232. uni.hideLoading();
  233. that.posterShow = false;
  234. }
  235. });
  236. },
  237. }
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. .promotion-code {
  242. display: flex;
  243. flex-direction: column;
  244. align-items: center;
  245. position: relative;
  246. .promotion-code-info {
  247. width: 548rpx;
  248. height: 784rpx;
  249. flex-shrink: 0;
  250. box-sizing: border-box;
  251. .canvas {
  252. width: 100%;
  253. height: 100%;
  254. }
  255. .promotion-code-info-img {
  256. width: 100%;
  257. height: 100%;
  258. }
  259. }
  260. .promotion-code-but {
  261. margin-top: 40rpx;
  262. width: 422rpx;
  263. height: 80rpx;
  264. background: var(--gd-but-color);
  265. border-radius: 40rpx;
  266. display: flex;
  267. justify-content: center;
  268. align-items: center;
  269. color: #fff;
  270. font-size: 28rpx;
  271. font-family: SourceHanSansCN, SourceHanSansCN;
  272. font-weight: 500;
  273. }
  274. .promotion-code-clear {
  275. position: absolute;
  276. top: -10rpx;
  277. right: -10rpx;
  278. }
  279. }
  280. </style>