share.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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:460px;" 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="/static/pages/images/close.png" class="close" @tap="showPoster"></image>
  24. <view class="binfos">
  25. <view class="bimgbox">
  26. <image :src="product.fileUrl?product.fileUrl:product.pic" mode="aspectFit" class="bimg"></image>
  27. </view>
  28. <view class="texts">
  29. <view class="binfo">
  30. <view class="btit">{{product.title}}</view>
  31. <view class="bprice dflex">
  32. <text>会员码</text>{{userInfo.userCode}}
  33. </view>
  34. </view>
  35. <view class="code">
  36. <image :src="codeUrl"></image>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="save" @tap="saveImage">保存图片</view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. const req = require("../../../utils/request.js");
  47. export default {
  48. data() {
  49. return {
  50. // canvasW: 0,
  51. // canvasH: 0,
  52. picUrlss: req.public.picUrls,
  53. imgUrl: '',
  54. userInfo: {},
  55. isVideoScene: false
  56. };
  57. },
  58. components: {},
  59. props: {
  60. hideToast: {
  61. type: Boolean,
  62. default: true
  63. },
  64. hidePoster: {
  65. type: Boolean,
  66. default: true
  67. },
  68. product: Object,
  69. codeUrl: String,
  70. bgUrl: String,
  71. aType: Number,
  72. canvasW: Number,
  73. canvasH: Number,
  74. address: {
  75. type: String,
  76. default: '湖南省娄底市经济技术开发区大埠桥办事处南阳村'
  77. },
  78. },
  79. onReady() {
  80. // console.log(this.hideToast)
  81. },
  82. onHide() {
  83. // console.log(this.hideToast)
  84. },
  85. mounted() {
  86. this.isVideoScene = req.getStorage('isVideoScene')
  87. // console.log(this.hideToast,888888888)
  88. },
  89. methods: {
  90. generatePoster() {
  91. let that = this;
  92. let isShowLoading = false;
  93. let userInfo = req.getStorage('userInfo');
  94. this.setData({
  95. userInfo: userInfo
  96. });
  97. if (!isShowLoading) {
  98. uni.showLoading({
  99. title: '生成中…',
  100. mask: true
  101. });
  102. isShowLoading = true;
  103. }
  104. this.generate(imgUrl => {
  105. // console.log(isShowLoading);
  106. if (isShowLoading) {
  107. uni.hideLoading();
  108. isShowLoading = false;
  109. }
  110. that.setData({
  111. imgUrl: imgUrl,
  112. hideToast: true,
  113. hidePoster: false
  114. });
  115. });
  116. },
  117. showPoster() {
  118. //隐藏海报
  119. this.setData({
  120. hidePoster: true
  121. });
  122. this.$emit('onFather', this.hideToast)
  123. },
  124. cancelPopup() {
  125. this.setData({
  126. hideToast: true
  127. });
  128. this.$emit('onFather', this.hideToast)
  129. },
  130. isAuth(fun) {
  131. if (!uni.saveImageToPhotosAlbum) {
  132. uni.showModal({
  133. title: '提示',
  134. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  135. });
  136. return;
  137. }
  138. ; //可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
  139. // #ifndef H5
  140. uni.getSetting({
  141. success(res) {
  142. // console.log(res);
  143. if (!res.authSetting['scope.writePhotosAlbum']) {
  144. // 接口调用询问
  145. uni.authorize({
  146. scope: 'scope.writePhotosAlbum',
  147. success() {
  148. // console.log("2-授权《保存图片》权限成功");
  149. fun(true);
  150. },
  151. fail() {
  152. fun(false);
  153. }
  154. });
  155. } else {
  156. // 拒绝授权
  157. fun(true);
  158. }
  159. },
  160. fail(res) {
  161. fun(false);
  162. }
  163. });
  164. // #endif
  165. },
  166. saveImage() {
  167. let that = this;
  168. that.isAuth(success => {
  169. if (success) {
  170. uni.saveImageToPhotosAlbum({
  171. filePath: that.imgUrl,
  172. success(res) {
  173. uni.showToast({
  174. title: '图片保存成功',
  175. icon: 'none',
  176. success() { // that.generate(imgUrl => {
  177. // that.setData({
  178. // imgUrl: imgUrl
  179. // })
  180. // })
  181. },
  182. fail() {
  183. uni.showToast({
  184. title: '图片保存失败'
  185. });
  186. }
  187. });
  188. }
  189. });
  190. }
  191. });
  192. },
  193. getImageInfo(url) {
  194. // console.log(url);
  195. return new Promise((resolve, reject) => {
  196. if (!url) {
  197. resolve();
  198. return false;
  199. }
  200. uni.getImageInfo({
  201. src: url,
  202. success: resolve,
  203. fail: reject
  204. });
  205. });
  206. },
  207. generate(success) {
  208. // console.log(this);
  209. let picPromise = '';
  210. let picUrl = this.product.fileUrl?this.product.fileUrl:this.product.pic;
  211. picPromise = this.getImageInfo(picUrl);
  212. let codePromise;
  213. if (!this.codeUrl) {
  214. return req.msg('二维码生成失败');
  215. } else {
  216. codePromise = this.getImageInfo(this.codeUrl);
  217. }
  218. let that = this;
  219. Promise.all([picPromise, codePromise]).then(([pic, code]) => {
  220. // console.log(pic, avatar, code); // 创建绘图上下文
  221. const ctx = uni.createCanvasContext('share', this); // 绘制背景,填充满整个canvas画布
  222. let width = 400,
  223. height = 460; //默认背景
  224. ctx.setFillStyle('#fff');
  225. ctx.fillRect(0, 0, width, height); // 拼团背景
  226. //白色背景
  227. // ctx.save();
  228. // that.roundRect(ctx, 0, 0, 400, 460, 10);
  229. // ctx.restore();
  230. const picW = 400;
  231. ctx.drawImage(pic.path, 0, 0, 400, 313);
  232. // let lv;
  233. // if (pic.height >= pic.width) {
  234. // lv = pic.width / pic.height;
  235. // ctx.drawImage(pic.path, (400 - picW * lv) / 2, 0, picW * lv, picW);
  236. // } else {
  237. // lv = pic.height / pic.width;
  238. // ctx.drawImage(pic.path, 0, (400 - picW * lv) / 2, picW, picW * lv);
  239. // }
  240. ctx.drawImage(code.path, 265, 328, 120, 120);
  241. ctx.fillStyle = '#000';
  242. ctx.font = "18px PingFang SC";
  243. let title = that.product.title;
  244. // console.log(title);
  245. if (title) {
  246. for (let i = 0; i <= title.length / 16 && i < 2; i++) {
  247. ctx.restore();
  248. if (i == 1) {
  249. if (title.length > 31) {
  250. console.log('...===',title.substring(i * 15, (i + 1) * 15) + '...')
  251. ctx.fillStyle = '#000';
  252. ctx.font = "16px PingFang SC";
  253. ctx.fillText(title.substring(i * 15, (i + 1) * 15) + '...', 15, 368 + i * 22);
  254. } else {
  255. console.log('...===27',title.substring(i * 15, (i + 1) * 15))
  256. ctx.fillStyle = '#000';
  257. ctx.font = "16px PingFang SC";
  258. ctx.fillText(title.substring(i * 15, (i + 1) * 15), 15, 368 + i * 22);
  259. }
  260. } else {
  261. console.log('...===0',title.substring(i * 16, (i + 1) * 15),i)
  262. ctx.fillStyle = '#000';
  263. ctx.font = "16px PingFang SC";
  264. ctx.fillText(title.substring(i * 16, (i + 1) * 15), 15, 368 + i * 22);
  265. }
  266. }
  267. }
  268. ctx.fillStyle = '#999';
  269. ctx.font = "14px PingFang SC";
  270. ctx.fillText('会员码', 15, 422);
  271. ctx.fillStyle = '#333';
  272. ctx.font = "14px PingFang SC";
  273. ctx.fillText(that.userInfo.userCode, 65, 422);
  274. ctx.save();
  275. // ctx.beginPath();
  276. // ctx.arc(40, 40, 25, 0, 2 * Math.PI, true); //画一个圆形裁剪区域
  277. // ctx.clip(); //裁剪
  278. // let config = JSON.parse(req.getStorage('configRes'))
  279. // ctx.drawImage(avatar && avatar.path ? avatar.path : config.CONFIG_PROJECT_LOGO, 15, 15, 50, 50); //绘制图片
  280. ctx.stroke()
  281. // 完成作画
  282. ctx.draw(false, function() {
  283. uni.canvasToTempFilePath({
  284. canvasId: 'share',
  285. success: function(res) {
  286. success.call(this, res.tempFilePath);
  287. },
  288. fail: function(res) {
  289. // console.log(res);
  290. }
  291. }, that);
  292. });
  293. });
  294. },
  295. /**
  296. *
  297. * @param {CanvasContext} ctx canvas上下文
  298. * @param {number} x 圆角矩形选区的左上角 x坐标
  299. * @param {number} y 圆角矩形选区的左上角 y坐标
  300. * @param {number} w 圆角矩形选区的宽度
  301. * @param {number} h 圆角矩形选区的高度
  302. * @param {number} r 圆角的半径
  303. */
  304. roundRect: function(ctx, x, y, w, h, r) {
  305. // 开始绘制
  306. ctx.beginPath(); // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  307. // 这里是使用 fill 还是 stroke都可以,二选一即可
  308. ctx.setFillStyle('white'); // ctx.setStrokeStyle('transparent')
  309. // 左上角
  310. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); // border-top
  311. ctx.moveTo(x + r, y);
  312. ctx.lineTo(x + w - r, y);
  313. ctx.lineTo(x + w, y + r); // 右上角
  314. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); // border-right
  315. ctx.lineTo(x + w, y + h - r);
  316. ctx.lineTo(x + w - r, y + h); // 右下角
  317. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); // border-bottom
  318. ctx.lineTo(x + r, y + h);
  319. ctx.lineTo(x, y + h - r); // 左下角
  320. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); // border-left
  321. ctx.lineTo(x, y + r);
  322. ctx.lineTo(x + r, y); // 这里是使用 fill 还是 stroke都可以,二选一即可,但是需要与上面对应
  323. ctx.fill(); // ctx.stroke()
  324. ctx.closePath(); // 剪切
  325. ctx.clip();
  326. }
  327. }
  328. };
  329. </script>
  330. <style>
  331. @import "./share.css";
  332. </style>