share.vue 18 KB

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