Commit 19e8ffeffba0b178a929f42d45fd5f8cc49a51d4

Authored by 前端开发-陈颖阳
1 parent 47e9534a

直播海报分享优化显示

packageA/pages/liveStream/liveStream.js
... ... @@ -46,7 +46,8 @@ Page({
46 46 screenWidth: 0,
47 47 canvasHidden: 1,
48 48 gid: '',
49   - shareImgPath: ''
  49 + shareImgPath: '',
  50 + shartitle:""
50 51 },
51 52 // list: [],
52 53 // live: {
... ... @@ -335,13 +336,15 @@ Page({
335 336 // 分享操作表
336 337 clickShare: function (e) {
337 338 var sharimg = e.currentTarget.dataset.sharimg
  339 + var shartitle = e.currentTarget.dataset.shartitle
338 340 if(!getApp().globalData.user_id){
339 341 getApp().goto("/packageE/pages/togoin/togoin");
340 342 return false;
341 343 }
342 344 this.setData({
343 345 showActionSheet: !this.data.showActionSheet,
344   - sharimg : sharimg
  346 + sharimg : sharimg,
  347 + shartitle:shartitle
345 348 })
346 349 },
347 350  
... ... @@ -406,7 +409,7 @@ Page({
406 409 if(!this.data.coverImg) {
407 410  
408 411 // 请求主图
409   - await that.getImageInfo(that.data['shareimg']).then(res => {
  412 + await that.getImageInfo(that.data.sharimg).then(res => {
410 413 that.setData({
411 414 coverImg: res.path,
412 415 coverImgWidth: res.width,
... ... @@ -446,7 +449,7 @@ Page({
446 449  
447 450  
448 451 // 数据准备
449   - const title = this.data.details.name;
  452 + const title = this.data.shartitle;
450 453 // const headImg = app.globalData.userInfo['head_pic'];
451 454  
452 455 // console.log('----------------v', headImg);
... ... @@ -573,6 +576,68 @@ Page({
573 576  
574 577 },
575 578  
  579 +
  580 +
  581 + imageResize(imgWidth, imgHeight) {
  582 + var imageSize = {};
  583 + var originalWidth = imgWidth;//图片原始宽
  584 + var originalHeight = imgHeight;//图片原始高
  585 + var originalScale = originalHeight/originalWidth;//图片高宽比
  586 + // console.log('originalWidth: ' + originalWidth)
  587 + // console.log('originalHeight: ' + originalHeight)
  588 +
  589 + //获取屏幕宽高
  590 + var winWidth = 480;
  591 + var winHeight = 380;
  592 + var winScale = winHeight/winWidth;//屏幕高宽比
  593 + // console.log('windowWidth: ' + winWidth)
  594 + // console.log('windowHeight: ' + winHeight)
  595 + if(originalScale < winScale){//图片高宽比小于屏幕高宽比
  596 + //图片缩放后的宽为屏幕宽
  597 + imageSize.imageWidth = winWidth;
  598 + imageSize.imageHeight = (winWidth * originalHeight) / originalWidth;
  599 + }else{//图片高宽比大于屏幕高宽比
  600 + //图片缩放后的高为屏幕高
  601 + imageSize.imageHeight = winHeight;
  602 + imageSize.imageWidth = (winHeight * originalWidth) / originalHeight;
  603 + }
  604 + console.log('缩放后的宽: ' + imageSize.imageWidth)
  605 + console.log('缩放后的高: ' + imageSize.imageHeight)
  606 + return imageSize;
  607 + },
  608 +
  609 +
  610 +//文本换行 参数:1、canvas对象,2、文本 3、距离左侧的距离 4、距离顶部的距离 5、6、文本的宽度
  611 +drawText: function(ctx, str, leftWidth, initHeight, titleHeight, canvasWidth, unit) {
  612 + var lineWidth = 0;
  613 + var lastSubStrIndex = 0; //每次开始截取的字符串的索引
  614 + var han = 0;
  615 + for (let i = 0; i < str.length; i++) {
  616 + if (han == 2) return;
  617 + //lineWidth += ctx.measureText(str[i]).width;
  618 + lineWidth += ut.measureText(str[i], 21.3 * unit);
  619 + if (lineWidth > canvasWidth) {
  620 + han++;
  621 +
  622 + if (han == 2) {
  623 + ctx.textAlign = 'justify';
  624 + ctx.fillText(str.substring(lastSubStrIndex, i) + '...', leftWidth, initHeight); //绘制截取部分
  625 + } else {
  626 + ctx.textAlign = 'justify';
  627 + ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight);
  628 + }
  629 + initHeight += 35; //22为字体的高度
  630 + lineWidth = 0;
  631 + lastSubStrIndex = i;
  632 + titleHeight += 20;
  633 + }
  634 + if (i == str.length - 1) { //绘制剩余部分
  635 + ctx.textAlign = 'justify';
  636 + ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
  637 + }
  638 + }
  639 +},
  640 +
576 641 /**
577 642 * 页面相关事件处理函数--监听用户下拉动作
578 643 */
... ... @@ -694,7 +759,145 @@ Page({
694 759 });
695 760  
696 761 return p;
697   - },
  762 + },
  763 +
  764 +
  765 + // 保存图片到手机
  766 + onSaveToPhone() {
  767 + var self = this;
  768 + // 获取用户的当前设置,返回值中有小程序已经向用户请求过的权限
  769 + this.getSetting().then((res) => {
  770 + // 判断用户是否授权了保存到相册的权限,如果没有发起授权
  771 + if (!res.authSetting['scope.writePhotosAlbum']) {
  772 + this.authorize().then(() => {
  773 + // 同意授权后保存下载文件
  774 + this.saveImageToPhotosAlbum(self.data.canvasToImgPath)
  775 + })
  776 + } else {
  777 + // 如果已经授权,保存下载文件
  778 + this.saveImageToPhotosAlbum(self.data.canvasToImgPath)
  779 + }
  780 + })
  781 + },
  782 +
  783 + //打开设置,引导用户授权
  784 + onOpenSetting() {
  785 + wx.openSetting({
  786 + success:(res) => {
  787 + // console.log(res.authSetting)
  788 + }
  789 + })
  790 + },
  791 +
  792 + // 获取用户已经授予了哪些权限
  793 + getSetting() {
  794 + return new Promise((resolve, reject) => {
  795 + wx.getSetting({
  796 + success: res => {
  797 + resolve(res)
  798 + }
  799 + })
  800 + })
  801 + },
  802 +
  803 + // 发起首次授权请求
  804 + authorize() {
  805 + // isFirst 用来记录是否为首次发起授权,
  806 + // 如果首次授权拒绝后,isFirst赋值为1
  807 + let isFirst = wx.getStorageSync('isFirst') || 0;
  808 + return new Promise((resolve, reject) => {
  809 + wx.authorize({
  810 + scope: 'scope.writePhotosAlbum',
  811 + // 同意授权
  812 + success: () => {
  813 + resolve();
  814 + },
  815 + // 拒绝授权,这里是用户拒绝授权后的回调
  816 + fail: res => {
  817 + if(isFirst === 0) {
  818 + wx.setStorageSync('isFirst', 1);
  819 + wx.showToast({
  820 + title: '保存失败',
  821 + icon: 'none',
  822 + duration: 1000
  823 + })
  824 + } else {
  825 + this.showModal();
  826 + }
  827 + console.log('拒绝授权');
  828 + reject();
  829 + }
  830 + })
  831 + })
  832 + },
  833 +
  834 + //保存下载文件
  835 + savedownloadFile(img) {
  836 + this.downLoadFile(img).then((res) => {
  837 + return this.saveImageToPhotosAlbum(res.tempFilePath)
  838 + }).then(() => {
  839 + // resolve()
  840 + })
  841 + },
  842 +
  843 + //单文件下载(下载文件资源到本地),客户端直接发起一个 HTTPS GET 请求,返回文件的本地临时路径。
  844 + downLoadFile(img) {
  845 + var self = this;
  846 + return new Promise((resolve, reject) => {
  847 + wx.showLoading({
  848 + title: '保存中...',
  849 + mask: true,
  850 + });
  851 + wx.downloadFile({
  852 + url: img,
  853 + success: (res) => {
  854 + resolve(res);
  855 + }
  856 + })
  857 + })
  858 + },
  859 +
  860 + // 保存图片到系统相册
  861 + saveImageToPhotosAlbum(saveUrl) {
  862 + var self = this;
  863 + return new Promise((resolve, reject) => {
  864 + wx.saveImageToPhotosAlbum({
  865 + filePath: saveUrl,
  866 + success: (res) => {
  867 + wx.showToast({
  868 + title: '保存成功',
  869 + duration: 1000,
  870 + });
  871 + self.setData({
  872 + showPlaybill: 'true'
  873 + });
  874 + resolve();
  875 + },
  876 + fail: () => {
  877 + wx.showToast({
  878 + title: '保存失败',
  879 + duration: 1000,
  880 + });
  881 + }
  882 + })
  883 + })
  884 + },
  885 +
  886 +
  887 + // 弹出模态框提示用户是否要去设置页授权
  888 + showModal() {
  889 + wx.showModal({
  890 + title: '检测到您没有打开保存到相册的权限,是否前往设置打开?',
  891 + success: (res) => {
  892 + if (res.confirm) {
  893 + console.log('用户点击确定')
  894 + this.onOpenSetting() // 打开设置页面
  895 + } else if (res.cancel) {
  896 + console.log('用户点击取消')
  897 + }
  898 + }
  899 + })
  900 + },
698 901  
699 902  
700 903 /**
... ...
packageA/pages/liveStream/liveStream.wxml
... ... @@ -85,7 +85,7 @@
85 85 {{item['anchor_name']}}</view>
86 86 </view>
87 87 <view style="width: 30%;height: 90rpx;align-items: center;justify-items: center;">
88   - <view bindtap="clickShare" data-sharimg="{{item.share_img}}" class="display: flex;" style="width: 70%;border-radius: 25rpx;border: 1rpx solid #fc6247;height: 46rpx;margin-top: 25rpx;margin-left: 30rpx;">
  88 + <view bindtap="clickShare" data-sharimg="{{item.share_img}}" data-shartitle="{{item.name}}" class="display: flex;" style="width: 70%;border-radius: 25rpx;border: 1rpx solid #fc6247;height: 46rpx;margin-top: 25rpx;margin-left: 30rpx;">
89 89 <view class="share-image"></view>
90 90 <view class="fs26" style="color: #fc6247;margin-left: 10rpx;line-height: 46rpx;"> 分享 </view>
91 91 </view>
... ...
packageA/pages/liveStream/liveStream.wxss
... ... @@ -257,6 +257,50 @@ subscribe {
257 257 }
258 258  
259 259 .share-actionSheet.active ~ .mask,
  260 +
  261 +.mask {
  262 + display: none;
  263 + background-color: rgba(0,0,0,.7);
  264 + position: fixed;
  265 + top: 0;
  266 + bottom: 0;
  267 + left: 0;
  268 + right: 0;
  269 +}
  270 +.playbill {
  271 + position: fixed;
  272 + left: 50%;
  273 + top: 50%;
  274 + transform: translate(-50%, -50%);
  275 + z-index: 9999;
  276 + background: none;
  277 +}
  278 +.share-actionSheet.active ~ .mask,
260 279 .playbill.active ~ .mask {
261 280 display: block;
262   -}
263 281 \ No newline at end of file
  282 +}
  283 +.playbill-pic {
  284 + display: block;
  285 + width: 560rpx;
  286 + height: auto;
  287 + margin: 0 auto 26rpx;
  288 + border-radius: 10rpx;
  289 + overflow: hidden;
  290 +}
  291 +
  292 +.playbill-save {
  293 + width: 560rpx;
  294 + height: 100rpx;
  295 + line-height: 100rpx;
  296 + background-color: #f23030;
  297 + color: #fff;
  298 + font-size: 36rpx;
  299 + text-align: center;
  300 + margin: 0 auto;
  301 +}
  302 +.playbill-save image {
  303 + width: 44rpx;
  304 + height: 36rpx;
  305 + margin-right: 23rpx;
  306 +}
  307 +
... ...