openLink.js 8.71 KB
(function () {
  function transParams(params) {
    var result = "";
    if (typeof params === "object" && Object.keys(params).length > 0) {
      var keys = Object.keys(params);
      if (keys.length > 0) {
        keys.forEach(function (key, index) {
          var pre = index === 0 ? "?" : "&";
          result += pre + key + "=" + params[key]
        })
      }
    }
    return result
  }

  function webOpen(url, openType) {
    var type = openType;
    if (window.e9mobileConfig && window.e9mobileConfig.openLinkType) {
      type = window.e9mobileConfig.openLinkType;
    }
    if (type === "href") {
      window.location.href = url;
    } else if (type === "iframe") {
      // 190601
      if (window.WeaverMobilePage && window.WeaverMobilePage.RouteLayout
        && typeof window.WeaverMobilePage.RouteLayout.open === 'function') {
        window.WeaverMobilePage.RouteLayout.open(url);
        return {
          close: function () {
            window.history && window.history.go(-1);
          }
        };
      }
      var frameView = document.createElement("div");
      frameView.style.position = "fixed";
      frameView.style.width = "100%";
      frameView.style.height = "100%";
      frameView.style.top = 0;
      frameView.style.left = 0;
      frameView.style.zIndex = 998;
      frameView.innerHTML = '<iframe style="border:none;height:100%;width:100%" src="' + url + '"></iframe>';
      document.body.appendChild(frameView);
      return {
        close: function () {
          document.body.removeChild(frameView)
        }
      }
    } else if (type === "form") {
      var form = document.createElement("form");
      form.setAttribute("style", "display:none");
      form.setAttribute("method", "get");
      form.setAttribute("action", url);
      form.setAttribute("target", "");
      var input = document.createElement("input");
      input.setAttribute("name", "test");
      input.setAttribute("value", "download");
      document.body.appendChild(form);
      form.appendChild(input);
      form.submit()
    }
    return window.open(url)
  }
  var openLink = function(url, type, isDownload) {
    if (window.em && typeof window.em.checkJsApi === "function"
      && window.em.checkJsApi("openLink")
      && (!isDownload || (isDownload && window.navigator.userAgent.indexOf("E-Mobile") < 0))) {
      window.em.openLink({
        url: url,
        openType: 2
      })
    } else {
      return webOpen(url, isDownload ? "open" : (type || "open"))
    }
  }
  openLink.openWorkflowByParams = function(){
    return function (params, callbackFun, returnUrl) {
      var urlParams = typeof params === "object" ? transParams(params) : params;
      if (urlParams && (urlParams.substring(0, 1) === "?" || urlParams.substring(0, 1) === "&")) 
        urlParams = urlParams.substring(1);
      var url = "/spa/workflow/static4mobileform/index.html?_random=" + new Date().getTime() + "#/req?" + urlParams;
      openLink.openWorkflow(url, callbackFun, returnUrl);
    }
  }();
  openLink.openWorkflow = function () {
    return function (url, callbackFun, returnUrl) {
      if (window.em && typeof window.em.checkJsApi === "function" && window.em.checkJsApi("openLink") && window.em.checkJsApi("closeWindow") && window.navigator.userAgent.indexOf("E-Mobile") > -1) {
        var title = "表单详情";
        if (window.e9_locale && window.e9_locale.label) {
          title = window.e9_locale.label[500244] || "表单详情";
        }
        window.em.openLink({
          openType: 2,
          title: title,
          url: url
        });
        if (typeof callbackFun === "function") {
          window.em.ready(function(){
            window.em.registerBroadcast({
              name: "_closeWfFormCallBack",
              action: function (argument) {
                if (typeof argument === "string") {
                  argument = JSON.parse(argument);
                }
                callbackFun(argument.text);
              }
            });
          });
        }
      } else {
        //window.open(url);   //微信、钉钉客户端open就等于location.href
        window.location.href = url;
        if (returnUrl === "") {
          returnUrl = window.location.href;
          if (returnUrl.indexOf("/spa") > -1) {
            returnUrl = returnUrl.substr(returnUrl.indexOf("/spa"));
          }
        }
        sessionStorage.setItem("__wf__openform__returnurl__", returnUrl);
        if (typeof callbackFun === "function") {
          window.__closeFormCallBack = callbackFun;
        }
      }
    }
  }();
  openLink.openDocument = function () {
    return function (docId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        search = transParams(params)
      }
      var url = "/spa/document/static4mobile/index.html#/doc/" + docId + search;
      openLink(url, type)
    }
  }();
  openLink.openAttach = function () {
    return function (attachId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        search = transParams(params)
      }
      var url = "/spa/document/static4mobile/index.html#/attach/" + attachId + search;
      openLink(url, type)
    }
  }();
  openLink.getWeaverFile = function () {
    return function (url, downloadType, docType) {
      if (window.e9DownloadConfig && window.e9DownloadConfig.inUse && typeof window.e9DownloadConfig.downloadHandler === "function") {
        window.e9DownloadConfig.downloadHandler(url)
      } else {
        var fileId = "";
        fileId = url.slice(url.indexOf("fileid="));
        fileId = fileId.slice(0, fileId.indexOf("&"));
        if (window.sso_callApi) {
          window.sso_callApi({
            url: "/api/doc/mobile/imagefile/getMobileDownloadParams?" + fileId,
            method: "GET",
            params: {
              type: docType
            },
            type: "json"
          }).then(function (res) {
            if (res.params) {
              url += res.params
            }
            openLink(url, undefined, true)
          }).catch(function () {
            openLink(url, undefined, true)
          })
        }
      }
    }
  }();
  openLink.openMeeting = function () {
    return function (meetingId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        params.meetingid = meetingid;
        search = transParams(params)
      } else {
        search = "?meetingid=" + meetingId
      }
      var url = "/spa/meeting/static4mobile/index.html#/calendar/detail" + search;
      openLink(url, type)
    }
  }();
  openLink.openWorkplan = function () {
    return function (workplanId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        params.workplanId = workplanId;
        search = transParams(params)
      } else {
        search = "?workplanId=" + workplanId
      }
      var url = "/spa/workplan/static4mobile/index.html#/detail" + search;
      openLink(url, type)
    }
  }();
  openLink.openHrm = function () {
    return function (hrmId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        search = transParams(params)
      }
      var url = "/spa/hrm/static4mobile/index.html#/resourceInfo/" + hrmId + search;
      openLink(url, type)
    }
  }();
  openLink.openCrm = function () {
    return function (crmId, params, type) {
      var search = "";
      if (Object.prototype.toString.call(params) === "[object Object]") {
        search = transParams(params)
      }
      var url = "/spa/crm/static4mobile/index.html#/customerCard/" + crmId + search;
      openLink(url, type)
    }
  }();
  openLink.openSendMsg = function () {
    return function (requestid, type) {
      var url = "/spa/sms/static4mobile/index.html#/sendSms?src=supervise&requestid=" + requestid;
      openLink(url, type)
    }
  }();
  openLink.openSendEmail = function () {
    return function (requestid, type) {
      var url = "/spa/email/static4mobile/index.html#/new?requestid="+requestid;
      openLink(encodeURI(url), type)
    }
  }();
  openLink.browserOpenLink = {
    defaultOptions: {
      // type : function
      1: 'openHrm',
      17: 'openHrm',
      7: 'openCrm',
      18: 'openCrm',
      16: 'openWorkflow',
      152: 'openWorkflow',
      9: 'openDocument',
      37: 'openDocument',
      28: 'openMeeting',
      workplan: 'openWorkplan',
    },
    customOpenLink: {
      // type : function
    },
    set: function (options) {
      options = options || {};
      this.customOpenLink = Object.assign(this.customOpenLink, options);
    },
    get: function () {
      return Object.assign({}, this.defaultOptions, this.customOpenLink);
    }
  };
  window.openLink = openLink
})();