share.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <view>
  3. <view class="ceng" :hidden="hideToast" @tap="cancelPopup"></view>
  4. <view class="sharePop" :hidden="hideToast">
  5. <view class="share-tit">分享到</view>
  6. <view class="share-btn">
  7. <button class="friend" open-type="share" v-if="!isVideoScene">
  8. <image src="/static/pages/images/wechat.png"></image>
  9. 微信好友
  10. </button>
  11. <view class="poster" @tap="generatePoster" v-if="codeUrl">
  12. <image src="/static/pages/images/poster.png"></image>
  13. 生成海报
  14. </view>
  15. </view>
  16. <view class="cancel" @tap="cancelPopup">取消</view>
  17. </view>
  18. <canvas class="canvas" style="width:400px;height:595px;" canvas-id="share"></canvas>
  19. <!-- 分享弹窗 -->
  20. <view class="ceng" :hidden="hidePoster" @tap="showPoster"></view>
  21. <view class="beshare" :hidden="hidePoster">
  22. <view class="beshares">
  23. <image src="/activity/static/activity/images/share_pintuan.jpg" mode="widthFix" class="sharebg" v-if="aType==3"></image>
  24. <image src="/static/pages/images/close.png" class="close" @tap="showPoster"></image>
  25. <view class="user dflex">
  26. <image :src="userInfo.avatar" mode="aspectFit" class="toux"></image>
  27. <view class="flex">
  28. <view :class="aType==3 ? 'colorfff' : ''">{{userInfo.nickName}}</view>
  29. <view v-if="product.pic">
  30. <text v-if="aType==1">邀请您参与社区团购</text>
  31. <text v-if="aType==2">邀请您参与限时秒杀</text>
  32. <text v-if="aType==3" class="colorfff">邀请您参与拼团</text>
  33. <text v-if="aType==4">邀请您参与新客特价</text>
  34. <text v-if="aType==5">邀请您参与砍价</text>
  35. <text v-if="aType==0">给你推荐了一个好东西</text>
  36. </view>
  37. <view v-if="product.share_img">
  38. <text>邀请您观看直播</text>
  39. <image src="/static/pages/images/live1.png" class="live"></image>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="binfos">
  44. <view class="bimgbox">
  45. <image :src="product.images[0]" mode="aspectFit" class="bimg" v-if="product.images && product.images.length > 0"></image>
  46. <block v-else>
  47. <image :src="product.pic" mode="aspectFit" class="bimg" v-if="product.pic"></image>
  48. </block>
  49. <image :src="product.share_img" mode="aspectFit" class="bimg" v-if="product.share_img"></image>
  50. </view>
  51. <view class="texts">
  52. <view class="binfo" v-if="product.pic">
  53. <view class="bprice dflex">
  54. <block v-if="product.mode==2">
  55. <text>{{product.integral?product.integral:0}}积分</text>
  56. <text class="money" v-if="product.salePrice">+¥{{product.salePrice}}</text>
  57. <text class="del" v-if="product.salePrice == product.actualPrice && product.marketPrice > 0">¥{{product.marketPrice}}</text>
  58. </block>
  59. <block v-else>
  60. <text>¥</text>{{product.salePrice?product.salePrice:'0'}}
  61. <view class="del">¥{{product.marketPrice?product.marketPrice:'0'}}</view>
  62. </block>
  63. </view>
  64. <view class="btit">{{product.title}}</view>
  65. </view>
  66. <view class="binfo" v-if="product.share_img">
  67. <view class="blive dflex">
  68. <text>{{product.anchor_name}}的直播间</text>
  69. </view>
  70. <view class="btit">{{product.month}}{{product.hour}}开播</view>
  71. </view>
  72. <view class="code">
  73. <image :src="codeUrl"></image>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. <view class="save" @tap="saveImage">保存图片</view>
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. const req = require("../../utils/request.js");
  84. export default {
  85. data() {
  86. return {
  87. // canvasW: 0,
  88. // canvasH: 0,
  89. imgUrl: '',
  90. userInfo: {},
  91. isVideoScene: false
  92. };
  93. },
  94. components: {},
  95. props: {
  96. hideToast: {
  97. type: Boolean,
  98. default: true
  99. },
  100. hidePoster: {
  101. type: Boolean,
  102. default: true
  103. },
  104. product: Object,
  105. codeUrl: String,
  106. bgUrl: String,
  107. aType: Number,
  108. canvasW: Number,
  109. canvasH: Number,
  110. address: {
  111. type: String,
  112. default: '湖南省娄底市经济技术开发区大埠桥办事处南阳村'
  113. },
  114. },
  115. onReady() {
  116. // console.log(this.hideToast)
  117. },
  118. onHide() {
  119. // console.log(this.hideToast)
  120. },
  121. mounted() {
  122. this.isVideoScene = req.getStorage('isVideoScene')
  123. // console.log(this.hideToast,888888888)
  124. },
  125. methods: {
  126. generatePoster() {
  127. let that = this;
  128. let isShowLoading = false;
  129. let userInfo = req.getStorage('userInfo');
  130. this.setData({
  131. userInfo: userInfo
  132. });
  133. if (!isShowLoading) {
  134. uni.showLoading({
  135. title: '生成中…',
  136. mask: true
  137. });
  138. isShowLoading = true;
  139. }
  140. this.generate(imgUrl => {
  141. // console.log(isShowLoading);
  142. if (isShowLoading) {
  143. uni.hideLoading();
  144. isShowLoading = false;
  145. }
  146. that.setData({
  147. imgUrl: imgUrl,
  148. hideToast: true,
  149. hidePoster: false
  150. });
  151. });
  152. },
  153. showPoster() {
  154. //隐藏海报
  155. this.setData({
  156. hidePoster: true
  157. });
  158. this.$emit('onFather', this.hideToast)
  159. },
  160. cancelPopup() {
  161. this.setData({
  162. hideToast: true
  163. });
  164. this.$emit('onFather', this.hideToast)
  165. },
  166. isAuth(fun) {
  167. if (!uni.saveImageToPhotosAlbum) {
  168. uni.showModal({
  169. title: '提示',
  170. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  171. });
  172. return;
  173. }
  174. ; //可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
  175. // #ifndef H5
  176. uni.getSetting({
  177. success(res) {
  178. // console.log(res);
  179. if (!res.authSetting['scope.writePhotosAlbum']) {
  180. // 接口调用询问
  181. uni.authorize({
  182. scope: 'scope.writePhotosAlbum',
  183. success() {
  184. // console.log("2-授权《保存图片》权限成功");
  185. fun(true);
  186. },
  187. fail() {
  188. fun(false);
  189. }
  190. });
  191. } else {
  192. // 拒绝授权
  193. fun(true);
  194. }
  195. },
  196. fail(res) {
  197. fun(false);
  198. }
  199. });
  200. // #endif
  201. },
  202. saveImage() {
  203. let that = this;
  204. that.isAuth(success => {
  205. if (success) {
  206. uni.saveImageToPhotosAlbum({
  207. filePath: that.imgUrl,
  208. success(res) {
  209. uni.showToast({
  210. title: '图片保存成功',
  211. icon: 'none',
  212. success() { // that.generate(imgUrl => {
  213. // that.setData({
  214. // imgUrl: imgUrl
  215. // })
  216. // })
  217. },
  218. fail() {
  219. uni.showToast({
  220. title: '图片保存失败'
  221. });
  222. }
  223. });
  224. }
  225. });
  226. }
  227. });
  228. },
  229. getImageInfo(url) {
  230. // console.log(url);
  231. return new Promise((resolve, reject) => {
  232. if (!url) {
  233. resolve();
  234. return false;
  235. }
  236. uni.getImageInfo({
  237. src: url,
  238. success: resolve,
  239. fail: reject
  240. });
  241. });
  242. },
  243. generate(success) {
  244. // console.log(this);
  245. let picPromise = '';
  246. if(this.product.images && this.product.images.length > 0){
  247. picPromise = this.getImageInfo(this.product.images[0]);
  248. }else{
  249. if (this.product.pic != undefined) {
  250. // 获取商品图片
  251. picPromise = this.getImageInfo(this.product.pic);
  252. // console.log(picPromise);
  253. }
  254. }
  255. if (this.product.share_img != undefined) {
  256. //获取直播界面图片
  257. picPromise = this.getImageInfo(this.product.share_img);
  258. } // console.log(this.data.product.share_img)
  259. // console.log(this.data.product.pic != undefined, this.data.product.share_img != undefined)
  260. // console.log(this.data.product.share_img)
  261. // 头像
  262. const avatarPromise = this.getImageInfo(this.userInfo.avatar); // 获取小程序码图
  263. let codePromise;
  264. if (!this.codeUrl) {
  265. return req.msg('二维码生成失败');
  266. } else {
  267. codePromise = this.getImageInfo(this.codeUrl);
  268. } //获取背景图
  269. // const bgPromise=this.getImageInfo(this.data.bgUrl)
  270. // console.log(this.data.userInfo.avatar)
  271. // console.log(this.data.codeUrl)
  272. // console.log(this.data.bgUrl)
  273. let that = this;
  274. Promise.all([picPromise, avatarPromise, codePromise]).then(([pic, avatar, code]) => {
  275. // console.log(pic, avatar, code); // 创建绘图上下文
  276. const ctx = uni.createCanvasContext('share', this); // 绘制背景,填充满整个canvas画布
  277. let width = 400,
  278. height = 595; //默认背景
  279. ctx.setFillStyle('#f6f6f6');
  280. ctx.fillRect(0, 0, width, height); // 拼团背景
  281. if (this.aType == 3) {
  282. // ctx.drawImage('/pages/images/live1.png', 333, 30,50,28) //绘制图片
  283. ctx.drawImage("/activity/static/activity/images/share_pintuan.jpg", 0, 0, 400, 595);
  284. }
  285. if (that.product.share_img != undefined) {
  286. ctx.drawImage("/static/pages/images/live1.png", 333, 30, 50, 28); //绘制图片
  287. // ctx.drawImage('/activity/images/share_pintuan.jpg', 400, 595, 0, 0);
  288. } // 用户信息
  289. ctx.fillStyle = '#333';
  290. ctx.font = "18px PingFang SC";
  291. if (this.aType == 3) {
  292. ctx.fillStyle = '#fff';
  293. }
  294. ctx.fillText(that.userInfo.nickName, 80, 35);
  295. ctx.fillStyle = '#999';
  296. ctx.font = "14px PingFang SC";
  297. if (this.aType == 1) {
  298. ctx.fillText('邀请您参与社区团购', 80, 58);
  299. }
  300. if (this.aType == 2) {
  301. ctx.fillText('邀请您参与限时秒杀', 80, 58);
  302. }
  303. if (this.aType == 3) {
  304. ctx.fillStyle = '#fff';
  305. ctx.fillText('邀请您参与拼团', 80, 58);
  306. }
  307. if (this.aType == 4) {
  308. ctx.fillText('邀请您参与新客特价', 80, 58);
  309. }
  310. if (this.aType == 5) {
  311. ctx.fillText('邀请您参与砍价', 80, 58);
  312. }
  313. if (this.product.share_img != undefined && this.aType == 6) {
  314. ctx.fillText('邀请您观看直播', 80, 58);
  315. }
  316. if (this.aType == 0) {
  317. ctx.fillText('给你推荐了一个好东西', 80, 58);
  318. } //白色背景
  319. ctx.save();
  320. that.roundRect(ctx, 15, 80, 370, 500, 10);
  321. ctx.restore();
  322. const picW = 340;
  323. let lv;
  324. if (pic.height >= pic.width) {
  325. lv = pic.width / pic.height;
  326. ctx.drawImage(pic.path, (340 - picW * lv) / 2 + 30, 95, picW * lv, picW);
  327. } else {
  328. lv = pic.height / pic.width;
  329. ctx.drawImage(pic.path, 30, (340 - picW * lv) / 2 + 95, picW, picW * lv);
  330. }
  331. ctx.drawImage(code.path, 250, 445, 120, 120);
  332. let tlength = '';
  333. if (that.product.mode == 2) {
  334. ctx.fillStyle = '#FD3B31';
  335. ctx.font = "20px PingFang SC";
  336. ctx.fillText(that.product.integral?that.product.integral:'0', 30, 470);
  337. if ((that.product.integral?that.product.integral:'0').toString().length > 2) {
  338. ctx.fillStyle = '#FD3B31';
  339. ctx.font = "14px PingFang SC";
  340. ctx.fillText('积分', 80, 470);
  341. if(that.product.salePrice){
  342. ctx.fillStyle = '#FD3B31';
  343. ctx.font = "14px PingFang SC";
  344. ctx.fillText('+¥', 90, 470);
  345. ctx.fillStyle = '#FD3B31';
  346. ctx.font = "20px PingFang SC";
  347. ctx.fillText(that.product.salePrice?that.product.salePrice:'0', 100, 470);
  348. }
  349. } else {
  350. ctx.fillStyle = '#FD3B31';
  351. ctx.font = "14px PingFang SC";
  352. ctx.fillText('积分', 58, 470);
  353. if(that.product.salePrice){
  354. ctx.fillStyle = '#FD3B31';
  355. ctx.font = "14px PingFang SC";
  356. ctx.fillText('+¥', 85, 470);
  357. ctx.fillStyle = '#FD3B31';
  358. ctx.font = "20px PingFang SC";
  359. ctx.fillText(that.product.salePrice?that.product.salePrice:'0', 110, 470);
  360. }
  361. }
  362. }
  363. if (that.product.salePrice && that.product.mode != 2) {
  364. ctx.fillStyle = '#FD3B31';
  365. ctx.font = "16px PingFang SC";
  366. ctx.fillText('¥', 30, 470);
  367. ctx.fillStyle = '#FD3B31';
  368. ctx.font = "20px PingFang SC";
  369. ctx.fillText(that.product.salePrice, 50, 470);
  370. ctx.fillStyle = '#999';
  371. ctx.font = "14px PingFang SC"; // console.log(that.data.product.salePrice.toString().length)
  372. if (that.product.salePrice.toString().length > 2) {
  373. ctx.fillStyle = '#999';
  374. ctx.font = "14px PingFang SC";
  375. ctx.fillText('¥' + that.product.marketPrice, 100, 470);
  376. let text = '¥' + that.product.marketPrice;
  377. tlength = text.length;
  378. ctx.beginPath();
  379. ctx.moveTo(100, 465);
  380. ctx.lineTo(100 + tlength * 11, 465);
  381. } else {
  382. ctx.fillStyle = '#999';
  383. ctx.font = "14px PingFang SC";
  384. ctx.fillText('¥' + that.product.marketPrice, 80, 470);
  385. let text = '¥' + that.product.marketPrice;
  386. tlength = text.length;
  387. ctx.beginPath();
  388. ctx.moveTo(80, 465);
  389. ctx.lineTo(80 + tlength * 11, 465);
  390. }
  391. ctx.lineWidth = 1; //线的宽度
  392. ctx.strokeStyle = '#999'; //线的颜色
  393. ctx.stroke();
  394. }
  395. ctx.fillStyle = '#333';
  396. ctx.font = "16px PingFang SC";
  397. let title = that.product.title;
  398. // console.log(title);
  399. if (title) {
  400. for (let i = 0; i < title.length / 14 && i < 2; i++) {
  401. ctx.restore();
  402. if (i == 1) {
  403. if (title.length > 27) {
  404. ctx.fillStyle = '#333';
  405. ctx.font = "16px PingFang SC";
  406. ctx.fillText(title.substring(i * 14, (i + 1) * 13) + '...', 30, 505 + i * 22);
  407. } else {
  408. ctx.fillStyle = '#333';
  409. ctx.font = "16px PingFang SC";
  410. ctx.fillText(title.substring(i * 14, (i + 1) * 13), 30, 505 + i * 22);
  411. }
  412. } else {
  413. ctx.fillStyle = '#333';
  414. ctx.font = "16px PingFang SC";
  415. ctx.fillText(title.substring(i * 14, (i + 1) * 13), 30, 505 + i * 22);
  416. }
  417. }
  418. }
  419. if (that.product.share_img != undefined && this.aType == 6) {
  420. ctx.font = "20px PingFang SC";
  421. ctx.fillStyle = '#333333';
  422. let title = that.product.anchor_name + '的直播间';
  423. if (title) {
  424. for (let i = 0; i < title.length / 14 && i < 2; i++) {
  425. ctx.restore();
  426. ctx.fillText(title.substring(i * 14, (i + 1) * 13), 30, 470 + i * 22);
  427. }
  428. }
  429. ctx.fillStyle = '#333';
  430. ctx.font = "15px PingFang SC";
  431. ctx.fillText(that.product.month + that.product.hour + '开播', 30, 500);
  432. } //用户头像
  433. ctx.save();
  434. ctx.beginPath();
  435. ctx.arc(40, 40, 25, 0, 2 * Math.PI, true); //画一个圆形裁剪区域
  436. ctx.clip(); //裁剪
  437. ctx.drawImage(avatar && avatar.path ? avatar.path : "/static/pages/images/userimg.png", 15, 15, 50, 50); //绘制图片
  438. // ctx.stroke()
  439. // 完成作画
  440. ctx.draw(false, function() {
  441. uni.canvasToTempFilePath({
  442. canvasId: 'share',
  443. success: function(res) {
  444. success.call(this, res.tempFilePath);
  445. },
  446. fail: function(res) {
  447. // console.log(res);
  448. }
  449. }, that);
  450. });
  451. });
  452. },
  453. /**
  454. *
  455. * @param {CanvasContext} ctx canvas上下文
  456. * @param {number} x 圆角矩形选区的左上角 x坐标
  457. * @param {number} y 圆角矩形选区的左上角 y坐标
  458. * @param {number} w 圆角矩形选区的宽度
  459. * @param {number} h 圆角矩形选区的高度
  460. * @param {number} r 圆角的半径
  461. */
  462. roundRect: function(ctx, x, y, w, h, r) {
  463. // 开始绘制
  464. ctx.beginPath(); // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  465. // 这里是使用 fill 还是 stroke都可以,二选一即可
  466. ctx.setFillStyle('white'); // ctx.setStrokeStyle('transparent')
  467. // 左上角
  468. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); // border-top
  469. ctx.moveTo(x + r, y);
  470. ctx.lineTo(x + w - r, y);
  471. ctx.lineTo(x + w, y + r); // 右上角
  472. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); // border-right
  473. ctx.lineTo(x + w, y + h - r);
  474. ctx.lineTo(x + w - r, y + h); // 右下角
  475. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); // border-bottom
  476. ctx.lineTo(x + r, y + h);
  477. ctx.lineTo(x, y + h - r); // 左下角
  478. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); // border-left
  479. ctx.lineTo(x, y + r);
  480. ctx.lineTo(x + r, y); // 这里是使用 fill 还是 stroke都可以,二选一即可,但是需要与上面对应
  481. ctx.fill(); // ctx.stroke()
  482. ctx.closePath(); // 剪切
  483. ctx.clip();
  484. }
  485. }
  486. };
  487. </script>
  488. <style>
  489. @import "./share.css";
  490. </style>