Sanakey
2020-12-11 ff3df3a28cf0698cbee029221f4c9696a49f11e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// pages/liveRoomCreate/liveRoomCreate.js
const utils = require("../../utils/util.js");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentStep: 0,
    type: "0",
    steps: [
      {
        text: "① 基本信息",
        inactiveIcon: "location-o",
        activeIcon: "success",
      },
      {
        text: "② 直播间配置",
        inactiveIcon: "like-o",
        activeIcon: "plus",
      },
    ],
    timeErrorMsg: {
      start: "请选择计划直播开始时间",
      end: "请选择计划直播结束时间",
      tips: "开播时间和结束时间间隔不得短于30分钟",
    },
    screenTypeOptions: [
      { text: "竖屏", value: 0 },
      { text: "横屏", value: 1 },
    ],
    screenType: 0,
    isFeedsPublic: false,
    isShowInHomepage: false,
    isLike: true,
    isComment: true,
    isGoods: true,
    isShare: true,
    isReplay: true,
    isKf: false,
    showQrCode: false,
    qrCodeUrl:"", //https://res.wx.qq.com/op_res/BbVNeczA1XudfjVqCVoKgfuWe7e3aUhokktRVOqf_F0IqS6kYR--atCpVNUUC3zr
    minStartDate: new Date().getTime() + 1000 * 60 * 20,
    maxStartDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 180,
    startDate: "", //new Date().getTime(),
    minEndDate: new Date().getTime() + 1000 * 60 * 40,
    maxEndDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 180,
    endDate: "", //new Date().getTime(),
    formatter(type, value) {
      // console.log(type,value)
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      } else if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
  },
 
  // 切换直播类型
  onChangeType(event) {
    
    this.setData({
      type: event.detail,
    });
  },
  onClickType(event) {
    // console.log('ddd',event);
    const { name } = event.currentTarget.dataset;
    this.setData({
      type: name,
    });
  },
 
  /**
   * 选择时间
   */
  onInputStartDate() {},
  onEnterStartDate(e) {
    let startTime = utils.formatTime(e.detail);
    if (
      this.data.endDate &&
      Math.abs(e.detail - this.data.endDate) > 1000 * 60 * 60 * 24
    ) {
      let date = e.detail + 1000 * 60 * 60 * 24;
      let time = utils.formatTime(date);
      this.setData({
        endDate: date,
        endTime: time,
      });
    }
    if (
      this.data.endDate &&
      Math.abs(e.detail - this.data.endDate) < 1000 * 60 * 30
    ) {
      let date = e.detail + 1000 * 60 * 30;
      let time = utils.formatTime(date);
      this.setData({
        endDate: date,
        endTime: time,
      });
    }
    // if (this.data.endDate && e.detail > this.data.endDate) {
    // }
    console.log("选择开播时间", startTime);
    this.setData({
      startDate: e.detail,
      startTime: startTime,
      maxEndDate: e.detail + 1000 * 60 * 60 * 24,
      minEndDate: e.detail + 1000 * 60 * 30,
    });
    this.handleLiveTime();
    this.onCancelStartDate();
  },
  onCancelStartDate() {
    // console.log(e);
    this.selectComponent("#start-time").toggle();
    this.checkTime("start", "请选择计划直播开始时间");
  },
 
  onInputEndDate() {},
  // onTitleTap(){
  //   if (!this.data.startTime){
  //     wx.showToast({
  //       title: '请先选择开播时间',
  //       duration: 2000
  //     })
  //
  //   }
  // },
 
  onEnterEndDate(e) {
    let endTime = utils.formatTime(e.detail);
    console.log("选择下播时间", endTime);
    this.setData({
      endDate: e.detail,
      endTime: endTime,
      // maxStartDate: e.detail - 1000 * 60 * 30,
      // minStartDate: e.detail + 1000 * 60 * 20,
      // e.detail - 1000 * 60 * 30 + (new Date().getTime() - e.detail)
    });
    // this.handleLiveTime();
    this.onCancelEndDate();
  },
  onCancelEndDate() {
    // console.log(e);
    this.selectComponent("#end-time").toggle();
    this.checkTime("end", "请选择计划直播结束时间");
  },
 
  handleLiveTime() {
    console.log("iii");
 
    let startDate = this.data.startDate;
    let startTime = this.data.startTime;
    let endDate = this.data.endDate;
    let endTime = this.data.endTime;
    if (!startTime || !endTime) return;
    if (startDate - endDate > 0) {
      this.setData({
        startDate: endDate,
        startTime: endTime,
        endDate: startDate,
        endTime: startTime,
      });
    }
  },
 
  checkTime(time, msg) {
    if (this.data[time + "Time"] == null) {
      this.setData({
        [time + "TimeError"]: msg,
      });
      return false;
    }
    this.setData({
      [time + "TimeError"]: "",
    });
    return true;
    // this.handleLiveTime()
  },
 
  onChangeFeeds({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ isFeedsPublic: detail });
  },
  onChangeShowHomepage({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ isShowInHomepage: detail });
  },
  /**
   *
   */
  checkAnchorName(event) {
    let value = event.detail.value;
    console.log(value);
    this.setData({
      anchorName: value,
    });
    this.checkInput("anchorName");
  },
  checkRoomTitle(event) {
    let value = event.detail.value;
    this.setData({
      roomTitle: value,
    });
    this.checkInput("roomTitle");
  },
  checkAnchorWechat(event) {
    let value = event.detail.value;
    this.setData({
      anchorWechat: value,
    });
    this.checkInput("anchorWechat");
  },
  checkSubAnchorWechat(event) {
    let value = event.detail.value;
    this.setData({
      subAnchorWechat: value,
    });
    if (value.length>0) {
      this.checkInput("subAnchorWechat");
    } else{
      this.setData({
        subAnchorWechatError: ""
      });
    }
  },
 
  checkInput(type) {
    let options = {
      anchorName: {
        error: "主播昵称必须为2-15个字",
        regExp: /^.{2,15}$/,
      },
      roomTitle: {
        error: "直播标题必须为3-17个字",
        regExp: /^.{3,17}$/,
      },
      anchorWechat: {
        error: "微信号不合规",
        regExp: /^[a-zA-Z][a-zA-Z\d_-]{5,19}$/,
      },
      subAnchorWechat: {
        error: "微信号不合规",
        regExp: /^[a-zA-Z][a-zA-Z\d_-]{5,19}$/,
      },
    };
    // let msg = {
    //   anchorName: '请输入主播昵称',
    //   anchorName2: '主播昵称必须为2-15个字',
    //   roomTitle: '请输入直播标题',
    //   roomTitle2: '直播标题必须为3-17个字',
    // }
    // // /^[a-zA-Z][a-zA-Z\d_-]{5,19}$/
    // let length = {
    //   anchorName:2,
    //   roomTitle: 3,
    //   anchorWechat
    // }
    let value = this.data[type] || "";
    console.log(value, options[type].regExp.test(value));
    if (!options[type].regExp.test(value)) {
      this.setData({
        [type + "Error"]: options[type].error,
      });
      return false;
    } else {
      this.setData({
        [type + "Error"]: "",
      });
      return true;
    }
  },
 
  /**
   * 检查
   */
 
  async onClickNextStep() {
    let arr = [];
    let doccode = this.data.doccode||'';
    let type = this.data.type;
    let startTime = this.data.startTime;
    let endTime = this.data.endTime;
    let anchorName = this.data.anchorName;
    let roomTitle = this.data.roomTitle;
    let anchorWechat = this.data.anchorWechat;
    let subAnchorWechat = this.data.subAnchorWechat||'';
    let screenType = this.data.screenType;
    let isFeedsPublic = this.data.isFeedsPublic;
    let isShowBannerPhotoOnHomePage = this.data.isShowInHomepage;
    arr.push(this.checkTime("start", "请选择计划直播开始时间"));
    arr.push(this.checkTime("end", "请选择计划直播结束时间"));
    arr.push(this.checkInput("anchorName"));
    arr.push(this.checkInput("roomTitle"));
    arr.push(this.checkInput("anchorWechat"));
    if (subAnchorWechat.length>0) {
      arr.push(this.checkInput("subAnchorWechat"));
    }
 
    // console.log("直播类型", type);
    // console.log("开播时间", startTime);
    // console.log("下播时间", endTime);
    // console.log("主播昵称", anchorName);
    // console.log("房间标题", roomTitle);
    // console.log("主播微信", anchorWechat);
    // console.log("屏幕类型", screenType);
    // console.log("官方收录", isFeedsPublic);
    if (arr.includes(false)) {
      console.log("不能下一步");
      return;
    }
    let data = {
      type,
      startTime,
      endTime,
      anchorName,
      name: roomTitle,
      anchorWechat,
      subAnchorWechat,
      screenType,
      isFeedsPublic,
      doccode,
      isShowBannerPhotoOnHomePage
    };
    console.log("第一步表单数据", data);
 
    let [err, res] = await utils.to(
      wx.$http.request({
        url: "/shopping/live/saveRoomStep1.do",
        data,
      })
    );
    if (err) {
      return;
    }
    console.log("新建房间--第一步", res);
    this.setData({
      currentStep: 1,
      doccode: res.doccode,
      logoUrl:app.globalData.logoUrl,
      shopName:app.globalData.shopName
    });
  },
 
  /**
   * 第二步--上传图片
   */
  uploadImg(imgType) {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
      success: (res) => {
        // console.log(res.data)
        // this.setData({
        //   imageList: res.tempFilePaths,
        //   tempFiles: res.tempFiles
        // })
        let imgList = res.tempFilePaths;
        let files = res.tempFiles;
        let fieldid = imgType;
        let doccode = this.data.doccode;
        console.log(imgList, files, fieldid, doccode);
        // this.setData({
        //   [imgType]: imgList[0],
        // });
        // return;
        wx.uploadFile({
          url: utils.getUrl(
            "/shopping/live/saveRoomAttachment.do?doccode=" +
              doccode +
              "&fieldid=" +
              fieldid
          ),
          // url: app.globalData.reqBase + '/shopping/confirm.do?m=uploadAttachment&doccode='+ doccode +'&rowid='+rowid,
          formData: {
            file: files[0],
          },
          filePath: imgList[0],
          name: "data",
          success: (res) => {
            console.log(res);
            // return;
            if (!utils.requestError(res)) {
              return false;
            }
            // console.log(data);
            this.setData({
              [imgType]: res.data.url,
              [imgType + "Error"]: "",
            });
          },
          fail: function (errmsg) {
            console.log("uploadImage fail, errMsg is", errmsg);
            utils.requestFail(
              errmsg,
              "/shopping/live/saveRoomAttachment.do?doccode="
            );
          },
        });
      },
    });
  },
  uploadShareImg() {
    this.uploadImg("shareImg");
  },
  uploadFeedsImg() {
    this.uploadImg("feedsImg");
  },
  uploadCoverImg() {
    this.uploadImg("coverImg");
  },
  uploadBannerPhoto() {
    this.uploadImg("bannerPhoto");
  },
 
  onClickLastStep() {
    this.setData({
      currentStep: 0,
    });
  },
 
  onChangeLike({ detail }) {
    this.setData({ isLike: detail });
  },
  onChangeComment({ detail }) {
    this.setData({ isComment: detail });
  },
  onChangeGoods({ detail }) {
    this.setData({ isGoods: detail });
  },
  onChangeShare({ detail }) {
    this.setData({ isShare: detail });
  },
  onChangeReplay({ detail }) {
    this.setData({ isReplay: detail });
  },
 
  showErrorMsg(imgType,errMsg){
    wx.showToast({
      title: errMsg,
      icon: "none",
      duration: 2000,
    });
    this.setData({
      [imgType + "Error"]: errMsg,
    });
  },
  isImgUpload(){
    if (!this.data.shareImg) {
      let errMsg = "请上传分享卡片封面图片";
      this.showErrorMsg('shareImg',errMsg)
      return false;
    }
    if (!this.data.feedsImg&&this.data.isFeedsPublic) {
      let errMsg = "请上传直播卡片封面";
      this.showErrorMsg('feedsImg',errMsg)
      return false;
    }
    if (!this.data.bannerPhoto&&this.data.isShowInHomepage) {
      let errMsg = "请上传直播间首页入口封面";
      this.showErrorMsg('bannerPhoto',errMsg)
      return false;
    }
    if (!this.data.coverImg) {
      let errMsg = "请上传直播间背景墙";
      this.showErrorMsg('coverImg',errMsg)
      return false;
    }
    return true;
  },
 
  async onClickSubmit() {
    let data = {
      doccode: this.data.doccode,
      closeLike: !this.data.isLike,
      closeGoods: !this.data.isGoods,
      closeComment: !this.data.isComment,
      closeReplay: !this.data.isReplay,
      closeShare: !this.data.isShare,
      closeKf: true,
    };
    console.log("第二步设置数据", data);
    if (!this.isImgUpload()) {
      return;
    }
    
    // /shopping/live/saveRoomStep2.do?doccode=XXX&closeLike=false&closeGoods=false&closeComment=false&closeReplay=false&closeShare=false&closeKf=false
    let [err, res] = await utils.to(
      wx.$http.request({
        url: "/shopping/live/saveRoomStep2.do",
        data,
      })
    );
    if (err) {
      return;
    }
    console.log("新建房间--第二步", res);
    if (res.QrCodeUrlUnid) {
      wx.showLoading();
      await this.downloadQr(res.QrCodeUrlUnid);
      wx.hideLoading();
      this.setData({
        showQrCode: true,
        qrCodeUrl:res.QrCodeUrlUnid
      });
    } else {
      wx.showToast({
        title: '直播间创建成功',
        icon: "none",
        duration: 2000,
      });
      setTimeout(()=>{
        wx.redirectTo({
          url: "../myLiveRoom/myLiveRoom",
        });
      },2000)
 
      // 'https://res.wx.qq.com/op_res/BbVNeczA1XudfjVqCVoKgfuWe7e3aUhokktRVOqf_F0IqS6kYR--atCpVNUUC3zr'
    }
  },
 
  async isAuthorizeSave() {
    let [err, res] = await utils.to(wx.pro.getSetting());
    if (err) {
      return false;
    }
    console.log("getSetting", res);
    return res.authSetting["scope.writePhotosAlbum"] !== false;
  },
 
  async downloadQr(qrCodeUrl) {
    let [err, res] = await utils.to(
      wx.pro.downloadFile({
        // url: this.data.qrCodeUrl
        url: qrCodeUrl,
      })
    );
    if (err) {
      console.log("downloadQr", err);
      return;
    }
    console.log("----downloadQr",res);
    if (res.statusCode === 200) {
      // imgDownloadSum++;
      console.log('......头像',res.tempFilePath)
      this.setData({
        qrCodeTempUrl: res.tempFilePath
      })
    }else{
      wx.showToast({
        title: '二维码下载失败,请重试',
        icon: "none",
        duration: 3000,
      });
    }
  },
 
  async saveQrImg() {
    wx.showLoading({
      title: "保存中...",
    });
    let isAuthorizeSave = await this.isAuthorizeSave();
    if (!isAuthorizeSave) {
      wx.hideLoading();
      wx.showModal({
        title: "提示",
        content: "您还未授权保存到相册,请在接下来打开的设置页面开启相册授权!",
        success(res) {
          if (res.confirm) {
            console.log("用户点击确定");
            wx.openSetting({
              success(res) {
                console.log(res.authSetting);
              },
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
      return;
    }
    wx.saveImageToPhotosAlbum({
      filePath: this.data.qrCodeTempUrl,
      success: (res) => {
        wx.showToast({
          title: "二维码已保存到本地,赶紧分享给主播认证吧!",
          icon: "none",
          duration: 3000,
        });
      },
      complete: () => {
        wx.hideLoading();
      },
    });
  },
  getTimeMS(time){
    return new Date(time.replace('-','/')).getTime();
  },
  async getRoomData(doccode){
    let [err,res] = await utils.to(
      wx.$http.request({
        url:'/shopping/live/getLiveRoomListForMe.do',
        data:{
          doccode
        }
      })
    )
    if (err) {
      console.log(err);
      return;
    }
    
    let roomData = res.list[0];
    console.log('编辑直播间。。。',roomData.Type.toString(),roomData.Type);
    
    this.setData({
      anchorName: roomData.AnchorName,
      anchorWechat: roomData.AnchorWechat,
      subAnchorWechat: roomData.SubAnchorWechat,
      roomTitle: roomData.RoomName,
      screenType: roomData.ScreenType,
      isFeedsPublic: roomData.isFeedsPublic,
      isShowInHomepage:roomData.isShowBannerPhotoOnHomePage,
      type:roomData.Type.toString()||'0',
      startDate:this.getTimeMS(roomData.StartTime),
      endDate:this.getTimeMS(roomData.EndTime),
      startTime:roomData.StartTime,
      endTime:roomData.EndTime,
      doccode:roomData.DocCode,
      shareImg:roomData.ShareImgUnid||'',
      coverImg:roomData.CoverImgUnid||'',
      feedsImg:roomData.FeedsImgUnid||'',
      bannerPhoto:roomData.BannerPhoto||'',
      isLike: !roomData.isCloseLike,
      isComment: !roomData.isCloseComment,
      isGoods: !roomData.isCloseGoods,
      isShare: !roomData.isCloseShare,
      isReplay: !roomData.isCloseReplay,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.$http.request({
      url:'/shopping/account.do?m=accountInfo',
    }).then((res)=>{
      console.log('获取用户信息',res);
      this.setData({
        HeadImgUrl:res.UserInfo.HeadImgUrl
      })
    })
    let doccode = options.doccode || '';
    if (doccode) {
      this.getRoomData(doccode);
    }
    this.setData({
      isShowBannerOnHomePage:app.globalData.isShowBannerPhotoOnHomePageForLiveUser
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
 
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
 
  // }
});