appDetails.js 3.33 KB
import {
  getOutlookUrl,
  getZoomUrl,
  getThirdUserPlatForm,
  unbindingApp
} from "../../api/request";
import "dingtalk-jsapi/entry/mobile";
import openLink from "dingtalk-jsapi/api/biz/util/openLink";
import create from "dd-store";
import { fail } from "assert";
let interval = "";
let requestNum = 0;
create.Page({
  data: {
    $data: null,
    url: "",
    platform: "",
    name: "",
    isBind: false,
    isLoading: false,
    showSelectPopup: false,
    selectPopupList: [{ text: "取消关联" }],
    bindStatus: "loading"
  },
  onLoad(e) {
    this.setData({
      platform: e.platform,
      name: e.userName,
      isBind: e.userName ? true : false
    });
  },
  onShow() { },
  sendRequest() {
    let that = this;
    interval = setInterval(() => {
      if (requestNum >= 180) {
        requestNum = 0;
        that.setData({
          isBind: false,
          bindStatus: "fail"
        });
        setTimeout(() => {
          that.setData({
            isLoading: false
          });
        }, 2000);
        clearInterval(interval);
      }
      requestNum++;
      let data = {
        platForm: this.data.platform,
        ddUserId: getApp().globalData.userid
      };
      getThirdUserPlatForm(data).then(res => {
        if (res.data.msg === "当前账号已被其他账号绑定!") {
          this.setData({
            isBind: false,
            bindStatus: "binded"
          });
          setTimeout(() => {
            that.setData({
              isLoading: false
            });
          }, 10000);
          clearInterval(interval);
        } else if (res.data.data) {
          if (res.data.data.platform === this.data.platform) {
            this.setData({
              name: res.data.data.userName,
              isBind: true,
              bindStatus: "success"
            });
            setTimeout(() => {
              that.setData({
                isLoading: false
              });
            }, 2000);
            clearInterval(interval);
            this.$store.data.relatedAppNeedUpdate = true;
            this.update();
          }
        }
      });
    }, 1000);
  },
  bindApp() {
    if (this.data.isLoading) {
      return false;
    }
    this.setData({
      isLoading: true,
      bindStatus: "loading"
    });
    this.sendRequest();
    if (this.data.platform === "outlook") {
      let data = `${getApp().globalData.userid},${dd.corpId}`;
      getOutlookUrl(data).then(res => {
        openLink({
          url: res.data.data
        });
      });
    } else if (this.data.platform === "zoom") {
      let data = {
        userId: getApp().globalData.userid,
        orgId: dd.corpId
      };
      getZoomUrl(data).then(res => {
        openLink({
          url: res.data.data
        });
      });
    }
  },
  unbind() {
    unbindingApp(this.data.platform).then(res => {
      if (res.data.data) {
        this.setData({
          name: "",
          isBind: false,
          showSelectPopup: false
        });
        this.$store.data.relatedAppNeedUpdate = true;
        this.update();
      }
    });
  },
  onSelectPopup(e) {
    if (e.target.dataset.item.text === "取消关联") {
      this.unbind();
    }
  },
  onSelectPopupCancel() {
    this.setData({
      showSelectPopup: false
    });
  },
  showPopup() {
    this.setData({
      showSelectPopup: true
    });
  },
  onUnload() {
    clearInterval(interval);
  }
});