giftModal.vue 14 KB

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