share.vue 9.5 KB

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