JavaScript和原生功能交互桥梁

/**
 * 注:“成功输出”指的是接口执行成功回调函数的入参,“失败输出”指的是接口执行失败回调函数的入参
 *      失败输出统一为:
 *          {"msg":"失败信息"}
 * Created by chaozhong on 2015/11/25.
 * */

/*
 * JS API最终执行函数,JavaScript和原生功能交互桥梁
 *
 * @param apiName:字符串,接口名
 * @param param:字符串或JSON,调用接口需要传递的参数
 * @param oncallback:字符串,接口调用成功回调函数名
 * @param errorcallback:字符串,接口调用失败回调函数名
 * */
function _execute(invokeID, apiName, param, successCallBackName, failedCallBackName) {
    try {
        if (param && typeof param !== "string") {
            param = JSON.stringify(param);
        }
    } catch (e) {
        throw new Error(e.message);
    }
    var src = "emm-services://?action=jsfunction"
        + "&invokeID=" + (invokeID || "")
        + "&apiname=" + (apiName || "")
        + "¶m=" + encodeURIComponent(param || "")
        + "&oncallback=" + (successCallBackName || "")
        + "&errorcallback=" + (failedCallBackName || "");
    var element = document.createElement("iframe");
    element.setAttribute("src", src);
    element.setAttribute("style", "display:none");
    element.setAttribute("width", "0px");
    element.setAttribute("height", "0px");
    element.setAttribute("frameborder", "0px");
    document.body.appendChild(element);
    element.parentNode.removeChild(element);

    console.info("successCallBackName", successCallBackName);
    console.info("failedCallBackName", failedCallBackName);
    console.info("invokeID", invokeID);
}

/*
 * 定义全局变量,接口回调函数的引用皆保存于此
 * */
window.JQAPICallBack = {
    callBackObjects: {},
    //接口执行成功回调
    successCallBack: function (invokeID, data) {
        this.callBackObjects[invokeID].successCallBack(data);
        delete  this.callBackObjects[invokeID];
    },
    //接口执行失败回调
    failedCallBack: function (invokeID, data) {
        this.callBackObjects[invokeID].failedCallBack(data);
        delete  this.callBackObjects[invokeID];
    }
};

/*
 * JS API创建工厂,JS API每被调用一次,创建一个实例,该实例保存API回调函数的引用
 * */
function JQAPIFactory(invokeID, APIName, param, successCallBack, failedCallBack) {
    if (typeof  successCallBack !== "function" || typeof  failedCallBack !== "function") {
        throw new Error("callback must be a function.");
    }
    this.successCallBack = successCallBack;
    this.failedCallBack = failedCallBack;
    _execute(invokeID, APIName, param, "JQAPICallBack.successCallBack", "JQAPICallBack.failedCallBack");
}

window.JQAPI = {
    /*
     * 获取SSOToken
     *
     * 输入:
     *      {"ISAID":"业务系统ID"}
     * 成功输出:
     *   {"SSOToken":"token"}
     * */
    getSSOToken: function (param, successCallBack, failedCallBack) {
        var invokeID = "getSSOToken" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getSSOToken", param, successCallBack, failedCallBack);
    },


    /**
     *	IC 平台获取用户身份票据
     *	成功输出:字符串ICToken值
     */
    jqGetICToken: function (successCallBack, failedCallBack) {
        var invokeID = "jqGetICToken" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetICToken", "", successCallBack, failedCallBack);
    },


    /**
     * 获取Xmas Session
     * 成功输出:{"xmas_session":"session"}
     * */
    getXmasSession: function (successCallBack, failedCallBack) {
        var invokeID = "getXmasSession" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getXmasSession", "", successCallBack, failedCallBack);
    },

    /*
     * 获取用户信息
     *
     * 成功输出:(返回字段供参考)
     *   {
     *       "loginName":"登录名",
     *       "password":"登录密码",
     *       "name":"中文名字",
     *       "imei":"imei",
     *       "os":"手机系统和版本"
     *   }
     * */
    getUserInfo: function (successCallBack, failedCallBack) {
        var invokeID = "getUserInfo" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getUserInfo", "", successCallBack, failedCallBack);
    },

    /**
     * 获取用户信息
     * 成功输出:(返回字段供参考)
     *   {
     *       "userId":"用户id",
     *       "userName":"用户账号",
     *       "passWord":"登录密码(EMM下无效)"
     *   }
     * */
    jqGetUserInfo: function (successCallBack, failedCallBack) {
        var invokeID = "jqGetUserInfo" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetUserInfo", "", successCallBack, failedCallBack);
    },


    /**
     * 获取用户信息
     * 成功输出:(返回字段供参考)
     *   {"userId":"用户id","userName":"用户账号","passWord":"加密的密码"}
     * */
    jqGetEncryptUserInfo: function (successCallBack, failedCallBack) {
        var invokeID = "jqGetEncryptUserInfo" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetEncryptUserInfo", "", successCallBack, failedCallBack);
    },


    /*
     * 获取经纬度
     *
     * 成功输出:
     *       {"latitude":"纬度","longitude":"经度"}
     * */
    getLatitudeAndLongitude: function (successCallBack, failedCallBack) {
        var invokeID = "getLatitudeAndLongitude" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getLatitudeAndLongitude", "", successCallBack, failedCallBack);
    },



    /**
     *	应用数据分享
     *	输入参数:{"title": "数据标题","detailID": "详情id","appcode": "应用标志","bigIcon": "大图标名称",
					"smallIcon": "小图标名称","rightDes": "底部右侧描述文字","leftDes": "底部左侧描述文字"}
     */
    sharedToChat:function (param, successCallBack, failedCallBack){

        var invokeID = "sharedToChat" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "sharedToChat", param, successCallBack, failedCallBack);
    },

    /*
     * 文件下载
     *
     * 输入:
     *       {
     "url": "http://210.75.8.38:9103/cnfantasia/easFlowDownload_v1",
     "fileName": "满意度调查系统项目二期及三期合同.pdf",
     "xmas-session": "http://192.168.1.17:6888/easweb/winlet/attachment/download.jsp?bosID=5qHgIAtLTFOa28QXHahLmPSvTwM=",
     "requestParam": "param",
     "requestHeaders": {
     "key1": "value1",
     "key2": "value2"
     }
     }
     * */
    fileDownload: function (param, successCallBack, failedCallBack) {
        var invokeID = "fileDownload" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "fileDownload", param, successCallBack, failedCallBack);
    },

    voiceDownload: function (param, successCallBack, failedCallBack) {
        var invokeID = "voiceDownload" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "voiceDownload", param, successCallBack, failedCallBack);
    },

    /*
     * 文件上传(可多文件上传)
     *
     * 输入:
     *      {"file":["文件1URI","文件2URI"...],"url":"接口地址"}
     * 成功输出:
     *
     * */
    fileUpload: function (param, successCallBack, failedCallBack) {
        var invokeID = "fileUpload" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "fileUpload", param, successCallBack, failedCallBack);
    },

    /*
     * 获取相片(打开相机拍摄或打开相册选取)
     *
     * 输入:
     *     {"sourceType":"相片来源(相机或相册)","maxNum":"最大获取相片张数"}
     *     sourceType:0-相册,1-相机
     * 成功输出:JSON数组
     *      [{"path":"相片在文件系统中的绝度路径","base64":"相片base64表示"}]
     * */
    getPicture: function (param, successCallBack, failedCallBack) {
        var invokeID = "getPicture" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getPicture", param, successCallBack, failedCallBack);
    },

    /**
     * 获取相片单选(打开相机拍摄或打开相册选取)
     *	输入:"[\"camera\"]" 或 param = "[\"pic\"]"
     *	成功输出:["path":"相片在文件系统中的绝对路径","base64":"相片base64表示"]
     * */
    pickImage: function (param, successCallBack, failedCallBack) {
        var invokeID = "pickImage" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "pickImage", param, successCallBack, failedCallBack);
    },


    /**
     * 拨打电话
     *	输入:"[\"8888888888\"]"
     * */
    call: function (data) {
        var invokeID = "call" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "call", data, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },
    /*
     * 获取相片并上传(打开相机拍摄或打开相册选取)
     *
     * 输入:
     *     {"getPicture":{"sourceType":"相片来源(相机或相册)","maxNum":"最大获取相片张数"},"fileUpload":{url":"接口地址"}}
     *     sourceType:0-相册,1-相机
     * 成功输出:JSON数组
     *      {后台接口返回信息,"pics": [{"path":"相片在文件系统中的绝度路径","base64":"相片base64表示"},{}]}
     * */
    jqImageChoiceUpload: function (param, successCallBack, failedCallBack) {
          var invokeID = "imageChoiceUpload" + new Date().getTime();
          JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqImageChoiceUpload", param, successCallBack, failedCallBack);
      },

     /**
     *	预览图片: position 第几张 pictures 图片集合 url 图片 地址 thumUrl 图片的缩略图地址 isNetResource 是否为网络图片

     *[{"position":2,"pictures":[{"url":"http://www....","thumUrl":"http://www...","isNetResource":false}]}]
     **/
    jqViewPicture:
      function (param, successCallBack, failedCallBack) {
        var invokeID = "jqViewPicture" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqViewPicture", param, successCallBack, failedCallBack);
      },

     /**
     *	记录行为日志: fromFunCode 上一步行为日志编码 funCode 当前行为日志编码 useTime 行为日志时间
     *[{"fromFunCode":"100002","funCode":"222222","useTime":"1234567898765"}]
     **/
    jqAddActionLog:
      function (param, successCallBack, failedCallBack) {
        var invokeID = "jqAddActionLog" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqAddActionLog", param, successCallBack, failedCallBack);
      },

     /**
      * 获取上一步记录日志编码:
      * 参数无[]
      * 反回结果,就是一个code值
      * **/
     jqGetActionCode:
      function (param, successCallBack, failedCallBack) {
        var invokeID = "jqGetActionCode" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetActionCode", param, successCallBack, failedCallBack);
      },

    /*
     * 设置元素返回按钮响应事件
     *
     * 输入:
     *     {"btnBack":"点左上角调js 的方法名称","keyBack":"点手机反回键调js 的方法名称"}

     * 成功输出:无
     * */
    jqSetClickBackBtnHandle: function (param, successCallBack, failedCallBack) {
      var invokeID = "jqSetClickBackBtnHandle" + new Date().getTime();
      JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetClickBackBtnHandle", param, successCallBack, failedCallBack);
    },
    /*
    * 输入[type] type=0: 隐藏  type=1:显示
    * */
  jqSetBrowserTopBarShow: function (param, successCallBack, failedCallBack) {
    var invokeID = "jqSetBrowserTopBarShow" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetBrowserTopBarShow", param, successCallBack, failedCallBack);
  },
    /*
     * 关闭当前WebView
     * */
    close: function () {
        var invokeID = "close" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "close", "", function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    /*
     * 关闭当前WebView
     * */
    back: function () {
        var invokeID = "back" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "back", "", function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    /**
     *	控制页面是否可以缩放
     *	输入:"[\"xx\"]",1为禁止,0为可缩放
     */
    jqSetBrowserForbidZoomControls: function (param) {
        var invokeID = "jqSetBrowserForbidZoomControls" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetBrowserForbidZoomControls", param, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    getDeviceInfo: function () {
        var invokeID = "getDeviceInfo" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "getDeviceInfo", "", function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

     jqSetBrowserTopBarShow: function (param) {
         var invokeID = "jqSetBrowserTopBarShow" + new Date().getTime();
         JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetBrowserTopBarShow", param, function () {
         }, function (msg) {
             throw new Error(msg);
         });
      },

     jqSetBrowserTopBarCloseShow: function (param) {
         var invokeID = "jqSetBrowserTopBarCloseShow" + new Date().getTime();
         JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetBrowserTopBarCloseShow", param, function () {
         }, function (msg) {
                throw new Error(msg);
         });
     },

     jqSetBrowserTopBarBackShow: function (param) {
         var invokeID = "jqSetBrowserTopBarBackShow" + new Date().getTime();
         JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqSetBrowserTopBarBackShow", param, function () {
         }, function (msg) {
                throw new Error(msg);
         });
     },


    /*
     * 打开一个新WebView
     *
     * 输入:
     *   {"url":"WebView地址(绝对路径或相对路径,未URI编码)"}
     * */
    open: function (param) {
        var invokeID = "open" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "open", param, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    /*
     * 打开一个新WebView
     *
     * 输入:
     *  {
     "url": "url地址",
     "title": "应用名称",
     "appCode": "应用包名",
     "type": "应用类型,4为外挂,2为在线也就cordova",
     "forceShowTopNav": "boolean类型true是原生标题栏强制显示",
     "backRefreshJsName": "关闭当前页面后,原生要调js的方法名"
     }
     * */
  jqOpenPage: function (param) {
        var invokeID = "jqOpenPage" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqOpenPage", param, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    /**
     * 打开一个新WebView
     * 输入:["url地址"]
     * */
    openpage: function (param) {
        var invokeID = "openpage" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "openpage", param, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },

    /*
     * 图片查看(可保存图片)
     *
     * 输入:
     *   {"pictures":[图片URI数组],"position":"当前显示图片位置","allowSave":"是否允许保存相片"}
     *   示例:{"pictures":["1.png","2.png"],"position":2,"allowSave":true}
     * */
    viewPicture: function (param) {
        var invokeID = "viewPicture" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "viewPicture", param, function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },


  /*
   * 加密方法
   * 入参:[{string:字符串类型加密的数据}]
   * */
  encrypt: function (param,successCallBack, failedCallBack) {
    var invokeID = "encrypt" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "encrypt", param, successCallBack, failedCallBack);
  },
  /*
   * 解密方法
   * 入参:JSON.stringify([{string:字符串类型加密的数据}])
   * */
  decrypt: function (param,successCallBack, failedCallBack) {
    var invokeID = "decrypt" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "decrypt", param, successCallBack, failedCallBack);
  },
  /*
   * 解密方法2
   * 入参:JSON.stringify([{string:字符串类型加密的数据}])
   * */
  jqDecrypt: function (param,successCallBack, failedCallBack) {
    var invokeID = "jqDecrypt" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqDecrypt", param, successCallBack, failedCallBack);
  },
  /*
   * 解密方法
   * 入参:JSON.stringify([{string:字符串类型加密的数据}])
   * */
  jqTest: function (successCallBack, failedCallBack) {
    var invokeID = "jqTest" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqTest", "", successCallBack, failedCallBack);
  },

  /**
   *	开始录制语音
   */
  startVoice: function () {
    var invokeID = "startVoice" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "startVoice", "", function () {
    }, function (msg) {
      throw new Error(msg);
    });
  },

  /**
   *	停止录制语音
   *	输出:{"filepath":"语音文件路径","msg":"success","time":2345} time录音时间单位s
   **/
  stopVoice: function (successCallBack, failedCallBack) {
    var invokeID = "stopVoice" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "stopVoice", "", successCallBack, failedCallBack);
  },

  /**
   *	播放录音
   *	输入:[{\"filepath\":\"path\"}]  path语音文件的本地路径
   */
  playVoice: function (param) {
    var invokeID = "playVoice" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "playVoice", param, function () {
    }, function (msg) {
      throw new Error(msg);
    });
  },

  /**
   *	停止播放录音
   */
  stopPlayVoice: function () {
    var invokeID = "stopPlayVoice" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "stopPlayVoice", "", function () {
    }, function (msg) {
      throw new Error(msg);
    });
  },

    /**
     *	验证指纹
     *	输出:字符串true/false
     */
    jqOpenFingerprint: function (successCallBack, failedCallBack) {
        var invokeID = "jqOpenFingerprint" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqOpenFingerprint", "", successCallBack, failedCallBack);
    },
    /**
     *
     */
    jqHasClickBackBtnHandle: function () {
        var invokeID = "jqHasClickBackBtnHandle" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqHasClickBackBtnHandle", "",function () {
        }, function (msg) {
            throw new Error(msg);
        });
    },
    /**
     *	判断是否支持指纹
     *	输出:字符串true/false
     */
    jqHasFingerprint: function (successCallBack, failedCallBack) {
        var invokeID = "jqHasFingerprint" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqHasFingerprint", "", successCallBack, failedCallBack);
    },
  /**
   * 文件上传 xmas
   * 输入:{
    "url": "上传接口",
    "file": [
        "filepath",
        "filepath2"
    ],
    "xmas-session": "XMAS-SESSIONID",
    "bizNeedEncrypt": true,
    "requestParam": "参数,当bizNeedEncrypt为true时,requestParam的值是参数json串的字符串(其中biz的值也为字符串格json),为false requestParam的值为xmas-json=拼上h5已加密好的密文"
}

   * 成功输出:返回接口执行结果
   * */
  xmasFileUpload: function (param, successCallBack, failedCallBack) {
    var invokeID = "xmasFileUpload" + new Date().getTime();
    JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "xmasFileUpload", param, successCallBack, failedCallBack);
  },

    /**
     判断应用是否已安装
     {"appCode":"包名"}
     反回结果就是{"result":true}
     */
    jqIsInstalled: function (param, successCallBack, failedCallBack) {
        var invokeID = "jqIsInstalled" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqIsInstalled", param, successCallBack, failedCallBack);
    },
    /**
     * 获取应用其本信息
     {"appCode":"包名"}
     反回结果就是{"install":true, "version":"3.1.2"}
     */
    jqGetLocalAppInfo: function (param, successCallBack, failedCallBack) {
        var invokeID = "jqGetLocalAppInfo" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetLocalAppInfo", param, successCallBack, failedCallBack);
    },
    /**
     *判断当前网络类型
     参数无
     反回结果 {"result":"WIFI"}   值 WIFI 为连接了wifi  ,MOBILE为手机网(4G/3G/2G等) UNKNOW为当前没有网络
     */
    jqGetNetworkType: function (successCallBack, failedCallBack) {
        var invokeID = "jqGetNetworkType" + new Date().getTime();
        JQAPICallBack.callBackObjects[invokeID] = new JQAPIFactory(invokeID, "jqGetNetworkType", "", successCallBack, failedCallBack);
    }

};

 

你可能感兴趣的:(android,Hybrid)