customPromotionCode.vue 6.7 KB

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