js与原生交互文件
base.js
注意:
1.pub.agr.detaUlr这个数组必传
2.setDetaurl() 这个函数在base.js在加载的完就要执行,才能避免有时候有时出现变量不存在的原因
3.pub 公共对象定义的原因在于“减少全局变量污染”,所以在要用到公共变量的时候,可以在pub对象里面定义属性或者扩展对象
4.新建一个 likeNative.js 里面定义要接口,该文件用来模拟js调用原生方法
5.该文件定义了项目公共的一些js函数或js调用原生函数,如果需要添加当前项目才用到的‘js函数或js调用原生函数’时,在对应位置末尾加上“”再开始添加。
var pub={};
pub.agr={};
pub.agr.dateUrl=[];
pub.selectUrl={};
pub.isAndroid=whatServer('android');
pub.isIos=whatServer('iphone');
pub.agr.dateUrl[0]=['192.168.1.12222','http://192.168.1.12222/quanshitong/public/api','http://192.168.1.12222/quanshitong/public/uploadfiles/thumb/'],
pub.agr.dateUrl[1]=['quanshitong.net','http://quanshitong.net/api','http://quanshitong.net/uploadfiles/thumb/'];
pub.selectUrl={};
setDetaurl();
$(function()
{
$('body').bind({
touchstart:function(e)
{
pub.touchState=true;
},
touchmove:function(e)
{
pub.touchState=false;
}
})
})
function likeHover(el_class,hover_class)
{
var el=$(el_class);
el.live({
'touchstart':function(e)
{
$(this).addClass(hover_class);
},
'touchmove':function(e)
{
$(this).removeClass(hover_class);
},
'touchend':function(e)
{
$(this).removeClass(hover_class);
}
});
}
function isNullObj(obj)
{
var rep=false;
for(key in obj)
{
rep=true;
}
return rep;
}
function setDetaurl()
{
var host=window.location.host;
for(var i=0;i<pub.agr.dateUrl.length;i++)
{
if(host==pub.agr.dateUrl[i][0])
{
pub.selectUrl.API_URL=pub.agr.dateUrl[i][1];
pub.selectUrl.IMG_URL=pub.agr.dateUrl[i][2];
}
}
if(isNullObj(pub.selectUrl))
{
console.log('pub.selectUrl 为空!');
}
else
{
console.log('无符合地址,请检查传入的链接是否错误!');
}
}
function getQueryStringArgs()
{
var qs=(window.location.search.length>0?window.location.search.substr(1):""),
args={},
items=qs.length?qs.split('&'):[],
item=null,
name=null,
value=null,
i=0,
len=items.length;
for(i=0;i<len;i++)
{
item=items[i].split('=');
name=decodeURIComponent(item[0]);
value=decodeURIComponent(item[1]);
if(name.length)
{
args[name]=value;
}
}
return args;
}
function hasSearch()
{
if(window.location.search.length>0)
{
return true;
}
else
{
return false;
}
}
function whatServer(serverStr)
{
var userAgent=(window.navigator.userAgent).toLocaleLowerCase(),
serverStr=serverStr.toLocaleLowerCase();
if(userAgent.indexOf(serverStr))
{
return true;
}
else
{
return false;
}
}
function sendCommand(arr)
{
var url='Interface:';
for(var i=0;i<arr.length;i++)
{
url+=arr[i]+':';
}
url=url.substr(0,url.length-1);
console.log(url);
document.location.href=url;
}
function show_loading()
{
$('.loading').show();
}
function hide_loading()
{
$('.loading').hide();
}
(function($)
{
$.fn.aiiTime=function(str,time_type,option)
{
var type=false;
var clear=false;
var countdown,$this;
$this=$(this);
try
{
if(time_type == true)
{
$this.text(countTime(str,time_type));
countdown=setInterval(function(){
if(this.clear==true)
{
clearInterval(countdown);
}
$this.text(countTime(str,time_type));
},1000);
}
else
{
$this.text(countTime(str,time_type,option));
}
}
catch(e)
{
alert('参数错误');
}
}
})(jQuery);
function aiiTime_(str,time_type,option)
{
var type=false;
var clear=false;
var countdown,backText;
try
{
backText=countTime(str,time_type,option);
return backText;
}
catch(e)
{
alert('参数错误');
}
}
function getDate(strDate)
{
var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
return date;
}
function countTime(str,type,option)
{
var data,datanow,time_dif,time_str,eval_str;
var arr=new Array(),
i=0;
data=getDate(str);
datanow=new Date();
sencond_dif=parseInt((datanow.getTime()-data.getTime())/1000);
if(type==false)
{
if(option!=undefined)
{
for(p in option)
{
arr[i]=new Array();
arr[i][0]=p;
arr[i][1]=option[p][0];
arr[i][2]=option[p][1];
i++;
}
eval_str='if(sencond_dif<arr[0][1]){time_str=arr[0][0];}';
for(var j=1;j<arr.length;j++)
{
eval_str+='else if(sencond_dif<'+arr[j][1]+'){time_str=parseInt(sencond_dif/'+arr[j][2]+') +"'+arr[j][0]+'";}'
}
console.log(arr);
console.log(arr[arr.length-1]);
eval_str+='else{time_str=parseInt(sencond_dif/'+arr[arr.length-1][1]+')+"年前";}'
eval(eval_str);
}
else
{
if (sencond_dif < 60)
{
time_str = "刚刚";
}
else if (sencond_dif < 60*60) {
time_str = parseInt(sencond_dif/60)+'分钟前';
}
else if (sencond_dif < 60*60*24) {
time_str = parseInt(sencond_dif/3600)+'小时前';
}
else if (sencond_dif < 60*60*24*30) {
time_str = parseInt(sencond_dif/86400)+'天前';
}
else if (sencond_dif<60*60*24*30*12)
{
time_str = parseInt(sencond_dif/2592000)+'个月前';
}
else
{
time_str = parseInt(sencond_dif/31536000)+'年前';
}
}
}
else
{
sencond_dif=-sencond_dif;
if(sencond_dif>0)
{
time_str=parseInt(sencond_dif/3600)>9 ? parseInt(sencond_dif/3600).toString()+":" : "0"+ parseInt(sencond_dif/3600).toString()+":";
time_str+=parseInt(sencond_dif%3600/60)>9 ? parseInt(sencond_dif%3600/60).toString()+":" : "0"+parseInt(sencond_dif%3600/60).toString()+":";
time_str+=parseInt(sencond_dif%3600%60)>9 ? parseInt(sencond_dif%3600%60).toString() :"0"+parseInt(sencond_dif%3600%60).toString();
}
else
{
time_str="00:00:00";
clear=true;
}
}
return time_str;
}
function jump_PrimaryView(obj,type)
{
var obj=JSON.stringify(obj);
if(pub.touchState) return;
try
{
if(pub.isAndroid)
{
window.Js_Interactive.jump_PrimaryView(obj,type);
}
else if(pub.isIos)
{
sendCommand(['jump_PrimaryView',obj,type]);
}
else
{
console.log('系统判断有误');
}
}
catch(e)
{
console.log(e.message);
}
}
function hasAlert(str)
{
try
{
if(pub.isAndroid)
{
window.Js_Interactive.hasAlert(str);
}
else if(pub.isIos)
{
sendCommand(['hasAlert',encodeURI(str)]);
}
else
{
console.log('系统判断有误');
}
}
catch(e)
{
console.log(e.message);
}
}
function getJsonString(data)
{
var data=JSON.stringify(data);
try
{
if(pub.isAndroid)
{
window.Js_Interactive.getJsonString(data);
}
else if(pub.isIos)
{
sendCommand(['getJsonString',encodeURI(data)]);
}
else
{
console.log('系统判断有误');
}
}
catch(e)
{
console.log(e.message);
}
}
function gotoLogin()
{
try
{
if(pub.isAndroid)
{
window.Js_Interactive.gotoLogin();
}
else if(pub.isIos)
{
sendCommand(['gotoLogin']);
}
else
{
console.log('系统判断有误');
}
}
catch(e)
{
console.log(e.message);
}
}
function Imageshow(ImgPath,Index)
{
var ImgPath=ImgPath,
Index=Index;
try
{
if(pub.isAndroid)
{
window.Js_Interactive.Imageshow(ImgPath,Index);
}
else if(pub.isIos)
{
sendCommand(['Imageshow',ImgPath,Index]);
}
else
{
console.log('系统判断有误');
}
}
catch(e)
{
console.log(e.message);
}
}