goodsPoster.vue 15 KB

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