自动更新作用

时间:2021-05-18

复制代码 代码如下:
functionCLASS_AUTOUPDATE(instanceName,win)
{
this.instanceName=instanceName;
this.updating=false;
this.error=false;
this.updateInfo="";
this.nextVersion="";
this.ver="";
this.win=win;
this.updatePath="";
this.updateFileName="";
this.softName="";
this.refresh();

}
CLASS_AUTOUPDATE.prototype.onafterupdate=function()
{

var_window=this.win;
vartempMessageWidth=360;
vartempMessageHeight=160;
vartempMessageLeft=(document.body.clientWidth-tempMessageWidth)/2;
vartempMessageTop=(document.body.clientHeight-tempMessageHeight)/2;
varfeature={
title:"更新完毕",
width:tempMessageWidth,
height:tempMessageHeight,
left:tempMessageLeft,
top:tempMessageTop,
borderColor:"#8B89A1",
backgroundColor:"#FFFFFF",
color:"#000000",
titleBackgroundColor:"#8B89A1",
titleColor:"#FFFFFF",
dragable:true
};

varTHIS=this;

vartempMessageContent=[];

//tempMessageContent[tempMessageContent.length]="<divstyle='font-size:12px;padding:5px'>";
tempMessageContent[tempMessageContent.length]=this.updateInfo;
//tempMessageContent[tempMessageContent.length]="<br/><br/>";
tempMessageContent[tempMessageContent.length]="<divalign=center><spanclass='bt'><inputtype='button'value='OK'class='bt'id='bt_update_ok'></span></div>";
tempMessageContent[tempMessageContent.length]="</div>";

_window.open("updateOk",feature);
_window.write("updateOk",tempMessageContent.join(""),false);

_window.items["updateOk"].all("bt_update_ok").onclick=function()
{
_window.close("updateOk");
}
_window.items["updateOk"].onunload=function()
{
if(THIS.error==false&&THIS.updateFileList.length>0)
{
document.location.reload();
}
}

}
CLASS_AUTOUPDATE.prototype.check=function()
{
var_current_version,_new_version,_exist_new_ver;


//检查配置文件
varconfigXML=newCLASS_XML("xml\\autoupdate.xml");
if(configXML.error==false)
{
var_update_date,_autoupdate_day,_current_version;

var_save_or_not;

var_d=newDate();
var_dd=_d.getFullYear()+"-"+(_d.getMonth()+1)+"-"+_d.getDate();

//检查是否有更新时间段设置
if(configXML.selectSingleNode("/Config/UpdateDate")==null)
{
configXML.setText("/Config","UpdateDate",_dd);

_update_date=_dd;
_save_or_not=true;
}
else
{
_update_date=configXML.getText("/Config","UpdateDate","1900-01-01");
}

if(configXML.selectSingleNode("/Config/AutoUpdateDay")==null)
{
configXML.setText("/Config","AutoUpdateDay","10");

_autoupdate_day="10";
_save_or_not=true;
}
else
{
_autoupdate_day=configXML.getText("/Config","AutoUpdateDay","10");
}
_autoupdate_day=_autoupdate_day*1;

if(configXML.selectSingleNode("/Config/CurrentVersion")==null)
{
configXML.setText("/Config","CurrentVersion","0.32");

_current_version="0.32";
_save_or_not=true;
}
else
{
_current_version=configXML.getText("/Config","CurrentVersion","0.32");
}
_current_version=_current_version*1;


//判断是否今天去连接服务器
var_od=newDate(_update_date.replace(/-/g,"\/"));
if(_d.getTime()-_od.getTime()>_autoupdate_day*24*3600*1000)
{

varnewDoc=newCLASS_XML();
newDoc.loadRemoteFile(this.updatePath+this.softName+"/"+this.updateFileName);

if(newDoc.selectSingleNode("/Config/CurrentVersion")==null)
{
newDoc.setText("/Config","CurrentVersion","0.32");
_new_version="0.32";
}
else
{
_new_version=newDoc.getText("/Config","CurrentVersion","0.32");
}
_new_version=_new_version*1;

//检查是否有新版本
if(_new_version>_current_version)
{
_exist_new_ver=true;
}

configXML.setText("/Config","UpdateDate",_dd);
_save_or_not=true;
}

if(_save_or_not)
{
configXML.save();
}

}

if(_exist_new_ver)
{
var_window=this.win;
vartempMessageWidth=260;
vartempMessageHeight=120;
vartempMessageLeft=(document.body.clientWidth-tempMessageWidth)/2;
vartempMessageTop=(document.body.clientHeight-tempMessageHeight)/2;
varfeature={
title:"升级",
width:tempMessageWidth,
height:tempMessageHeight,
left:tempMessageLeft,
top:tempMessageTop,
borderColor:"#8B89A1",
backgroundColor:"#FFFFFF",
color:"#000000",
titleBackgroundColor:"#8B89A1",
titleColor:"#FFFFFF",
dragable:true
};

varTHIS=this;

vartempMessageContent=[];

//tempMessageContent[tempMessageContent.length]="<divstyle='font-size:12px;padding:5px'>";
tempMessageContent[tempMessageContent.length]="<tablewidth='100%'height='60'cellPadding='10'><tr><tdvalign='top'>有新版本,是否更新?<!--insert//--></td></tr></table>";
//tempMessageContent[tempMessageContent.length]="<br/><br/>";
tempMessageContent[tempMessageContent.length]="<divalign=center><spanclass='bt'><inputtype='button'value='Yes'class='bt'id='bt_update_yes'></span>&nbsp;&nbsp;<spanclass='bt'><inputtype='button'value='No'class='bt'id='bt_update_no'></span></div>";
tempMessageContent[tempMessageContent.length]="</div>";

_window.open("update_or_not",feature);
_window.write("update_or_not",tempMessageContent.join(""),false);

THIS.ver=_current_version;

_window.items["update_or_not"].all("bt_update_yes").onclick=function()
{
THIS.ver=_new_version;
_window.close("update_or_not");

THIS.update();
}
_window.items["update_or_not"].all("bt_update_no").onclick=function()
{
_window.close("update_or_not");
}
_window.items["update_or_not"].onunload=function()
{

}

}


}

CLASS_AUTOUPDATE.prototype.update=function()
{

this.updating=!this.updating;

vart=newDate();
varTHIS=this;

if(this.error==false)
{

//得到新配置文档
varnewDoc=newCLASS_XML();
newDoc.loadRemoteFile(this.updatePath+this.softName+"/"+this.updateFileName);

this.error=newDoc.error;

if(this.error)
{
THIS.updating=false;
THIS.updateInfo="<tablewidth='100%'height='100'cellPadding='10'><tr><tdvalign='top'>连接服务器失败<!--insert//--></td></tr></table>";
}
else
{
varupdateFileNodes=newDoc.selectNodes("//SystemFile");
varupdateInfoNode=newDoc.selectSingleNode("//UpdateInfo");
varnextVersion=newDoc.selectSingleNode("//nextversion");

if(updateInfoNode!=null)
{
THIS.updateInfo=updateInfoNode.childNodes[0].text;
}

if(nextVersion!=null)
{
THIS.nextVersion=nextVersion.childNodes[0].text;
}

THIS.updateFileList=[];

for(vari=0;i<updateFileNodes.length;i++)
{
varcurUpdateFileName=updateFileNodes[i].getAttribute("name").toLowerCase();
varcurUpdateFileVersion=updateFileNodes[i].getAttribute("version").toLowerCase();
varcurUpdateFilePath=updateFileNodes[i].getAttribute("path").toLowerCase();

if(THIS.systemFiles[curUpdateFileName]==null||THIS.systemFiles[curUpdateFileName]!=curUpdateFileVersion)
{
//本地没有该文件或者版本号不一致
THIS.updateFileList[THIS.updateFileList.length]=
{
name:curUpdateFileName,
version:curUpdateFileVersion,
path:curUpdateFilePath,
type:"text"
};
}
}

newDoc.dispose();


if(THIS.updateFileList.length==0)
{
THIS.updating=false;
THIS.updateInfo="<tablewidth='100%'height='100'cellPadding='10'><tr><tdvalign='top'>没有需要更新的文件<!--insert//--></td></tr></table>";
}
else
{
varconfigXML=newCLASS_XML("xml\\autoupdate.xml");
//添加更新信息
if(configXML.error==false&&THIS.updateInfo.length>0)
{
var_updateInfo=configXML.createElement("UpdateInfo");
var_cdata=configXML.createCDATASection(THIS.updateInfo);

_updateInfo.appendChild(_cdata);
configXML.root().appendChild(_updateInfo);

//添加下版本预告
var_nextVersion=configXML.selectSingleNode("//nextversion");
if(_nextVersion==null)
{
var__next=configXML.createElement("nextversion");
var__cdata=configXML.createCDATASection(THIS.nextVersion);

__next.appendChild(__cdata);

configXML.root().appendChild(__next);
}
else
{
if(_nextVersion.childNodes.length==0)
{
_nextVersion.appendChild(configXML.createCDATASection(""));
}
_nextVersion.childNodes[0].text=THIS.nextVersion;
}

configXML.setText("/Config","CurrentVersion",THIS.ver);

configXML.save();
}
configXML.dispose();

THIS.updateFile();
}
}
}
else
{
this.updateInfo="<tablewidth='100%'height='100'cellPadding='10'><tr><tdvalign='top'>加载配置文件失败!<!--insert//--></td></tr></table>";
}
this.updating=false;
this.onafterupdate();

}

CLASS_AUTOUPDATE.prototype.updateFile=function(curUpdateId)
{

vart=newDate();
varTHIS=this;

for(vari=0;i<this.updateFileList.length;i++)
{
varcurUpdateFile=this.updateFileList[i];

if(curUpdateFile.type=="base64")
{
varurl=this.updatePath+this.softName+"/base64.aspx?src="+curUpdateFile.name+"&t="+t.valueOf();
}
elseif(curUpdateFile.type=="text")
{
varurl=this.updatePath+this.softName+"/"+curUpdateFile.path+"/"+curUpdateFile.name;
}

//下载文件
varerrors=this.downloadFile(url,curUpdateFile.path,curUpdateFile.name);

if(this.error==false)
{
//记录当前版本信息
varconfigXML=newCLASS_XML("xml\\autoupdate.xml");
if(configXML.error==false)
{
varsystemFiles=configXML.selectSingleNode("//SystemFiles");
if(systemFiles==null)
{
systemFiles=configXML.createElement("SystemFiles");
configXML.selectSingleNode("/Config").appendChild(systemFiles);
}

varsystemFile=configXML.selectSingleNode("//SystemFiles/SystemFile[@name='"+curUpdateFile.name.toLowerCase()+"']");

if(systemFile==null)
{
systemFile=configXML.createElement("SystemFile");
systemFile.setAttribute("name",curUpdateFile.name.toLowerCase());
systemFile.setAttribute("path",curUpdateFile.path.toLowerCase());
configXML.selectSingleNode("//SystemFiles").appendChild(systemFile);
}
systemFile.setAttribute("version",curUpdateFile.version);

configXML.save();
configXML.dispose();

this.error=false;
this.updateInfo+=errors+"\n";
}
else
{
this.updateInfo+="<tablewidth='100%'height='100'cellPadding='10'><tr><tdvalign='top'>加载配置文件出错!<!--insert//--></td></tr></table>";
}
}
}

}

CLASS_AUTOUPDATE.prototype.downloadFile=function(url,path,filename)
{
try
{
varxmlHTTP=newActiveXObject("Microsoft.XMLHTTP");

xmlHTTP.open("Get",url,false);
xmlHTTP.send();

if(xmlHTTP.readyState==4&&xmlHTTP.status==200)
{
varadodbStream=newActiveXObject("ADODB"+"."+"Stream");

//判断目录是否存在,不存在则创建
this.buildPath(path);

varstrLocalURL=path.length>0?path+"\\"+filename:filename;

//1=adTypeBinary
adodbStream.Type=1;
adodbStream.Open();
adodbStream.write(xmlHTTP.responseBody);
adodbStream.SaveToFile(strLocalURL,2);
adodbStream.Close();

adodbStream=null;
xmlHTTP=null;
}
else
{
this.error=true;
returnfilename+"下载失败";
}

return"";
}
catch(e)
{
this.error=true;
returne.description;
}
}
CLASS_AUTOUPDATE.prototype.refresh=function()
{
varconfigXML=newCLASS_XML("xml\\autoupdate.xml");
if(configXML.error==false)
{
var_softName=configXML.selectSingleNode("/Config/SoftName");
var_updatePath=configXML.selectSingleNode("/Config/UpdatePath");
var_updateFileName=configXML.selectSingleNode("/Config/UpdateFileName");

this.updatePath=_updatePath!=null?_updatePath.text:"http://dooit.3322.org/";
this.softName=_softName!=null?_softName.text:"cc";
this.updateFileName=_updateFileName!=null?_updateFileName.text:"autoupdate.xml";

this.systemFiles={};
vartempSystemFileNodes=configXML.selectNodes("/Config/SystemFiles/SystemFile");
for(vari=0;i<tempSystemFileNodes.length;i++)
{
this.systemFiles[tempSystemFileNodes[i].getAttribute("name")]=tempSystemFileNodes[i].getAttribute("version");
}
}
else
{
this.error=true;
}
configXML.dispose();
}
CLASS_AUTOUPDATE.prototype.buildPath=function(path)
{
var_baseUrl=unescape(document.location.href.substring(document.location.href.lastIndexOf("/")+1,-1)).replace(/^file\:\/\/\//i,"").replace(/\//g,"\\");
var_path=path.replace(/^\s*|\s*$/g,"");
var_fullPath="";

//得到全路径
if(/^\w\:\\/.test(_path)==false){
_fullPath=_baseUrl+_path;
}else{
_fullPath=_path;
}

varp=_fullPath.split(/\\|\//g);
varfso=newActiveXObject("Scripting.FileSystemObject");

vart="";

for(vari=0;i<p.length;i++){
t+=p[i]+"\\";

try
{
if(!fso.FolderExists(t))
{
fso.CreateFolder(t);
}
}
catch(e)
{
returnfalse;
}
}

returntrue;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章