UNPKG

eapp

Version:
945 lines (832 loc) 28.2 kB
var timestamp = '1'; /** * 初始化 */ $(window).bind('load', function () { timestamp = window.localStorage.getItem("version"); }); /** * -----------监听插件初始化----------------------------------------- */ EappApi.on('EappReady', function () { console.log('demo--Eappready----->'); // 页面状态指示灯闪烁 var d = $("#status"); d.removeClass("btnRed"); d.addClass("btnGreen"); d.text("ok"); loading.tip.hide(); // 首次获取网络 var dom = $("#net_status"); dom.text(EappApi.getNetWorkType()); // 检查 EappApi.checkVersionAndUpdate(false); }); // 法一:监听网络 EappApi.on("NetWork", function (type) { console.log('demo--NetWork----->', type); }); // 法二:分开监听之网络在线 EappApi.on("EappNetWorkOnline", function (e) { console.log('demo--EappNetWorkOnline----->'); var d = $("#net_status"); d.text(EappApi.getNetWorkType()); }); // 法二:分开监听之网络离线 EappApi.on("EappNetWorkOffLine", function (e) { console.log('demo--EappNetWorkOffLine----->'); webToast("当前处于断网状态!", 800); var d = $("#net_status"); d.text(EappApi.getNetWorkType()); }); /** * -----------Eapp插件功能展示---start------------------------------------------ */ // 拍照 function takePhotoFromCameraTest() { // 压缩率为20% EappApi.takePhotoFromCamera(20, function (base64Data) { // alert("拍照成功---------->"); showPopup(base64Data); }, function (error) { console.log('failCallBack--->' + error); }); } // 相册拍照 function takePhotoFromAlbumTest() { // 压缩率为20% EappApi.takePhotoFromAlbum(20, function (base64Data) { // alert("相册---------->"); showPopup(base64Data); }, function (error) { console.log('相册失败--->' + error); }); } // 二维码 function takeScanTest() { var params = {}; EappApi.takeScan(params, function (dataJO) { //扫码成功后执行的回调函数 alert("扫码文字结果: " + JSON.stringify(dataJO)); }, function (error) { //扫码失败执行的回调函数 alert("Scanning failed: " + error); }); } // 第三方链接跳转,兼顾ios无法返回 function goThirdUrlTest() { var url = "https://m.fagougou.com/wx/fromMkt?mkt=idfkj031792af#/robot/product/592f7e5f15a8097875cb8ddb"; EappApi.goThirdUrl(url); } // 显示启动页 function showSplashTest() { EappApi.showSplash(); setTimeout(function () { EappApi.hideSplash(); }, 3000); } // 隐藏启动页 function hideSplashTest() { EappApi.hideSplash(); } // 设置状态栏颜色 function titleBarColorTest() { EappApi.titleBarColor('#bf1d03'); } var dlg; // 自定义状态栏颜色 function titleBarColorTest2() { var html = "<label>颜色值:<input class='confirm_input' placeholder='请输入颜色值如:#bf1d03'></label>"; if (dlg == undefined) { dlg = popTipShow.confirm('自定义状态栏颜色', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var value = $(".confirm_input").val(); if (null == value || "" == value) { // webToast("颜色值不能为空!", 800); $(".confirm_input").val('#bf1d03'); value = '#bf1d03'; } EappApi.titleBarColor(value); } }); } else { dlg.show(); } } // 清除浏览记录(android有效) function clearHistoryTest() { EappApi.clearHistory(); } // 清除缓存(android有效) function clearCacheTest() { EappApi.clearCache(); } // 退出app(android有效) function exitAppTest() { showConfirmDialog("您确认要退出应用么?", function () { EappApi.exitApp(); }); } // 返回上一次浏览页面(android有效) function exitActivityTest() { EappApi.goBack(); } // 跳转apn function toSettingApn() { EappApi.toSettingApn(); } function getVersionTest() { // 获取apk壳子版本号 // var vCode = EappApi.getAndroidVersion(); // var vName = EappApi.getAndroidVersionName(); // var msg = "当前android版本名为:V" + vName + ";版本号为:" + vCode; // showTipDialog(msg, function () { // console.log("-------->") // }) } // EappReady中触发:检查更新 function checkVersionAndUpdate() { EappApi.checkVersionAndUpdate(true); } // ios升级 function gotoInstallIosPage() { var iosUrl = ROOT_URL + "/_files/ios/phone/index.html"; EappApi.goInstallIosPage(iosUrl); } // android升级 function downloadApk(newApkUrl) { // android下载并安装apk,可监控下载进度 EappApi.downloadAndInstallApk(newApkUrl, function (data) { if (data.status == 200) { console.log("开始下载-->", data); // 显示下载进度条 loading.progress.show(); } else if (data.status == 201) { var per = data.progress; // 下载整数进度条 if (parseInt(per * 100) % 100 == 0) { console.log("下载中-->" + per + "%"); // 更新下载进度条 loading.progress.update(per); } } else if (data.status == 202) { // 下载完成,关闭进度条 loading.progress.hide(); console.log("下载完成-->", data); } else if (data.status == 203) { console.log("下载失败-->", data); } else if (data.status == 400) { console.log("未知失败-->", data); } }); } //获取当前网络状态 function getNetWorkCheck() { showTipDialog('当前网络状态:' + EappApi.getNetWorkType()); } //获取当前版本号 function getAppVersionAndCode() { EappApi.getAppVersionNameAndCode(function (data) { showTipDialog('当前Version:' + data.version + "\n当前VersionCode:" + data.code); }); } var tempApk; //下载文件 function fileDownloadFile() { if (!EappApi.isAndroid()) { showTipDialog("下载apk只能在Anroid平台试验"); return; } var newFileUrl = ROOT_URL + "/_files/android/phone/eappdemo.apk"; var rootPath = EappApi.getValueByKey("cordova.file.externalDataDirectory"); var file = EappApi.getValueByKey("cordova.file"); consoleLog("fileDownloadFile--->", file); var filePath = rootPath + "temp.apk"; EappApi.downloadFile(filePath, newFileUrl, function (data) { if (data.status == 200) { console.log("开始下载-->", data); } else if (data.status == 201) { var per = data.progress; if (parseInt(per * 100) % 100 == 0) { console.log("下载中-->" + per + "%"); } } else if (data.status == 202) { console.log("下载完成-->", data); tempApk = data.filePath; } else if (data.status == 203) { console.log("下载失败-->", data); } else if (data.status == 400) { console.log("未知失败-->", data); } }); } // 安装应用测试 function gotoInstallAndroid() { if (IboxUtils.isEmpty(tempApk)) { showTipDialog("请先下载apk再试!"); return; } EappApi.installApk(tempApk); } // 获取GPS坐标 function getGPSoordinates() { EappApi.getCurrentPosition(function (pos) { if (pos.success) { alert(JSON.stringify(pos.result)); } else { alert(JSON.stringify(pos.error)); } }); } //-----语音转文字ios插件---- /** * 开始语音识别 * isShow 是否显示动画 * isPunc 是否显示标点符号 */ function startVoiceRecognizer() { // 权限常量 var api = EappApi.getPermissionApi(); // 检查和申请权限 EappApi.checkAndRequestPermission(api.RECORD_AUDIO, function () { EappApi.StartVoiceRecognizer(testSuccess, testFailed, true, true); }, function (error) { consoleLog("checkAndRequestPermission---->", error); }); } /** * 停止语音识别 * @constructor */ function StopVoiceRecognizer() { EappApi.StopVoiceRecognizer(); } /** * 取消语音识别 */ function cancelVoiceRecognizer() { EappApi.CancelVoiceRecognizer(); } /** * 开始语音合成 */ function startSpeak() { var msg = "大家好,我是E a p p,我是由移动、微应用组合力打造的一款基于混合开发、云端部署的移动应用开发解决方案!快来体验吧!"; EappApi.StartSpeack(speackSuccess, testFailed, msg); } /** * 暂停语音合成 */ function pauseSpeack() { EappApi.PauseSpeack(); } /** * 继续语音合成 */ function resumeSpeack() { EappApi.ResumeSpeack(); } /** * 停止语音拼写 */ function stopSpeack() { EappApi.StopSpeack(); } /** * Http Get请求 */ function HttpGet() { var url = "https://www.qshu.top/TencentVideo/app_base/checkNewVersion.do"; var options = { curVersionCode: '12', type: '1' }; EappApi.HttpRequest('get', url, options, null, function (success) { console.log("返回结果:", success); alert(success.data) }, function (error) { console.log("返回结果:", error); }); } /** * Http Post请求 */ function HttpPost() { var url = "http://zzpt.njga.gov.cn/zzyth_test/Outer/T_dwbb_info/ListDwbbFileByRecordid.do"; var options = { jgbh: '8ae5cdf56c90276e016c9349b9550011111938' }; EappApi.HttpRequest('post', url, options, null, function (success) { console.log("返回结果:", success); alert(success.data); }, function (error) { console.log("返回结果:", error); }); } function openOtherApp() { EappApi.startAPP("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"); } function testSuccess(msg) { showTipDialog('当前结果:' + msg); console.log("success:" + msg); } function speackSuccess(msg) { console.log("success:" + msg); } function testFailed(msg) { console.log("error:" + msg); } //从底部跳转原生B页面 function goBPageBottom() { var url = 'http://wg.skynjchat.com/Eapp/eapp_demo/eapp.html?v=' + timestamp; var effectFlag = EappApi.getValueByKey("sky.plugin.TYPE.BY_BOTTOM"); EappApi.goNewAppPage(url, effectFlag, function (success) { console.log("返回结果:", success); }, function (error) { console.log("返回结果:", error); }); } //从右侧跳转原生B页面 function goBPageRight() { var url = 'http://wg.skynjchat.com/Eapp/eapp_demo/eapp.html?v=' + timestamp; var effectFlag = EappApi.getValueByKey("sky.plugin.TYPE.BY_RIGHT"); EappApi.goNewAppPage(url, effectFlag, function (success) { console.log("返回结果:", success); }, function (error) { console.log("返回结果:", error); }); } /** * 退出B页面 */ function exitAppPageClick() { EappApi.exitAppPage(function (success) { console.log("exitAppPage--ok--->", success); }, function (error) { console.log("exitAppPage--error--->", error); }); } /** * -----------Eapp插件功能展示---end------------------------------------------ */ /** * -----------js功能展示---start------------------------------------------ */ // 返回上一个页面 function back() { history.go(-1); } // 刷新页面 function refeshPageTest() { EappApi.refreshPage(); timestamp = new Date().getTime(); window.localStorage.setItem("version", timestamp); loading.tip.show(); } // 检查当前运行系统,如android、ios、pc function checkOsTest() { var msg = "当前系统为:" + EappApi.curOs; showTipDialog(msg); } // js相机拍照 function jsGetPhotoFromCamera() { showTipDialog('还未实现,敬请期待!'); } // js相册取照片 function jsGetPhotoFromAlbum() { showTipDialog('还未实现,敬请期待!'); } // 刷新 function pullToRefeshPageTest(type) { // showTipDialog('还未实现,敬请期待!'); var url = ROOT_URL + "/_mescroll/demo/index.html?v=" + timestamp; if (type == '1') { EappApi.goThirdUrl(url) } else { window.location.href = url; } } // 单页面 function goSinglePageTest() { // showTipDialog('还未实现,敬请期待!'); var url = CUR_PRE_URL + "_demo/module_f7_core/index.html?theme=ios&v=11" + timestamp; // window.location.href = url; EappApi.goThirdUrl(url) } // toast function showToastTest() { webToast("测试吐司", 1000, function () { console.log("toast消失了------>") }); } // 图层显示拍照or相册图片 function showPopup(base64Data) { $(".popup").fadeIn(); if (base64Data.indexOf("data:") < 0) { base64Data = "data:text/html;base64," + base64Data; } $("#imgId").attr('src', base64Data); } // 隐藏图片 function hidePopup() { $(".popup").fadeOut(); } // 弹框通知提示 function showAlertTest() { showTipDialog('弹框告知提示', function () { console.log("知道了-------->") }); } // 弹框确认提示 function showConfirmTest() { showConfirmDialog('弹框确认提示', function () { console.log("ok-------->") }, function () { console.log("cancel-------->") }); } // 提示对话框 function showTipDialog(msg, callback) { msg = msg || '还未实现,敬请期待!'; popTipShow.alert('提示', msg, ['知道了'], function (e) { var button = $(e.target).attr('class'); if (button == 'ok') { if (callback != undefined) { callback(); } this.hide(); } } ); } // 确认对话框 function showConfirmDialog(msg, callOkBack, callCancelBack) { msg = msg || '还未实现,敬请期待!'; popTipShow.confirm('提示', msg, ["确定", "取消"], function (e) { var button = $(e.target).attr('class'); if (button == 'ok') { if (callOkBack != undefined) { callOkBack(); } this.hide(); } else if (button == 'cancel') { if (callCancelBack != undefined) { callCancelBack(); } this.hide(); } } ); } function testClick() { // console.log("testClick------->"); // EappApi.emit("EappReady"); test(); } function getFName(fn) { return (/^[\s\(]*function(?:\s+([\w$_][\w\d$_]*))?\(/).exec(fn.toString())[1] || ''; } function test() { var name = arguments.callee.name; // alert(name); console.log("log---->", ""); //console.info("info---->", ""); console.warn("warn---->", ""); //console.debug("debug---->", ""); console.error("error---->", ""); // console.exception("exception---->", ""); } //ios打开其他应用 function openIosAppTest() { if (!EappApi.isIos()) { showTipDialog("只能在ios手机上运行!"); return; } var html = "<label>选择应用:<select id='appSelectId'>"; var appTypes = EappApi.getIosAppList(); // console.log("wechat--->", appTypes["S_WEIXIN"].code); console.log("appTypes长度--->", Object.keys(appTypes).length); for (var key in appTypes) { html += "<option value =" + appTypes[key].code + ">" + appTypes[key].name + "</option>"; } html += "</select>"; if (dlg == undefined) { dlg = popTipShow.confirm('选择应用', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var value = $("#appSelectId").val(); if (null == value || "" == value) { if (Object.keys(appTypes).length == 0) { return; } value = appTypes["S_WEIXIN"].code; } EappApi.openIosApp(function () { console.info("Open---->", "成功"); }, function () { console.info("Open---->", "失败"); webToast("手机上没有安装该应用!"); }, value); } } ); } else { dlg.show(); } } /** * -----------js功能展示---end------------------------------------------ */ /** * -----------一体化定制功能---end------------------------------------------ */ // if (EappApi.isInEapp()){ // window.EappApi = window.EappApi.overrideBy("IYth.base"); // }else{ // } //设置缓存值 function setValueForApp() { var html = "<label>键:<input id = 'app_key' class='confirm_input' placeholder='请设置key:defaultKey'></label> <p /> <label><p />值:<input id ='app_value' class='confirm_input' placeholder='请设置value:defaultValue'></label>"; dlg = popTipShow.confirm('输入键值', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var key = $("#app_key").val(); if (null == key || "" == key) { key = "defaultKey"; } var value = $("#app_value").val(); if (null == value || "" == value) { value = "defaultValue"; } EappApi.setValueForApp(function (data) { console.info("set---->", data); }, function (data) { console.info("set---->", data); }, [key, value]); } } ); } //获取缓存值 function getValueFromApp() { var html = "<label>键:<input id = 'app_key' class='confirm_input' placeholder='请设置key:defaultKey'></label>"; dlg = popTipShow.confirm('输入键值', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var key = $("#app_key").val(); if (null == key || "" == key) { key = "defaultKey"; } EappApi.getValueFromApp(function (data) { alert(data); console.info("get---->", data); }, function (data) { console.info("get---->", data); }, [key]); } } ); } //IM登录 function loginIM() { var userId = "txy_cq_001"; var userSig = "eJyrVgrxCdZLrSjILEpVsjI0NTU1MjAw0AGLlqUWKVkpGekZKEH4xSnZiQUFmSlAdSYGBkaWxkZGlhCZzJTUvJLMtEywhpKKyvjkwngDA0OYvsx0oHBimqGrflW*eUiAb4pxSaKZkYdrUlBxaHpASK5PUpa-SUZUVa5belhlSVWyLVRjSWYu2FHmpsZmpsYmlrUAPcAzdA__"; var sdkAppId = "1400293229"; var params = { userId: userId, userSig: userSig, sdkAppId: sdkAppId, } var html = "<label></label>"; EappApi.loginIM(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, [params]); } //监听 function addIMListen() { EappApi.onIM("receiveMsg", function (data) { //消息监听 console.info("RECEIVE_MSG成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("RECEIVE_MSG失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); EappApi.onIM("kickoutLine", function (data) { //被踢监听 console.info("KICKOUT_LINE成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("KICKOUT_LINE失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); EappApi.onIM("sigExpired", function (data) { //sig过期 console.info("SIG_EXPIRED成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("SIG_EXPIRED失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); } //发送消息 function sendIMMessage() { var html = "<label>键:<input id = 'app_content' class='confirm_input' placeholder='测试发送内容'></label>"; dlg = popTipShow.confirm('输入键值', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var content = $("#app_content").val(); if (null == content || "" == content) { content = "测试发送内容"; } var msgJO = { "flag": "901", "sendTid": "txy_cq_002", "fromId": "0001", "fromName": "admin5", "action": "message", "content": content }; var str = JSON.stringify(msgJO); EappApi.sendIMMessage(function (data) { console.info("成功---->", data); }, function (data) { console.info("失败---->", data); }, [str]); } } ); }; //历史消息 function histroyMessage() { var info = { receiverId: "txy_cq_002", num: 3, }; EappApi.histroyMessage(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, [info]); }; //设置已读 function setReadMessage() { var info = { receiverId: "txy_cq_002", }; EappApi.setReadMessage(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, [info]); }; //获取未读数 function getUnReadMessageNum() { var info = { receiverId: "txy_cq_002", }; EappApi.getUnReadMessageNum(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, [info]); }; //最近一条数据 function getLastMsg() { var info = { receiverId: "txy_cq_002", }; EappApi.getLastMsg(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, [info]); }; //会话列表 function getConversationList() { EappApi.getConversationList(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); }; //登出IM function loginOutIM() { EappApi.loginOutIM(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); }; //设置通知数量 function setNotificationNum() { var html = "<label>键:<input id = 'app_num' class='confirm_input' placeholder='数量'></label>"; dlg = popTipShow.confirm('输入数量', html, ['确 定', '取 消'], function (e) { this.hide(); var button = $(e.target).attr('class'); if (button == 'ok') { var num = $("#app_num").val(); EappApi.setNotificationNum(function (data) { console.info("成功---->", data); }, function (data) { console.info("失败---->", data); }, [num]); } }); }; function getOnlineUser() { EappApi.getOnlineUser(function (data) { console.info("成功---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }, function (data) { console.info("失败---->", data); var html = "<label>" + data + "</label>"; dlg = popTipShow.confirm("提示", html, ['确 定'], function (e) { this.hide(); }); }); }