自动更新作用

2016-02-19 12:01 8 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐自动更新作用,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - Web开发 】

代码如下:

function CLASS_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;
    var tempMessageWidth    = 360;
    var tempMessageHeight    = 160;
    var tempMessageLeft        = (document.body.clientWidth - tempMessageWidth) /2;
    var tempMessageTop        = (document.body.clientHeight - tempMessageHeight) /2;
    var feature = {
        title:"更新完毕",
        width:tempMessageWidth,
        height:tempMessageHeight,
        left:tempMessageLeft,
        top:tempMessageTop,
        borderColor:"#8B89A1",
        backgroundColor:"#FFFFFF",
        color:"#000000",
        titleBackgroundColor:"#8B89A1",
        titleColor:"#FFFFFF",
        dragable:true
    };

    var THIS = this;

    var tempMessageContent    = [];

    //tempMessageContent[tempMessageContent.length] = "div style='font-size:12px;padding:5px'";
    tempMessageContent[tempMessageContent.length] = this.updateInfo;
    //tempMessageContent[tempMessageContent.length] = "br/br/";
    tempMessageContent[tempMessageContent.length] = "div align=centerspan class='bt'input type='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.length0)
                                                {
                                                    document.location.reload();
                                                }
                                            }

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


    //检查配置文件
    var configXML = new CLASS_XML("xmlautoupdate.xml");
    if(configXML.error==false)
    {
        var _update_date,_autoupdate_day,_current_version;

        var _save_or_not;

        var _d  = new Date();
        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 = new Date(_update_date.replace(/-/g, "/"));
        if(_d.getTime()-_od.getTime()_autoupdate_day*24*3600*1000)
        {

            var newDoc        = new CLASS_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;
        var tempMessageWidth    = 260;
        var tempMessageHeight    = 120;
        var tempMessageLeft        = (document.body.clientWidth - tempMessageWidth) /2;
        var tempMessageTop        = (document.body.clientHeight - tempMessageHeight) /2;
        var feature = {
            title:"升级",
            width:tempMessageWidth,
            height:tempMessageHeight,
            left:tempMessageLeft,
            top:tempMessageTop,
            borderColor:"#8B89A1",
            backgroundColor:"#FFFFFF",
            color:"#000000",
            titleBackgroundColor:"#8B89A1",
            titleColor:"#FFFFFF",
            dragable:true
        };

        var THIS = this;

        var tempMessageContent    = [];

        //tempMessageContent[tempMessageContent.length] = "div style='font-size:12px;padding:5px'";
        tempMessageContent[tempMessageContent.length] = "table width='100%' height='60' cellPadding='10'trtd valign='top'有新版本,是否更新?!--insert//--/td/tr/table";
        //tempMessageContent[tempMessageContent.length] = "br/br/";
        tempMessageContent[tempMessageContent.length] = "div align=centerspan class='bt'input type='button' value=' Yes ' class='bt' id='bt_update_yes'/span  span class='bt'input type='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;

    var t            = new Date();
    var THIS        = this;

    if(this.error == false)
    {

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

        this.error = newDoc.error;

        if(this.error)
        {
            THIS.updating    = false;
            THIS.updateInfo    = "table width='100%' height='100' cellPadding='10'trtd valign='top'连接服务器失败!--insert//--/td/tr/table";
        }
        else
        {
            var updateFileNodes = newDoc.selectNodes("//SystemFile");
            var updateInfoNode    = newDoc.selectSingleNode("//UpdateInfo");
            var nextVersion        = newDoc.selectSingleNode("//nextversion");

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

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

            THIS.updateFileList = [];            

            for(var i=0;iupdateFileNodes.length;i++)
            {
                var curUpdateFileName        = updateFileNodes[i].getAttribute("name").toLowerCase();
                var curUpdateFileVersion    = updateFileNodes[i].getAttribute("version").toLowerCase();
                var curUpdateFilePath        = 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    = "table width='100%' height='100' cellPadding='10'trtd valign='top'没有需要更新的文件!--insert//--/td/tr/table";
            }
            else
            {
                var configXML    = new CLASS_XML("xmlautoupdate.xml");
                //添加更新信息
                if(configXML.error==false&&THIS.updateInfo.length0)
                {
                    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 = "table width='100%' height='100' cellPadding='10'trtd valign='top'加载配置文件失败!!--insert//--/td/tr/table";
    }
    this.updating = false;
    this.onafterupdate();

}

CLASS_AUTOUPDATE.prototype.updateFile = function(curUpdateId)
{

    var t        = new Date();
    var THIS    = this;

    for(var i= 0;ithis.updateFileList.length;i++)
    {
        var curUpdateFile = this.updateFileList[i];

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

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

        if(this.error==false)
        {
            //记录当前版本信息                    
            var configXML    = new CLASS_XML("xmlautoupdate.xml");
            if(configXML.error==false)
            {
                var systemFiles = configXML.selectSingleNode("//SystemFiles");
                if(systemFiles    == null)
                {
                    systemFiles = configXML.createElement("SystemFiles");
                    configXML.selectSingleNode("/Config").appendChild(systemFiles);
                }

                var systemFile    = 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 += "table width='100%' height='100' cellPadding='10'trtd valign='top'加载配置文件出错!!--insert//--/td/tr/table";
            }
        }
    }

}

CLASS_AUTOUPDATE.prototype.downloadFile = function(url,path,filename)
{
    try
    {
        var xmlHTTP    =    new ActiveXObject("Microsoft.XMLHTTP");

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

        if(xmlHTTP.readyState==4&&xmlHTTP.status==200)
        {
            var adodbStream = new ActiveXObject("ADODB" + "." + "Stream");

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

            var strLocalURL = path.length0?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;
            return    filename + "下载失败";
        }

        return "";
    }
    catch(e)
    {
        this.error        = true;
        return    e.description;
    }
}
CLASS_AUTOUPDATE.prototype.refresh = function()
{
    var configXML = new CLASS_XML("xmlautoupdate.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 = {};
        var tempSystemFileNodes = configXML.selectNodes("/Config/SystemFiles/SystemFile");
        for(var i=0;itempSystemFileNodes.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;
    }

    var p    = _fullPath.split(/|//g);
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    var t    = "";

    for(var i=0;ip.length;i++){
        t += p[i] + "";

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

    return true;
}

来源:http://www.tulaoshi.com/n/20160219/1599499.html

延伸阅读
标签: windows 操作系统
近期单位一些安装了Windows XP的电脑在登录时,系统提示“此媒体类型的产品号无效。请用正确的安装CD重新安装你的系统,错误代码:0x80040509”。无论用户使用什么用户名(包括Administrator)都无法正常进入桌面。用Windows XP安装光盘修复所需时间比较长,且个别电脑的C盘内容保不住。好在使用“安全模式”可以进入,经过研究,最终在“...
如何关闭iphone软件自动更新   进入设置-iTunes Store与App StorTuLaoShi.come,把自动下载的项目关闭即可 希望以上两个步骤能解决各位关于软件自动更新的问题,以后图老师小编都会给微会员们带来iPhone询问度较高问题的解决办法,您有什么希望图老师小编帮您解答的欢迎发送过来。 iPhone/ipadTulaoshi.Com ios8 siri怎么打...
标签: 软件教程
不知道大家是否和小编有同样的喜好,小编很反感win7的自动更新每次都是关机的时候显示你的系统要更新看着很郁闷,下面小编就教大家怎么关闭win7的自动更新 1、首先我们打开“控制面板“ 2、点击“系统和安全” 3、然后选择“Windows Update” 4、选择“更改设置” 5、选择“从不检查更新”、 ...
第一种方法: 第一步:在电脑桌面找到我的电脑,鼠标右键点击。在下拉菜单中打开属性。   第二步:然后出现系统书信,我们切换到自动更新,选择关闭自动更新。确定。   第二种方法: 第一步:在电脑桌面打开开始,点击运行。   ...
标签: windows10
打开“控制面板” 首先,在这台电脑上单击右键,选择属性。打开系统属性对话框。或者您可以通过键盘上的windows键+PauseBreak的快捷键迅速打开系统属性。如下图。在打开的系统属性中,单击左上角的“控制面板”。 点击“系统和安全” 在电脑的控制面板界面,点击“系统和安全”,如下图。 单击“启用或关闭自动更新” 在弹出的对话...

经验教程

98

收藏

48
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部