share.vue 16 KB

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