poster.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <template>
  2. <view class="poster_page">
  3. <swiper class="poster_swiper" previous-margin="110rpx" circular :current="swiperIndex" next-margin="110rpx" @change="onSwiperChange">
  4. <swiper-item v-for="(item, index) of list" :key="index">
  5. <view class="goods_info_box" :class="{ active: swiperIndex == index }">
  6. <image class="goods_image" :src="item.headImgs" mode="scaleToFill"></image>
  7. <view class="goods_info">
  8. <view class="goods_name">{{item.goodsName}}</view>
  9. <view class="price_box">
  10. <view class="price">¥{{item.goodsPrice}}</view>
  11. <view class="store_price">¥{{item.disCountPrice}}</view>
  12. </view>
  13. <view class="poster_info">
  14. <view class="info">
  15. <view>扫描二维码访问</view>
  16. <text>{{platformName}}</text>
  17. </view>
  18. <image class="poster_code_image" :src="recommendCodeGoods" mode="aspectFit"></image>
  19. </view>
  20. </view>
  21. </view>
  22. </swiper-item>
  23. </swiper>
  24. <canvas canvas-id="poster" class="poster_canvas"></canvas>
  25. <view class="share_save_box">
  26. <view class="push_button" @click="onSaveImg">
  27. <text style="font-size: 28rpx;">保存图片</text>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. // 文字换行
  34. function drawtext(text, maxWidth) {
  35. let textArr = text.split("");
  36. let len = textArr.length;
  37. // 上个节点
  38. let previousNode = 0;
  39. // 记录节点宽度
  40. let nodeWidth = 0;
  41. // 文本换行数组
  42. let rowText = [];
  43. // 如果是字母,侧保存长度
  44. let letterWidth = 0;
  45. // 汉字宽度
  46. let chineseWidth = 21;
  47. // otherFont宽度
  48. let otherWidth = 10.5;
  49. for (let i = 0; i < len; i++) {
  50. if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
  51. if(letterWidth > 0){
  52. if(nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth){
  53. rowText.push({
  54. type: "text",
  55. content: text.substring(previousNode, i)
  56. });
  57. previousNode = i;
  58. nodeWidth = chineseWidth;
  59. letterWidth = 0;
  60. } else {
  61. nodeWidth += chineseWidth + letterWidth * otherWidth;
  62. letterWidth = 0;
  63. }
  64. } else {
  65. if(nodeWidth + chineseWidth > maxWidth){
  66. rowText.push({
  67. type: "text",
  68. content: text.substring(previousNode, i)
  69. });
  70. previousNode = i;
  71. nodeWidth = chineseWidth;
  72. }else{
  73. nodeWidth += chineseWidth;
  74. }
  75. }
  76. } else {
  77. if(/\n/g.test(textArr[i])){
  78. rowText.push({
  79. type: "break",
  80. content: text.substring(previousNode, i)
  81. });
  82. previousNode = i + 1;
  83. nodeWidth = 0;
  84. letterWidth = 0;
  85. }else if(textArr[i] == "\\" && textArr[i + 1] == "n"){
  86. rowText.push({
  87. type: "break",
  88. content: text.substring(previousNode, i)
  89. });
  90. previousNode = i + 2;
  91. nodeWidth = 0;
  92. letterWidth = 0;
  93. }else if(/[a-zA-Z0-9]/g.test(textArr[i])){
  94. letterWidth += 1;
  95. if(nodeWidth + letterWidth * otherWidth > maxWidth){
  96. rowText.push({
  97. type: "text",
  98. content: text.substring(previousNode, i + 1 - letterWidth)
  99. });
  100. previousNode = i + 1 - letterWidth;
  101. nodeWidth = letterWidth * otherWidth;
  102. letterWidth = 0;
  103. }
  104. } else{
  105. if(nodeWidth + otherWidth > maxWidth){
  106. rowText.push({
  107. type: "text",
  108. content: text.substring(previousNode, i)
  109. });
  110. previousNode = i;
  111. nodeWidth = otherWidth;
  112. }else{
  113. nodeWidth += otherWidth;
  114. }
  115. }
  116. }
  117. }
  118. if (previousNode < len) {
  119. rowText.push({
  120. type: "text",
  121. content: text.substring(previousNode, len)
  122. });
  123. }
  124. return rowText;
  125. }
  126. let settingWritePhotosAlbum = false;
  127. export default {
  128. data() {
  129. return {
  130. list:[
  131. {
  132. goodsName:'麦趣尔纯牛奶 早餐奶 新疆全脂牛奶 营养盒装鲜奶',
  133. headImgs:'https://cms.gzhylwh.com/visit/hyl/445195a7-5b85-42c0-aa4c-f4bc0ca3d7a0detail.png',
  134. goodsPrice:'98',
  135. disCountPrice:'89'
  136. },
  137. {
  138. goodsName:'麦趣尔纯牛奶 早餐奶 新疆全脂牛奶 营养盒装鲜奶',
  139. headImgs:'https://cms.gzhylwh.com/visit/hyl/445195a7-5b85-42c0-aa4c-f4bc0ca3d7a0detail.png',
  140. goodsPrice:'98',
  141. disCountPrice:'89'
  142. },
  143. {
  144. goodsName:'麦趣尔纯牛奶 早餐奶 新疆全脂牛奶 营养盒装鲜奶',
  145. headImgs:'https://cms.gzhylwh.com/visit/hyl/445195a7-5b85-42c0-aa4c-f4bc0ca3d7a0detail.png',
  146. goodsPrice:'98',
  147. disCountPrice:'89'
  148. },
  149. {
  150. goodsName:'麦趣尔纯牛奶 早餐奶 新疆全脂牛奶 营养盒装鲜奶',
  151. headImgs:'https://cms.gzhylwh.com/visit/hyl/445195a7-5b85-42c0-aa4c-f4bc0ca3d7a0detail.png',
  152. goodsPrice:'98',
  153. disCountPrice:'89'
  154. }
  155. ],
  156. //平台
  157. platformName: "新邻社区",
  158. recommendCodeGoods: "https://cms.gzhylwh.com/visit/hyl/7f858fb8-bb1c-4289-95f9-23572d96862cad0e591c-489b-4869-b6da-84483a2ac559qrcode.jpg",
  159. //二维码
  160. swiperIndex: 0,
  161. posterImgs: [],
  162. h5SaveImg: ""
  163. };
  164. },
  165. //第一次加载
  166. onLoad(options) {
  167. },
  168. computed: {
  169. },
  170. //方法
  171. methods: {
  172. // 轮播图变化
  173. onSwiperChange(e) {
  174. this.swiperIndex = e.detail.current;
  175. },
  176. // 创建海报
  177. createPoster() {
  178. return new Promise((resolve, reject) => {
  179. uni.showLoading({
  180. title: '海报生成中'
  181. });
  182. const ctx = uni.createCanvasContext('poster');
  183. ctx.fillRect(0, 0, 375, 673);
  184. ctx.setFillStyle("#FFF");
  185. ctx.fillRect(0, 0, 375, 673);
  186. uni.downloadFile({
  187. url: this.list[this.swiperIndex].headImgs,
  188. success: (res) => {
  189. if (res.statusCode === 200) {
  190. ctx.drawImage(res.tempFilePath, 0, 0, 375, 375);
  191. uni.downloadFile({
  192. url: this.recommendCodeGoods,
  193. success: (res2) => {
  194. if (res.statusCode === 200) {
  195. // 商品标题
  196. ctx.setFontSize(16);
  197. ctx.setFillStyle('#333');
  198. let drawtextList = drawtext(this.list[this.swiperIndex].goodsName, 420);
  199. let textTop = 0;
  200. drawtextList.forEach((item,index) => {
  201. if(index < 2){
  202. textTop = 380 + (index + 1) * 28;
  203. if(index==0){
  204. ctx.fillText(item.content, 22, textTop);
  205. }else{
  206. if(item.content.length>20){
  207. ctx.fillText(item.content+"...", 22, textTop);
  208. }else{
  209. ctx.fillText(item.content, 22, textTop);
  210. }
  211. }
  212. }
  213. });
  214. // 商品价格
  215. ctx.setFontSize(22);
  216. ctx.setFillStyle('#f00');
  217. ctx.fillText('¥'+(this.list[this.swiperIndex].goodsPrice), 17, textTop + 47);
  218. // 商品门市价
  219. ctx.setFontSize(18);
  220. ctx.setFillStyle('#999');
  221. let textLeft = 38 + (('¥'+this.list[this.swiperIndex].goodsPrice).length * 13)
  222. ctx.fillText('¥'+(this.list[this.swiperIndex].disCountPrice), textLeft, textTop + 45);
  223. // 商品门市价横线
  224. ctx.beginPath();
  225. ctx.setLineWidth(1);
  226. ctx.moveTo(textLeft - 1, textTop + 38);
  227. ctx.lineTo((textLeft + 18 + (('¥'+this.list[this.swiperIndex].disCountPrice).length * 9)), textTop + 38);
  228. ctx.setStrokeStyle('#999');
  229. ctx.stroke();
  230. // 商品分割线
  231. ctx.beginPath();
  232. ctx.setLineWidth(1);
  233. ctx.moveTo(17, textTop + 70);
  234. ctx.lineTo(358, textTop + 70);
  235. ctx.setStrokeStyle('#eee');
  236. ctx.stroke();
  237. // 商品价格
  238. // ctx.setFontSize(20);
  239. // ctx.setFillStyle('#f00');
  240. // ctx.fillText(this.list[this.swiperIndex].goodsPrice, 17, textTop + 47);
  241. // // 商品门市价
  242. // ctx.setFontSize(18);
  243. // ctx.setFillStyle('#999');
  244. // let textLeft = 38 + (this.list[this.swiperIndex].goodsPrice.length * 13)
  245. // ctx.fillText(this.list[this.swiperIndex].disCountPrice, textLeft, textTop + 45);
  246. // // 商品门市价横线
  247. // ctx.beginPath();
  248. // ctx.setLineWidth(1);
  249. // ctx.moveTo(textLeft - 1, textTop + 38);
  250. // ctx.lineTo((textLeft + 5 + this.list[this.swiperIndex].disCountPrice.length * 9), textTop + 38);
  251. // ctx.setStrokeStyle('#999');
  252. // ctx.stroke();
  253. // 商品分割线
  254. ctx.beginPath();
  255. ctx.setLineWidth(1);
  256. ctx.moveTo(17, textTop + 70);
  257. ctx.lineTo(358, textTop + 70);
  258. ctx.setStrokeStyle('#eee');
  259. ctx.stroke();
  260. // 长按识别二维码访问
  261. ctx.setFontSize(14);
  262. ctx.setFillStyle('#333');
  263. ctx.fillText("扫描二维码访问", 17, textTop + 136);
  264. // 平台名称
  265. ctx.setFontSize(12);
  266. ctx.setFillStyle('#999');
  267. ctx.fillText(this.platformName, 17, textTop + 170);
  268. // 二维码
  269. ctx.drawImage(res2.tempFilePath, 238, textTop + 88, 120, 120);
  270. ctx.draw(true, () => {
  271. // canvas画布转成图片并返回图片地址
  272. uni.canvasToTempFilePath({
  273. canvasId: 'poster',
  274. width: 375,
  275. height: 673,
  276. success: (res) => {
  277. if(this.posterImgs[this.swiperIndex]){
  278. this.posterImgs[this.swiperIndex].temporary = res.tempFilePath;
  279. }else{
  280. this.posterImgs[this.swiperIndex] = {
  281. temporary: res.tempFilePath
  282. };
  283. }
  284. console.log("海报制作成功!");
  285. resolve(res.tempFilePath);
  286. },
  287. fail: () => {
  288. uni.hideLoading();
  289. reject();
  290. }
  291. })
  292. });
  293. } else {
  294. uni.hideLoading();
  295. uni.showToast({
  296. title: '海报制作失败,图片下载失败',
  297. icon: 'none'
  298. });
  299. }
  300. },
  301. fail: err => {
  302. uni.hideLoading();
  303. uni.showToast({
  304. title: '海报制作失败,图片下载失败',
  305. icon: 'none'
  306. });
  307. }
  308. });
  309. } else {
  310. uni.hideLoading();
  311. uni.showToast({
  312. title: '海报制作失败,图片下载失败',
  313. icon: 'none'
  314. });
  315. }
  316. },
  317. fail: err => {
  318. uni.hideLoading();
  319. uni.showToast({
  320. title: '海报制作失败,图片下载失败',
  321. icon: 'none'
  322. });
  323. }
  324. });
  325. });
  326. },
  327. // 保存图片
  328. async onSaveImg() {
  329. let imgUrl = "";
  330. if(this.posterImgs[this.swiperIndex] && this.posterImgs[this.swiperIndex].temporary){
  331. imgUrl = await this.posterImgs[this.swiperIndex].temporary;
  332. }else{
  333. imgUrl = await this.createPoster();
  334. }
  335. // #ifdef H5
  336. this.h5SaveImg = imgUrl;
  337. uni.hideLoading();
  338. // #endif
  339. // #ifdef MP-WEIXIN
  340. uni.showLoading({
  341. title: '海报下载中'
  342. });
  343. if (settingWritePhotosAlbum) {
  344. uni.getSetting({
  345. success: res => {
  346. if (res.authSetting['scope.writePhotosAlbum']) {
  347. uni.saveImageToPhotosAlbum({
  348. filePath: imgUrl,
  349. success: () => {
  350. uni.showToast({
  351. title: '保存成功'
  352. });
  353. },
  354. complete: () => {
  355. uni.hideLoading();
  356. }
  357. });
  358. } else {
  359. uni.showModal({
  360. title: '提示',
  361. content: '请先在设置页面打开“保存相册”使用权限',
  362. confirmText: '去设置',
  363. cancelText: '算了',
  364. success: data => {
  365. if (data.confirm) {
  366. uni.openSetting();
  367. }
  368. },
  369. complete: () => {
  370. uni.hideLoading();
  371. }
  372. });
  373. }
  374. },
  375. complete: () => {
  376. uni.hideLoading();
  377. }
  378. });
  379. } else {
  380. settingWritePhotosAlbum = true;
  381. uni.authorize({
  382. scope: 'scope.writePhotosAlbum',
  383. success: () => {
  384. uni.saveImageToPhotosAlbum({
  385. filePath: imgUrl,
  386. success: () => {
  387. uni.showToast({
  388. title: '保存成功'
  389. });
  390. },
  391. complete: () => {
  392. uni.hideLoading();
  393. }
  394. });
  395. },
  396. complete: () => {
  397. uni.hideLoading();
  398. }
  399. });
  400. }
  401. // #endif
  402. // #ifdef APP-PLUS
  403. uni.showLoading({
  404. title: '海报下载中'
  405. });
  406. uni.saveImageToPhotosAlbum({
  407. filePath: imgUrl,
  408. success: () => {
  409. uni.hideLoading();
  410. uni.showToast({
  411. title: '保存成功'
  412. });
  413. }
  414. });
  415. // #endif
  416. }
  417. }
  418. };
  419. </script>
  420. <style lang="scss" scoped>
  421. .poster_page {
  422. min-height: 100vh;
  423. background-color: #f5f5f5;
  424. display: flex;
  425. align-items: center;
  426. }
  427. .poster_canvas {
  428. width: 750rpx;
  429. height: 1324rpx;
  430. position: fixed;
  431. top: -10000rpx;
  432. left: 0rpx;
  433. }
  434. .poster_swiper {
  435. height: 950rpx;
  436. width: 100%;
  437. swiper-item {
  438. box-sizing: border-box;
  439. display: flex;
  440. align-items: center;
  441. .goods_info_box {
  442. width: 100%;
  443. height: 100%;
  444. transform: scale(0.88);
  445. transition: all 0.4s;
  446. position: relative;
  447. overflow: hidden;
  448. background-color: #FFFFFF;
  449. &.active {
  450. transform: scale(1);
  451. }
  452. .goods_image {
  453. width: 100%;
  454. height: calc(100vw - 220rpx);
  455. }
  456. .goods_info {
  457. padding: 24rpx;
  458. .goods_name {
  459. color: #333;
  460. font-size: 30rpx;
  461. overflow: hidden;
  462. text-overflow: ellipsis;
  463. display: -webkit-box;
  464. -webkit-line-clamp: 2;
  465. -webkit-box-orient: vertical;
  466. }
  467. .price_box {
  468. margin-top: 24rpx;
  469. display: flex;
  470. align-items: center;
  471. .price {
  472. font-size: 38rpx;
  473. color: red;
  474. }
  475. .store_price {
  476. margin-left: 30rpx;
  477. font-size: 26rpx;
  478. color: #999;
  479. text-decoration: line-through;
  480. }
  481. }
  482. .poster_info {
  483. border-top: 2rpx solid #f1f1f1;
  484. padding-top: 24rpx;
  485. margin-top: 24rpx;
  486. display: flex;
  487. align-items: center;
  488. justify-content: space-between;
  489. .info {
  490. display: flex;
  491. flex-direction: column;
  492. view {
  493. color: #333;
  494. font-size: 28rpx;
  495. }
  496. text {
  497. color: #999;
  498. font-size: 24rpx;
  499. margin-top: 20rpx;
  500. }
  501. }
  502. .poster_code_image {
  503. width: 170rpx;
  504. height: 170rpx;
  505. flex-shrink: 0;
  506. }
  507. }
  508. }
  509. }
  510. }
  511. }
  512. .push_button{
  513. position:relative;
  514. color:#FFF;
  515. display:block;
  516. text-decoration:none;
  517. margin:0 auto;
  518. border-radius:40rpx;
  519. text-align:center;
  520. padding:12rpx 80rpx;
  521. -webkit-transition: all 0.1s;
  522. -moz-transition: all 0.1s;
  523. transition: all 0.1s;
  524. background-color: rgba(232, 0, 0,.7);
  525. -webkit-box-shadow: 2px 4px 2px rgba(232, 0, 0,.5);
  526. -moz-box-shadow: 2px 4px 2px rgba(232, 0, 0,.5);
  527. box-shadow: 2px 4px 2px rgba(232, 0, 0,.5);
  528. }
  529. .push_button:active{
  530. -webkit-box-shadow: 0px 2px 0px rgba(232, 0, 0,.5);
  531. -moz-box-shadow: 0px 2px 0px rgba(232, 0, 0,.5);
  532. box-shadow: 0px 2px 0px rgba(232, 0, 0,.5);
  533. position:relative;
  534. top:7px;
  535. }
  536. .share_save_box {
  537. position: fixed;
  538. bottom: calc((100vh - 950rpx - 140rpx) / 4);
  539. left: 0;
  540. z-index: 6;
  541. width: 100%;
  542. display: flex;
  543. }
  544. </style>