giftModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <view class="aleart" v-if="aleartStatus" :style="'background-image: url(' + giftbag + ');'">
  3. <template v-if="!posterImageStatus">
  4. <text class="iconfont icon-cha2 close" @click="posterImageClose"></text>
  5. <view class="from">赠送给好友一份礼物</view>
  6. <view class="message">{{ giftData.message }}</view>
  7. <view class="aleart-body">
  8. <image class="goods-img" :src="giftData.image" mode=""></image>
  9. </view>
  10. <view class="title line1">
  11. {{ $t(giftData.title) }}
  12. </view>
  13. <!-- #ifdef H5 -->
  14. <view class="btn" @click="copyLink()">
  15. {{ $t('复制礼物链接') }}
  16. </view>
  17. <!-- #endif -->
  18. <!-- #ifndef H5 -->
  19. <button class="btn" open-type="share" hover-class="none">
  20. {{ $t(`送给好友`) }}
  21. </button>
  22. <!-- #endif -->
  23. <view class="btn-clear" @click="getPoster()">
  24. {{ $t('保存海报') }}
  25. </view>
  26. </template>
  27. <template v-if="posterImageStatus">
  28. <text class="iconfont icon-cha2 close" @click="posterImageClose"></text>
  29. <image class="poster-img" :src="posterImage"></image>
  30. <!-- #ifdef H5 -->
  31. <view class="keep">{{ $t(`长按图片可以保存到手机`) }}</view>
  32. <!-- #endif -->
  33. </template>
  34. <!-- #ifdef H5 || APP-PLUS -->
  35. <zb-code
  36. ref="qrcode"
  37. :show="codeShow"
  38. :cid="cid"
  39. :val="codeVal"
  40. :size="size"
  41. :unit="unit"
  42. :background="background"
  43. :foreground="foreground"
  44. :pdground="pdground"
  45. :icon="codeIcon"
  46. :iconSize="iconsize"
  47. :onval="onval"
  48. :loadMake="loadMake"
  49. @result="qrR"
  50. />
  51. <!-- #endif -->
  52. </view>
  53. </template>
  54. <script>
  55. import { HTTP_REQUEST_URL } from '@/config/app';
  56. export default {
  57. data() {
  58. return {
  59. aleartData: {},
  60. bag: HTTP_REQUEST_URL + '/statics/images/canvas-bag.png',
  61. giftBorder: HTTP_REQUEST_URL + '/statics/images/gift-border.png',
  62. giftbag: HTTP_REQUEST_URL + '/statics/images/gift-bag.png',
  63. //二维码参数
  64. codeShow: false,
  65. cid: '1',
  66. codeVal: '', // 要生成的二维码值
  67. size: 200, // 二维码大小
  68. unit: 'upx', // 单位
  69. background: '#FFF', // 背景色
  70. foreground: '#000', // 前景色
  71. pdground: '#000', // 角标色
  72. codeIcon: '', // 二维码图标
  73. iconsize: 40, // 二维码图标大小
  74. lv: 3, // 二维码容错级别 , 一般不用设置,默认就行
  75. onval: true, // val值变化时自动重新生成二维码
  76. loadMake: true, // 组件加载完成后自动生成二维码
  77. PromotionCode: '',
  78. posterImageStatus: false,
  79. posterImage: ''
  80. };
  81. },
  82. props: {
  83. giftData: {
  84. type: Object
  85. },
  86. aleartStatus: {
  87. type: Boolean,
  88. default: false
  89. }
  90. },
  91. watch: {
  92. aleartStatus(status) {
  93. if (!status) {
  94. this.aleartData = {};
  95. } else {
  96. // #ifdef H5
  97. this.codeVal = window.location.origin + '/pages/goods/receive_gift/index?id=' + this.giftData.id + '&spid=' + this.$store.state.app.uid;
  98. // #endif
  99. // #ifdef APP-PLUS
  100. this.codeVal = HTTP_REQUEST_URL + '/pages/goods/receive_gift/index?id=' + this.giftData.id + '&spid=' + this.$store.state.app.uid;
  101. // #endif
  102. // #ifdef MP
  103. this.PromotionCode = this.giftData.code;
  104. // #endif
  105. }
  106. }
  107. },
  108. methods: {
  109. copyLink() {
  110. uni.setClipboardData({
  111. data: this.codeVal
  112. });
  113. },
  114. qrR(res) {
  115. console.log(res);
  116. // #ifdef H5
  117. if (!this.$wechat.isWeixin() || this.shareQrcode != '1') {
  118. this.PromotionCode = res;
  119. }
  120. // #endif
  121. // #ifdef APP-PLUS
  122. this.PromotionCode = res;
  123. // #endif
  124. },
  125. //隐藏弹窗
  126. posterImageClose() {
  127. this.posterImageStatus = false
  128. this.$emit('close', false);
  129. },
  130. drawPoster(loadedImages, name, store_name) {
  131. // 截断标题函数
  132. function truncateTitle(title, maxLength) {
  133. if (title.length > maxLength) {
  134. return title.substring(0, maxLength) + '...';
  135. }
  136. return title;
  137. }
  138. // 获取canvas上下文
  139. const ctx = uni.createCanvasContext('posterCanvas');
  140. return new Promise(async (resolve, reject) => {
  141. uni.getImageInfo({
  142. src: loadedImages[0],
  143. success: (res) => {
  144. // 海报尺寸
  145. const posterWidth = 375;
  146. const posterHeight = 579;
  147. // const posterWidth = res.width / 2;
  148. // const posterHeight = res.height / 2;
  149. console.log(posterWidth, posterHeight);
  150. // 绘制背景图
  151. ctx.drawImage(loadedImages[0], 0, 0, posterWidth, posterHeight);
  152. ctx.save();
  153. // 头像和标题的布局
  154. const avatarSize = 22; // 头像尺寸
  155. const nickname = name; // 昵称
  156. const title = '赠送给好友一份礼物'; // 标题文字
  157. const titleFontSize = 14; // 标题字号
  158. const nicknameFontSize = 14; // 昵称字号
  159. const padding = 10; // 元素之间的间距
  160. // 计算标题宽度
  161. ctx.setFontSize(titleFontSize);
  162. const titleWidth = ctx.measureText(title).width;
  163. const nicknameWidth = ctx.measureText(nickname).width;
  164. // 计算头像和标题的总宽度
  165. const totalWidth = avatarSize + padding + nicknameWidth + padding + titleWidth;
  166. // 计算起始绘制位置(水平居中)
  167. const startX = (posterWidth - totalWidth) / 2;
  168. const startY = 77; // 距离顶部的距离
  169. // 绘制头像
  170. // ctx.drawImage(loadedImages[3], startX, startY, avatarSize, avatarSize);
  171. const avatarX = startX + avatarSize / 2; // 头像中心点 X
  172. const avatarY = startY + avatarSize / 2; // 头像中心点 Y
  173. ctx.save(); // 保存画布状态
  174. ctx.beginPath();
  175. ctx.arc(avatarX, avatarY, avatarSize / 2, 0, Math.PI * 2); // 绘制圆形路径
  176. ctx.clip(); // 裁剪圆形区域
  177. ctx.drawImage(loadedImages[3], startX, startY, avatarSize, avatarSize); // 绘制头像
  178. ctx.restore(); // 恢复画布状态
  179. // 绘制昵称
  180. ctx.setFontSize(nicknameFontSize);
  181. ctx.setTextAlign('left');
  182. ctx.fillText(nickname, startX + avatarSize + padding, startY + avatarSize - 5); // 调整文字垂直居中
  183. // 绘制标题
  184. ctx.setFontSize(titleFontSize);
  185. ctx.fillText(title, startX + avatarSize + padding + nicknameWidth + padding, startY + avatarSize - 5);
  186. // 商品图尺寸
  187. const productImageSize = 225; // 商品图尺寸为 225px x 225px
  188. // 绘制商品图边框
  189. const productBorderX = (posterWidth - productImageSize) / 2; // 水平居中
  190. const productBorderY = startY + avatarSize + 31; // 距离头像和标题的间距
  191. ctx.drawImage(loadedImages[1], productBorderX, productBorderY, productImageSize, productImageSize);
  192. // 绘制商品图
  193. const productImagePadding = 10; // 商品图与边框的内边距
  194. const productImageX = productBorderX + productImagePadding;
  195. const productImageY = productBorderY + productImagePadding + 11;
  196. const productImageInnerSize = productImageSize - 2 * productImagePadding; // 商品图实际绘制尺寸
  197. ctx.drawImage(loadedImages[2], productImageX, productImageY, productImageInnerSize, productImageInnerSize - 10);
  198. // 绘制商品标题
  199. const productTitle = store_name;
  200. const maxTitleLength = 20; // 标题最大长度
  201. const truncatedTitle = truncateTitle(productTitle, maxTitleLength); // 截断标题
  202. ctx.setFontSize(14);
  203. ctx.setTextAlign('center');
  204. ctx.fillText(truncatedTitle, posterWidth / 2, productBorderY + productImageSize + 26);
  205. // 绘制分享二维码
  206. const qrCodeSize = 100;
  207. const qrCodeX = (posterWidth - qrCodeSize) / 2;
  208. const qrCodeY = productBorderY + productImageSize + 63; // 距离商品标题的间距
  209. ctx.drawImage(loadedImages[4], qrCodeX, qrCodeY, qrCodeSize, qrCodeSize);
  210. // 绘制完成
  211. ctx.draw(false, () => {
  212. console.log(posterWidth, posterHeight);
  213. // 生成海报图片
  214. uni.canvasToTempFilePath({
  215. canvasId: 'posterCanvas',
  216. width: posterWidth,
  217. height: posterHeight,
  218. success: (res) => {
  219. console.log(res.tempFilePath);
  220. resolve(res.tempFilePath);
  221. },
  222. fail: (err) => {
  223. console.log(err);
  224. reject(err);
  225. }
  226. });
  227. });
  228. }
  229. });
  230. });
  231. },
  232. loadImage(src) {
  233. return new Promise((resolve, reject) => {
  234. const img = new Image();
  235. img.crossOrigin = 'anonymous'; // 允许跨域
  236. img.src = src;
  237. img.onload = () => {
  238. console.log(img);
  239. resolve(img);
  240. };
  241. img.onerror = (err) => reject(err);
  242. });
  243. },
  244. share() {
  245. this.$emit('shareH5');
  246. },
  247. getPoster() {
  248. let images = [this.bag, this.giftBorder, this.giftData.image, this.giftData.avatar, this.PromotionCode];
  249. console.log(images);
  250. let postImg = ['', '', '', '', ''];
  251. // #ifdef MP
  252. for (let i = 0; i < images.length; i++) {
  253. uni.downloadFile({
  254. url: images[i],
  255. success: (res) => {
  256. if (res.statusCode == 200) {
  257. postImg[i] = res.tempFilePath;
  258. }
  259. console.log(i, images.length - 1, postImg);
  260. const allNotEmpty = postImg.every((item) => item !== '');
  261. if (allNotEmpty) this.goPoster(postImg);
  262. },
  263. fail: function () {
  264. this.$set(this, 'PromotionCode', '');
  265. }
  266. });
  267. }
  268. // #endif
  269. // #ifndef MP
  270. this.goPoster(images);
  271. // #endif
  272. },
  273. goPoster(postImg) {
  274. this.drawPoster(postImg, this.giftData.nickname, this.giftData.title)
  275. .then((posterPath) => {
  276. console.log('海报生成成功:', posterPath);
  277. // #ifdef APP-PLUS || MP
  278. this.savePosterPathMp(posterPath);
  279. // #endif
  280. // #ifdef H5
  281. this.posterImage = posterPath;
  282. this.posterImageStatus = true;
  283. // #endif
  284. })
  285. .catch((err) => {
  286. console.error('海报生成失败:', err);
  287. });
  288. },
  289. // #ifdef APP-PLUS || MP
  290. savePosterPathMp(url) {
  291. let that = this;
  292. uni.saveImageToPhotosAlbum({
  293. filePath: url,
  294. success: function (res) {
  295. that.$util.Tips({
  296. title: that.$t(`保存成功`),
  297. icon: 'success'
  298. });
  299. },
  300. fail: function (res) {
  301. that.$util.Tips({
  302. title: that.$t(`保存失败`)
  303. });
  304. }
  305. });
  306. },
  307. // #endif
  308. savePic(url) {
  309. var a = document.createElement('a'); // 生成一个a元素
  310. a.download = 'Gift'; // 设置图片名称
  311. a.style.display = 'none';
  312. a.href = url; // 将生成的URL设置为a.href属性
  313. document.body.appendChild(a); // 将a标签追加到文档对象中
  314. a.click(); // 触发a的单击事件
  315. a.remove(); // 一次性的,用完就删除a标签
  316. }
  317. }
  318. };
  319. </script>
  320. <style lang="scss" scoped>
  321. .aleart {
  322. width: 600rpx;
  323. height: 980rpx;
  324. position: fixed;
  325. left: 50%;
  326. transform: translateX(-50%);
  327. z-index: 999;
  328. top: 50%;
  329. margin-top: -490rpx;
  330. background-color: #fff;
  331. border-radius: 32rpx;
  332. background-size: 100% 100%;
  333. display: flex;
  334. flex-direction: column;
  335. align-items: center;
  336. justify-content: center;
  337. .poster-img {
  338. width: 100%;
  339. height: 100%;
  340. border-radius: 32rpx;
  341. }
  342. .from {
  343. font-size: 28rpx;
  344. font-weight: 500;
  345. color: #333333;
  346. margin-bottom: 16rpx;
  347. }
  348. .message {
  349. font-weight: 400;
  350. font-size: 26rpx;
  351. color: #999999;
  352. margin-bottom: 42rpx;
  353. }
  354. .aleart-body {
  355. display: flex;
  356. align-items: center;
  357. justify-content: center;
  358. width: 396rpx;
  359. height: 419rpx;
  360. background-image: url('../../static/gift-border.png');
  361. background-size: 100% 100%;
  362. margin-bottom: 32rpx;
  363. .goods-img {
  364. width: 360rpx;
  365. height: 360rpx;
  366. margin-top: 24rpx;
  367. }
  368. }
  369. .title {
  370. max-width: 80%;
  371. font-weight: 400;
  372. font-size: 28rpx;
  373. color: #333333;
  374. margin-bottom: 52rpx;
  375. text-align: center;
  376. }
  377. .btn,
  378. .btn-clear {
  379. width: 396rpx;
  380. height: 80rpx;
  381. line-height: 80rpx;
  382. border-radius: 20px;
  383. text-align: center;
  384. font-size: 28rpx;
  385. }
  386. .btn {
  387. color: #fff;
  388. background: linear-gradient(90deg, #ff7931 0%, #e93323 100%);
  389. }
  390. .btn-clear {
  391. margin-top: 20rpx;
  392. color: #e93323;
  393. border: 1px solid #e93323;
  394. }
  395. .keep {
  396. font-size: 24rpx;
  397. font-weight: bold;
  398. color: rgba(255, 255, 255, 0.7);
  399. position: fixed;
  400. right: calc(50% - 130rpx);
  401. bottom: -45rpx;
  402. display: block;
  403. }
  404. .close {
  405. font-size: 50rpx;
  406. font-weight: bold;
  407. color: #fff;
  408. position: fixed;
  409. right: calc(50% - 23rpx);
  410. bottom: -110rpx;
  411. display: block;
  412. }
  413. }
  414. </style>