customPromotionCode.vue 7.7 KB

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