blessing.js 19.6 KB
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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
import {
  getBindtapData,
  checkAuth
} from '../../utils/util';

import Dialog from '../../ui/vant-weapp/dialog/dialog';
import Poster from '../../miniprogram_dist/poster/poster';

const innerAudio = wx.createInnerAudioContext();

let app = getApp();
Page({
  data: {
    isOverShare: true,
    authorizeVisible: false,
    total: 0,
    page: 1,
    size: 10,
    productList: [], // 产品列表
    indexInfo: {},
    userInfo: {},
    options: {},
    detailData: {},
    ownerMember: {}, // 房主
    memberList: [], //	用户信息
    blessContent: [], // 祝福内容
    isAuth: false, // 是否授权
    // type: 0, // 0单人 1组队
    // 个人信息修改
    updateUserInfoDialogVisible: false,
    updateUserInfo: {}, // 需要更新的用户数据
    files: [], // 上传文件列表

    // 房间信息信息修改
    updateRoomDialogVisible: false,
    updateRoomInfo: {}, // 需要更新的房间数据

    // 加入房间
    joinRoomDialogVisible: false,
    isJoin: false, // 是否能加入状态

    isPlayingBgm: false,
    isMore: false,

    imageUrl: "", // 海报图
    posterVisible: false,
    wxCodeUrl: "", // 微信小程序码
  },
  onShareAppMessage(res) {
    let shareType = ""
    if (res.from === 'button') {
      // 来自页面内转发按钮
      // shareType == join 分享给家人,能加入 组队模式
      shareType = res.target.dataset.data || "";
      console.log("shareType:", shareType);
    } else {}
    let {
      detailData
    } = this.data;

    let memberCode = app.store.getItem("memberCode");
    let title = "";
    let path = `pages/blessing/blessing?c=${detailData.blessCode}&m=${memberCode}&t=${shareType}`;
    console.log("share path:", path);
    // let imageUrl = detailData && detailData.thumbnail || "";
    return {
      // title,
      path,
      // imageUrl
    }
  },
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onHide() {

  },
  onUnload() {
    this.pauseBgm();
  },
  onLoad(options) {
    this.setData({
      options
    });
    checkAuth().then((result) => {
      this.setData({
        isAuth: result
      })
      this.initData();
      this.queryBlessDetail().then((result) => {
        this.playBgm();
        let {
          detailData,
          ownerMember,
          memberList
        } = this.data;

        // 判断memberList里面是否有自己
        let joined = false;
        memberList.forEach(element => {
          if (element.mySelf == 1) {
            joined = true;
          }
        });

        /**
         * 显示加入房间条件
         * 1.已定制
         * 2.类型为组队类型
         * 3.非房主
         * 4.shareType为join  t="join"
         * 5.未加入过  判断memberList里面是否有自己
         */
        console.log("---- 判断是否显示加入 Start ----");
        console.log("已定制:", detailData.customMade == 1);
        console.log("类型为组队类型:", detailData.type == 1);
        console.log("非房主:", ownerMember.mySelf == 0);
        console.log("shareType为join:", options);
        console.log("是否加入过:", joined);
        if (detailData.customMade == 1 && detailData.type == 1 && ownerMember.mySelf == 0 && options.t == "join" && !joined) {
          this.setData({
            joinRoomDialogVisible: true,
            isJoin: true
          })
        }
      });
      console.log("memberCode:", app.store.getItem("memberCode"));
    }).catch((err) => {});

  },

  /**
   * 基础方法
   * 授权完毕重拉数据用
   */
  initData() {},
  playBgm() {
    let _this = this;
    let {
      detailData
    } = this.data;

    innerAudio.src = detailData.musicUrl;
    innerAudio.seek(0);
    innerAudio.play();
    innerAudio.onEnded(() => {
      _this.playBgm();
    })
    this.setData({
      isPlayingBgm: true,
    })
  },

  pauseBgm() {
    innerAudio.pause();
    innerAudio.offEnded(() => {})
    this.setData({
      isPlayingBgm: false,
    })
  },

  onBgmHandler() {
    if (this.data.isPlayingBgm) {
      this.pauseBgm();
    } else {
      this.playBgm();
    }
  },

  onShowMoreHandler() {
    this.setData({
      isMore: true
    })
  },

  showTips(evt) {
    let tips = getBindtapData(evt);
    wx.showModal({
      content: tips,
      success(res) {}
    })
  },

  /**
   * 赞赏祝福
   * 跳转到赞赏页
   */
  toReward() {
    app.router.push({
      path: "reward"
    })
  },

  /**
   * 选择更多祝福模版
   */
  onMoreTemplateHandler() {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      // 如果能返回上一级
      app.router.push({
        openType: "back"
      });
    } else {
      app.router.push({
        openType: "redirect",
        path: "index"
      });
    }
  },

  onHidePosterHandler() {
    this.setData({
      posterVisible: false
    })
  },

  /**
   * 图片查看
   */
  onPreviewImageHandler(evt) {
    let current = this.data.imageUrl;
    let urls = [current];
    wx.previewImage({
      current: current,
      urls: urls
    })
  },

  /**
   * 保存图片到本地
   */
  saveImageToPhotosAlbum() {
    let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: _this.data.imageUrl,
      success(res) {
        wx.showToast({
          title: '保存成功',
          icon: 'success'
        });
      },
      fail(err) {
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.writePhotosAlbum']) {
              // 未授权
              wx.showModal({
                title: '提示',
                content: '小程序请求访问相册权限',
                confirmText: '前往授权',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(res) {}
                    })
                  } else if (res.cancel) {}
                }
              })
            }
          }
        })
      }
    })
  },

  // 配置小程序码
  getWxCode() {
    let {
      detailData
    } = this.data;
    let memberCode = app.store.getItem("memberCode");
    let wxCodePath = `pages/blessing/blessing?c=${detailData.blessCode}&m=${memberCode}`;

    return new Promise((resolve, reject) => {
      // 先获取小程序码
      app.get({
        mode: "common",
        url: app.api.wxacodeGet,
        data: {
          path: encodeURIComponent(wxCodePath)
        }
      }).then((result) => {
        this.setData({
          wxCodeUrl: result
        })
        resolve(result);
      })
    });

  },

  /**
   * 分享图片祝福
   * 生成海报
   */
  onPosterHandler() {
    console.log("onPosterHandler");

    // 先获取小程序码
    this.getWxCode().then((result) => {
      let posterData = this.getPosterConfig();
      this.onCreatePoster(posterData);
    });
  },

  onPosterSuccess(e) {
    wx.hideLoading();
    const {
      detail
    } = e;
    console.log("detail:", detail)
    this.setData({
      imageUrl: detail,
      posterVisible: true,
    })
  },

  onPosterFail(err) {
    wx.hideLoading();
    console.error(err);
  },

  /**
   * 异步生成海报
   */
  onCreatePoster(posterConfig) {
    console.log("posterConfig:", posterConfig);
    this.setData({
      posterConfig: posterConfig
    }, () => {
      Poster.create(true); // 入参:true为抹掉重新生成
    });
  },

  // 获取海报数据
  getPosterConfig() {
    let {
      detailData,
      ownerMember,
      memberList,
      wxCodeUrl
    } = this.data;
    let avatarWid = 120;
    let nickname = ownerMember.memberName;
    let avatar = ownerMember.memberHead;
    let desc = "";
    if (detailData.type == 1) {
      // 组队
      if (detailData.count > 0) {
        desc = `携${detailData.familyName}${detailData.count}人`
      } else {
        desc = `携${detailData.familyName}`
      }
    } else {
      // 单人
      desc = `向各位亲朋好友`
    }
    let blocks = []
    let images = [
      // 背景图
      {
        x: 0,
        y: 0,
        width: 750,
        height: 1334,
        url: "../../image/poster/poster_c1.png",
      },
      // 头像
      {
        x: 184,
        y: 186,
        width: avatarWid,
        height: avatarWid,
        borderRadius: avatarWid,
        zIndex: 22,
        url: avatar,
      },
      // 小程序码
      {
        width: 100,
        height: 100,
        x: 586,
        y: 1198,
        url: wxCodeUrl,
        zIndex: 31
      }
    ];
    let lines = [];
    let texts = [
      // 房主昵称
      {
        x: 53,
        y: 460,
        fontSize: 60,
        color: "#fee085",
        textAlign: "left",
        zIndex: 11,
        text: nickname,
      },
      // 文案
      {
        x: 53,
        y: 540,
        fontSize: 60,
        color: "#fee085",
        textAlign: "left",
        zIndex: 11,
        text: desc,
      }
    ];

    // 组队补充
    if (detailData.type == 1) {
      // 头像昵称取出3人
      let member1 = memberList[0] || null;
      let member2 = memberList[1] || null;
      let member3 = memberList[2] || null;

      let memberDrawList = [];
      if (member1) memberDrawList.push(member1);
      if (member2) memberDrawList.push(member2);
      if (member3) memberDrawList.push(member3);
      let endX = 630;
      let wid = 160;
      let startY = 800;
      memberDrawList.forEach((element, idx) => {
        // blocks.push({
        //   x: endX - (idx * wid) - ((wid - 92)),
        //   y: startY,
        //   width: 92,
        //   height: 92,
        //   borderRadius: 92,
        //   zIndex: 90,
        //   borderColor: "#fee085",
        // })
        images.push({
          x: endX - (idx * wid) - ((wid - 92) / 2) - 4,
          y: startY + 2,
          width: 92,
          height: 92,
          borderRadius: 92,
          zIndex: 101,
          url: avatar,
        })
        texts.push({
          x: endX - (idx * wid),
          y: startY + 136,
          width: wid,
          fontSize: 24,
          color: "#fee085",
          textAlign: "center",
          zIndex: 101,
          text: element.memberName,
        })
      });

      texts.push({
        x: 686,
        y: startY + 200,
        fontSize: 48,
        color: "#fee085",
        textAlign: "right",
        zIndex: 11,
        text: `长按查看全部${detailData.count}人 >>`,
      })
    }

    let posterData = {
      width: 750,
      height: 1334,
      debug: false,
      blocks: blocks,
      images: images,
      lines: lines,
      texts: texts,
    }
    return posterData;
  },

  // 显示更新用户信息
  showUpdateUserInfoDialog() {
    this.setData({
      updateUserInfoDialogVisible: true,
      updateUserInfo: {},
      files: []
    })
  },

  // 房主名字
  onUpdateUserInfoBindInput(e) {
    this.setData({
      updateUserInfo: {
        memberName: e.detail.value
      }
    })
  },

  // 显示更新用户信息
  showUpdateRoomDialog() {
    this.setData({
      updateRoomDialogVisible: true,
      updateRoomInfo: {},
    })
  },

  // 家庭名称
  onUpdateFamilyNameBindInput(e) {
    this.setData({
      "updateRoomInfo.familyName": e.detail.value
    })
  },
  // 祝福语
  onUpdateSentenceBindInput(e) {
    this.setData({
      "updateRoomInfo.sentence": e.detail.value
    })
  },

  /**
   * 更新房间信息
   */
  onUpdateRoomHander() {
    let {
      detailData,
      ownerMember,
      updateRoomInfo
    } = this.data;
    console.log("updateRoomInfo:", updateRoomInfo);
    app.post({
      url: app.api.blessUpdateRoom,
      data: {
        blessCode: detailData.blessCode,
        // ownerMemberCode: app.store.getItem("memberCode"),
        familyName: updateRoomInfo.familyName || "",
        sentence: updateRoomInfo.sentence || "",
      }
    }).then((result) => {
      console.log("result:", result);
      this.queryBlessDetail();
    }).catch((err) => {});
  },

  /**
   * 上传头像
   */
  onUploadHandler() {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      count: 1,
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFiles = res.tempFiles
        let files = tempFiles;
        console.log("chooseImage files:", files);
        _this.setData({
          files
        });
      }
    })
  },

  /**
   * 更新拜年页用户信息
   */
  onUpdateUserInfoHander() {
    let {
      detailData,
      ownerMember,
      updateUserInfo,
      files
    } = this.data;
    let pics = [];
    files.forEach(element => {
      pics.push(element.path)
    });

    this.uploadToCustomService(pics).then((picRes) => {
      console.log("picRes:", picRes);
      console.log("updateUserInfo:", updateUserInfo);
      app.post({
        url: app.api.blessUpdateUserInfo,
        data: {
          blessCode: detailData.blessCode,
          ownerMemberCode: this.data.options.m ? this.data.options.m : app.store.getItem("memberCode") ? app.store.getItem("memberCode") : "",
          memberName: updateUserInfo.memberName || "",
          memberHead: picRes[0] || "",
        }
      }).then((result) => {
        console.log("result:", result);
        this.queryBlessDetail();
      }).catch((err) => {});
    })
  },


  /**
   * 上传到自定义服务器
   * urlList 需要上传的图片地址
   */
  uploadToCustomService(urlList) {
    let _this = this;
    return new Promise((resolve, reject) => {
      // promise列表
      let p = [];
      // 用promise上传音频
      urlList.forEach(element => {
        let myPromise = new Promise((resolve2, reject2) => {
          _this.uploadfileMultiple(element).then((result2) => {
            resolve2(result2)
          }).catch((err) => {
            reject2();
          });
        });
        p.push(myPromise);
      });
      Promise.all(p).then(uploadFiles => {
        resolve(uploadFiles)
      }, reason => {
        reject();
      });
    });
  },
  /**
   * 多文件上传
   * @param {*} filePath
   */
  uploadfileMultiple(filePath) {
    let _this = this;
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: app.config.NET_CONFIG.commonApi + app.api.uploadFile,
        filePath: filePath,
        name: 'file',
        // header: {}, // 设置请求的 header
        header: {
          'content-type': 'multipart/form-data'
        },
        formData: {
          path: '/weapp/jyes/avatar/'
        },
        // HTTP 请求中其他额外的 form data
        success(res) {
          let result = JSON.parse(res.data).content;
          resolve(result);
        },
        fail() {
          reject()
        },
        complete: function () {}
      })
    });
  },



  /**
   * 视频详情
   */
  queryBlessDetail() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.blessDetail,
        data: {
          blessCode: this.data.options.c,
          ownerMemberCode: this.data.options.m ? this.data.options.m : app.store.getItem("memberCode") ? app.store.getItem("memberCode") : "",
        }
      }).then((result) => {
        let blessContent = [];
        try {
          blessContent = JSON.parse(result.blessContent) || [];
        } catch (error) {}
        let memberList = result.memberList;
        // hardcode数据
        // for (let index = 0; index < 4; index++) {
        //   memberList.push({
        //     blessCode: "4144feba2b904ba89585d90e8618a863",
        //     blessMemberCode: "747e8e314d0e46679be17fb495975439",
        //     count: 2,
        //     customMade: 0,
        //     familyName: "祝福团体称呼",
        //     memberCode: "9af835ad57234cf2826b770dcc8a9897",
        //     memberHead: "https://wx.qlogo.cn/mmopen/vi_32/v4XnMftVxCzkFD8ibLicDjxicFhzugVt4vUWsHmup9ZeunwZhQ21H9ugELpgyTicsVyNCXenqIlUHBIkZ2aEP2qPpw/132",
        //     memberName: "ee",
        //     mySelf: 1,
        //     owner: 0,
        //     sentence: "定义"
        //   })
        // }
        this.setData({
          detailData: result,
          ownerMember: result.ownerMember,
          memberList: memberList,
          blessContent: blessContent,
        });
        console.log("detailData:", result);
        console.log("blessContent:", this.data.blessContent);
        console.log("类型 type => ", result.type == 1 ? "组队" : "单人");
        console.log("是否已定制 customMade => ", result.customMade == 1 ? "已经定制" : "未定制");
        console.log("是否自己房间 mySelf => ", result.ownerMember.mySelf == 1 ? "是" : "否");
        console.log("房主信息 ownerMember => ", result.ownerMember);
        console.log("是否授权 isAuth =>", this.data.isAuth);
        console.log("房主信息 =>", result.ownerMember);
        console.log("祝福内容 =>", result.blessContent);
        resolve(result);
      }).catch((err) => {
        reject(err);
      });
    });
  },

  /**
   * 标记定制祝福
   */
  onBlessMakeHandler() {
    let {
      detailData,
      isAuth
    } = this.data;
    console.log("isAuth:", isAuth);
    if (isAuth) {
      // 已授权
      app.post({
        url: app.api.blessMade,
        data: {
          blessCode: detailData.blessCode,
        }
      }).then((result) => {
        this.queryBlessDetail().then((result) => {
          wx.showModal({
            title: '定制祝福成功!',
            content: '马上为亲朋好友送上祝福吧~',
            success(res) {}
          })
        });
      }).catch((err) => {});
    } else {
      // 未授权
    }
  },

  /**
   * 加入房间
   */
  onBlessJoinHandler(e) {
    let {
      options,
      detailData
    } = this.data;
    app.post({
      url: app.api.blessJoin,
      data: {
        ownerMemberCode: options.m,
        blessCode: detailData.blessCode
      }
    }).then((result) => {
      this.setData({
        isJoin: false
      })
      this.queryBlessDetail().then((result) => {
        wx.showModal({
          content: '加入成功',
          success(res) {}
        })
      });
    }).catch((err) => {

    });
  },

  // 点击确认授权按钮
  bindGetUserInfo(e) {
    let _this = this;
    wx.showLoading();
    wx.login({
      success(res) {
        app.store.setItem('wxcode', res.code);
        _this.getUserInfo(e.detail);
      }
    });
  },

  // 授权操作
  getUserInfo(e) {
    let _this = this;
    if (e.encryptedData && e.iv) {
      // 同意授权
      app.post({
        url: app.api.register,
        sid: false,
        data: {
          encryptedData: e.encryptedData,
          iv: e.iv,
          code: app.store.getItem("wxcode"),
          tlMemberCode: app.globalData.tlMemberCode
        }
      }).then((res2) => {
        wx.hideLoading();
        if (res2 && res2.sessionId) {
          app.store.setItem('sessionId', res2.sessionId);
        }
        wx.showModal({
          content: '授权成功',
          success(res) {}
        })
        checkAuth().then((result) => {
          _this.setData({
            isAuth: result
          });
        }).catch((err) => {});
      }).catch((err) => {
        wx.hideLoading();
        console.log("授权成功 err:", err);
      })
    } else {
      // 取消授权
      wx.hideLoading();
      console.log("取消授权");
    }
  },


  /**
   * 隐藏蒙层
   */
  hideMask() {
    this.setData({
      productDetailVisible: false,
      authorizeVisible: false,
    })
  },
  /**
   * 子组件事件
   * @param {*} evt
   */
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      // 隐藏蒙层
      case "_evt_hide_mask":
        this.hideMask();
        break;

        /**
         * 重拉数据已在
         */
      case "_evt_auth_complete":
        // this.initData();
        this.hideMask();
        break;

      default:
        break;
    }
  },
})