livedposterDia.vue 13.4 KB
Newer Older
李嘉林 committed
1 2 3 4 5 6 7 8
<template>
  <div class="livedposterDia">
    <van-popup
      :show="showPosterDia"
      custom-style="width:100vw;height:100vh;background:transparent;"
      :close-on-click-overlay="true"
      @close="closeDia"
    >
程智春 committed
9 10
      <div class="posterWrap" @click.stop="closeDia">
        <div class="canvas-wrap" >
程智春 committed
11 12 13 14
          <canvas canvas-id="posterCanvas" class="myCanvas"></canvas>
          <div v-if="!posterStatus" class="masker">加载中...</div>
        </div>
        
程智春 committed
15
        <div class="saveBtn" @click.stop="getImg">点击保存海报</div>
李嘉林 committed
16 17 18 19 20 21
      </div>
    </van-popup>
  </div>
</template>

<script type="text/ecmascript-6">
李嘉林 committed
22
import live from "@/api/live";
程智春 committed
23
import { DFSImg } from '../../utils/common.js'
李嘉林 committed
24
export default {
程智春 committed
25
  props: ["showPosterDia", "posterInfo","userInfo","infoIsComplete"],
李嘉林 committed
26 27 28 29 30 31 32 33
  name: "",
  data() {
    return {
      InfoSync: {},
      liveId: 0,
      params: {},
      // 海报中信息
      info: {
李嘉林 committed
34
        posterCoverUrl: "",
李嘉林 committed
35
        livedStatus: 1, //直播状态  0未开播 1一开播
程智春 committed
36
        livedTime: "",
李嘉林 committed
37 38 39 40
        livedTitle: "",
        nickName: "",
        avatarUrl: "",
        miniCode: ""
程智春 committed
41 42
      },
      posterStatus : false,
程智春 committed
43 44

      tempFilePath : '',
李嘉林 committed
45 46 47 48 49
    };
  },
  components: {},
  computed: {},
  onLoad(options) {
程智春 committed
50
    this.posterStatus = false;
程智春 committed
51 52 53 54 55 56 57 58 59 60
    if(options.params){
      let params = JSON.parse(options.params);
      this.liveId = params.liveId;  
    }else if(options.scene){
      var scene = decodeURIComponent(options.scene); //参数二维码传递过来的参数
      console.log(scene)
      let sceneInfo = scene.split(',')
      console.log('sceneInfo',sceneInfo)
      this.liveId = sceneInfo[0].split('=')[1]
    }
程智春 committed
61
    console.log(this.userInfo)
李嘉林 committed
62 63
    Object.assign(this.info, this.info, this.posterInfo);
    console.log(this.info, "--------------------35");
程智春 committed
64
    console.log(this.$store.state)
李嘉林 committed
65 66 67 68 69 70 71 72 73
    this.init();
  },
  created() {},
  mounted() {},
  methods: {
    closeDia() {
      this.$emit("closePosterDia");
    },
    init() {
程智春 committed
74
      wx.hideLoading();
李嘉林 committed
75 76 77
      // wx.showLoading({
      //   title: "生成中..."
      // });
程智春 committed
78 79
      this.$set(this.info,'avatarUrl',this.userInfo.headPortraitUrl)
      // this.info.avatarUrl = this.userInfo.headPortraitUrl
李嘉林 committed
80
      let _this = this;
程智春 committed
81 82 83 84 85 86 87 88
      if(this.infoIsComplete){
        wx.getUserInfo({
          //获取微信用户信息
          success: function(res) {
            (_this.info.nickName = res.userInfo.nickName),
              _this.initPoster(_this.info);
          },
          fail: function(res) {
李嘉林 committed
89
            _this.initPoster(_this.info);
程智春 committed
90 91 92 93
          }
        });
      }
      
李嘉林 committed
94 95 96 97 98
    },
    initPoster(info) {
      console.log(info, "---------------------71");
      const InfoSync = wx.getSystemInfoSync();
      this.InfoSync = InfoSync;
李嘉林 committed
99 100
      let bili = InfoSync.windowWidth / 375 * 1;
      // let bili = 1;
李嘉林 committed
101 102 103 104
      const ctx = wx.createCanvasContext("posterCanvas");
      ctx.setFillStyle("#fff");
      ctx.fillRect(0, 0, 310 * bili, 520 * bili);

程智春 committed
105 106
      console.log('info.posterCoverUrl',info.posterCoverUrl)

李嘉林 committed
107
      //封面
李嘉林 committed
108
      this.saveThe(info.posterCoverUrl, path => {
程智春 committed
109 110
        console.log(this.info)
        console.log('0000000000000000000000封面')
李嘉林 committed
111 112 113 114 115 116
        ctx.drawImage(path, 0, 0, 310 * bili, 370 * bili);
        ctx.save();
        // 微信昵称
        ctx.setTextAlign("left");
        ctx.setFillStyle("#3F2F00");
        ctx.setFontSize(15);
李嘉林 committed
117
        ctx.fillText(info.nickName, 70 * bili, 410 * bili);
李嘉林 committed
118

程智春 committed
119 120
        ctx.setFillStyle("#FDC330");
        ctx.fillRect(0, 330 * bili, 310 * bili, 40 * bili);
程智春 committed
121
   
程智春 committed
122
        if(this.info.livedStatus == 0){
程智春 committed
123
          console.log('直播时间',this.info.livedTime)
程智春 committed
124 125
          this.saveThe('https://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/2a6cbdf0-91ca-4993-9ec5-963cb30ba9b8.png',path => {
            ctx.drawImage(path, 160 * bili , 340 * bili, 18 * bili, 18 * bili);
程智春 committed
126 127 128 129
            ctx.save()
            ctx.setTextAlign("left");
            ctx.setFillStyle("#3F2F00");
            ctx.setFontSize(14);
程智春 committed
130
            ctx.fillText(this.info.livedTime, 184 * bili , 355 * bili);
程智春 committed
131

程智春 committed
132 133 134 135 136 137
            this.saveThe('https://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/cc6db4d4-55bb-4add-a9ba-5733499aa5d0.png',path => {
              ctx.drawImage(path, 20 * bili , 340 * bili, 20 * bili, 18 * bili);
              ctx.save()
              ctx.setTextAlign("left");
              ctx.setFillStyle("#3F2F00");
              ctx.setFontSize(14);
程智春 committed
138

程智春 committed
139
              
程智春 committed
140

程智春 committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
              let liveText = '';
              if(this.info.livedStatus == 0){
                liveText = '直播预告'
              }else if(this.info.livedStatus == 1 || this.info.livedStatus == 3){
                liveText = '直播中'
              }else{
                liveText = '直播结束'
              }
              console.log('直播状态',liveText)
              ctx.fillText(liveText, 48 * bili , 355 * bili);   
              
              
              ctx.setTextAlign("left");
              ctx.setFillStyle("#3F2F00");
              ctx.setFontSize(14);
              ctx.fillText("邀你一起看直播", 10 * bili, 450 * bili);
              // 直播名
              ctx.setTextAlign("left");
              ctx.setFillStyle("#3F2F00");
              ctx.setFontSize(18);
              ctx.fillText(info.livedTitle, 10 * bili, 500 * bili);
              //圆形头像框
              ctx.setStrokeStyle("rgba(0,0,0,0)");
              ctx.arc(36 * bili, 400 * bili, 25 * bili, 0, 2 * Math.PI);
              ctx.fill();
              this.saveThe(info.qrCode, path => {
                ctx.drawImage(path, 200 * bili, 390 * bili, 100 * bili, 100 * bili);
                ctx.save();
                //头像
                ctx.clip();
                this.saveThe(info.avatarUrl,path => {
                  ctx.drawImage(path, 10 * bili, 370 * bili, 54 * bili, 54 * bili);
                  ctx.save();
程智春 committed
174

程智春 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                  ctx.stroke();
                  // ctx.draw(false,this.drawImageHanlder());
                  ctx.draw();
                
                  this.posterStatus = true;
                  console.log("--------海报生成成功");
                })
              });
              
            })
          })
        } else{
          this.saveThe('https://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/png/cc6db4d4-55bb-4add-a9ba-5733499aa5d0.png',path => {
            ctx.drawImage(path, 20 * bili , 340 * bili, 20 * bili, 18 * bili);
            ctx.save()
            ctx.setTextAlign("left");
            ctx.setFillStyle("#3F2F00");
            ctx.setFontSize(14);
程智春 committed
193

程智春 committed
194
            
程智春 committed
195

程智春 committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            let liveText = '';
            if(this.info.livedStatus == 0){
              liveText = '直播预告'
            }else if(this.info.livedStatus == 1 || this.info.livedStatus == 3){
              liveText = '直播中'
            }else{
              liveText = '直播结束'
            }
            console.log('直播状态',liveText)
            ctx.fillText(liveText, 48 * bili , 355 * bili);   
            
            
            ctx.setTextAlign("left");
            ctx.setFillStyle("#3F2F00");
            ctx.setFontSize(14);
            ctx.fillText("邀你一起看直播", 10 * bili, 450 * bili);
            // 直播名
            ctx.setTextAlign("left");
            ctx.setFillStyle("#3F2F00");
            ctx.setFontSize(18);
            ctx.fillText(info.livedTitle, 10 * bili, 500 * bili);
            //圆形头像框
            ctx.setStrokeStyle("rgba(0,0,0,0)");
            ctx.arc(36 * bili, 400 * bili, 25 * bili, 0, 2 * Math.PI);
            ctx.fill();
            this.saveThe(info.qrCode, path => {
              ctx.drawImage(path, 200 * bili, 390 * bili, 100 * bili, 100 * bili);
              ctx.save();
              //头像
              ctx.clip();
              this.saveThe(info.avatarUrl,path => {
                ctx.drawImage(path, 10 * bili, 370 * bili, 54 * bili, 54 * bili);
                ctx.save();

                ctx.stroke();
                // ctx.draw(false,this.drawImageHanlder());
                ctx.draw();
              
                this.posterStatus = true;
                console.log("--------海报生成成功");
              })
            });
            
程智春 committed
239
          })
程智春 committed
240 241 242 243 244
        } 

        

        
李嘉林 committed
245 246 247 248 249 250 251 252 253
      });
    },
    // 小程序需要将图片下载下来,然后才能绘制到画布上
    saveThe(url, callback) {
      wx.getImageInfo({
        src: url, //服务器返回的图片地址
        success: res => {
          callback(res.path);
        },
程智春 committed
254
        fail: function(res) {
程智春 committed
255
          console.log('saveTheFail ___________________________',url + '------url-----',res)
程智春 committed
256
        }
李嘉林 committed
257 258 259
      });
    },
    getImg() {
程智春 committed
260 261 262 263 264 265 266 267
      if(this.posterStatus){
        let query = {
          liveBroadcastId: this.liveId,
          shareType: "HB"
        };
        live.addShareRecord(query).then();
        this.handleSave();
      }
李嘉林 committed
268 269 270 271
    },
    // 点击保存时,将画布生成海报
    handleSave() {
      var that = this;
程智春 committed
272 273 274 275 276

      wx.showLoading({
        title: "正在保存...",
        mask: true
      });
程智春 committed
277
      
李嘉林 committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      wx.getSetting({
        success(res) {
          if (res.authSetting["scope.writePhotosAlbum"]) {
            that.saveImg();
          } else if (res.authSetting["scope.writePhotosAlbum"] === undefined) {
            wx.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                that.saveImg();
              },
              fail() {
                wx.hideLoading();
                wx.showToast({
                  title: "您没有授权,无法保存到相册",
                  icon: "none"
                });
              }
            });
          } else {
            wx.openSetting({
              success(res) {
                if (res.authSetting["scope.writePhotosAlbum"]) {
                  that.saveImg();
                } else {
                  wx.showToast({
                    title: "您没有授权,无法保存到相册",
                    icon: "none"
                  });
                }
              }
            });
          }
        },
        fail: err => {
          wx.hideLoading();
          wx.showToast({
程智春 committed
314
            title: "出现了错误,请稍后再试11111",
李嘉林 committed
315 316 317 318 319 320
            icon: "none"
          });
        }
      });
    },
    saveImg() {
程智春 committed
321 322 323 324 325


      let _this = this;


李嘉林 committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
      // 按照设备比例去计算图片和画布尺寸
      let bili = this.InfoSync.windowWidth / 375 * 1;
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 310 * bili,
        height: 520 * bili,
        destWidth: 310 * bili * this.InfoSync.pixelRatio,
        destHeight: 520 * bili * this.InfoSync.pixelRatio,
        fileType: "png",
        quality: 1,
        canvasId: "posterCanvas",
        success: function(res) {
          wx.hideLoading();
          var tempFilePath = res.tempFilePath;
          // 需要权限
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
程智春 committed
345
              _this.$emit("closePosterDia");
李嘉林 committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
              wx.showModal({
                content: "图片已保存到相册,赶紧晒一下吧~",
                showCancel: false,
                confirmText: "好的",
                confirmColor: "#333"
              });
            },
            fail: function(res) {
              wx.hideLoading();
              wx.showToast({
                title: "没有相册权限",
                icon: "none",
                duration: 2000
              });
            }
          });
        },
        fail: err => {
程智春 committed
364 365

          console.log(err,'保存相册出错');
李嘉林 committed
366 367
          wx.hideLoading();
          wx.showToast({
程智春 committed
368
            title: "出现了错误,请稍后再试2222",
李嘉林 committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
            icon: "none"
          });
        }
      });
    },
    // 发送给朋友,以图片的方式,先生成临时图片地址,然后调用微信api显示转发
    handleShowImg() {
      let bili = this.InfoSync.windowWidth / 375 * 1;
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 310 * bili,
        height: 520 * bili,
        destWidth: 310 * bili * this.InfoSync.pixelRatio,
        destHeight: 520 * bili * this.InfoSync.pixelRatio,
        fileType: "png",
        quality: 1,
        canvasId: "posterCanvas",
        success: function(res) {
          wx.hideLoading();
          wx.previewImage({
            urls: [res.tempFilePath],
            current: res.tempFilePath
          });
        },
        fail: err => {
          wx.hideLoading();
          wx.showToast({
            title: "出现了错误,请稍后再试",
            icon: "none"
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.livedposterDia {
  .posterWrap {
    min-height: calc(100vh - 80px);
    padding-top: 80px;
    // background-color: #f1f1f1;
程智春 committed
413
    .canvas-wrap{
李嘉林 committed
414 415 416 417
      width: 310px;
      height: 520px;
      margin: 0 auto;
      background: #fff;
程智春 committed
418
      position: relative;
程智春 committed
419 420
      overflow: hidden;
      border-radius: 5px;
程智春 committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      .myCanvas {
        width: 100%;
        height: 100%;
        background: #fff;
      }
      .masker{
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background-color: white;
        text-align: center;
        line-height: 520px;
        font-size: 16px;
        color: #666;
      }
李嘉林 committed
438
    }
程智春 committed
439
    
李嘉林 committed
440 441 442 443 444 445 446 447 448 449
    .saveBtn {
      margin: 0 auto;
      margin-top: 20px;
      background: #f82f31;
      width: 50vw;
      height: 40px;
      border-radius: 40px;
      text-align: center;
      line-height: 40px;
      color: #fff;
李嘉林 committed
450 451 452 453
    }
  }
}
</style>