自动更新作用


Posted in Javascript onOctober 08, 2006
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=center><span 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.length>0) 
                                                { 
                                                    document.location.reload(); 
                                                } 
                                            } 
} 
CLASS_AUTOUPDATE.prototype.check = function() 
{ 
    var _current_version,_new_version,_exist_new_ver; 

    //检查配置文件 
    var configXML = new CLASS_XML("xml\\autoupdate.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'><tr><td valign='top'>有新版本,是否更新?<!--insert//--></td></tr></table>"; 
        //tempMessageContent[tempMessageContent.length] = "<br/><br/>"; 
        tempMessageContent[tempMessageContent.length] = "<div align=center><span 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'><tr><td 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;i<updateFileNodes.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'><tr><td valign='top'>没有需要更新的文件<!--insert//--></td></tr></table>"; 
            } 
            else 
            { 
                var configXML    = new CLASS_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 = "<table width='100%' height='100' cellPadding='10'><tr><td 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;i<this.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("xml\\autoupdate.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'><tr><td 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.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; 
            return    filename + "下载失败"; 
        } 
        return ""; 
    } 
    catch(e) 
    { 
        this.error        = true; 
        return    e.description; 
    } 
} 
CLASS_AUTOUPDATE.prototype.refresh = function() 
{ 
    var configXML = new CLASS_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 = {}; 
        var tempSystemFileNodes = configXML.selectNodes("/Config/SystemFiles/SystemFile"); 
        for(var i=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; 
    } 
    var p    = _fullPath.split(/\\|\//g); 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var t    = ""; 
    for(var i=0;i<p.length;i++){ 
        t += p[i] + "\\"; 
        try 
        { 
            if(!fso.FolderExists(t)) 
            { 
                fso.CreateFolder(t); 
            } 
        } 
        catch(e) 
        { 
            return false; 
        } 
    } 
    return true; 
}
Javascript 相关文章推荐
修改jQuery.Autocomplete插件 支持中文输入法 避免TAB、ENTER键失效、导致表单提交
Oct 11 Javascript
jquery.messager.js插件导致页面抖动的解决方法
Jul 14 Javascript
js 操作符汇总
Nov 08 Javascript
详谈JavaScript内存泄漏
Nov 14 Javascript
基于jQuery1.9版本如何判断浏览器版本类型
Jan 12 Javascript
30分钟快速掌握Bootstrap框架
May 24 Javascript
Node.js中常规的文件操作总结
Oct 13 Javascript
javascript工厂模式和构造函数模式创建对象方法解析
Dec 30 Javascript
Node.js中如何合并两个复杂对象详解
Dec 31 Javascript
angular+ionic 的app上拉加载更新数据实现方法
Jan 16 Javascript
JavaScript 实现 Tab 点击切换实例代码
Mar 25 Javascript
基于vue v-for 循环复选框-默认勾选第一个的实现方法
Mar 03 Javascript
许愿墙中用到的函数
Oct 07 #Javascript
解放web程序员的输入验证
Oct 06 #Javascript
通过ifame指向的页面高度调整iframe的高度
Oct 05 #Javascript
javascript字典探测用户名工具
Oct 05 #Javascript
TFDN图片播放器 不错自动播放
Oct 03 #Javascript
DOM精简教程
Oct 03 #Javascript
JavaScript去除空格的几种方法
Oct 03 #Javascript
You might like
php gzip压缩输出的实现方法
2013/04/27 PHP
thinkphp在模型中自动完成session赋值示例代码
2014/09/09 PHP
php实现文件管理与基础功能操作
2017/03/21 PHP
浅谈Laravel中的三种中间件的作用
2019/10/13 PHP
Prototype使用指南之base.js
2007/01/10 Javascript
jquery遍历数组与筛选数组的方法
2013/11/05 Javascript
jQuery$命名冲突怎么办如何解决
2014/01/16 Javascript
seajs中模块的解析规则详解和模块使用总结
2014/03/12 Javascript
基于jQuery实现左右图片轮播(原理通用)
2015/12/24 Javascript
深究AngularJS如何获取input的焦点(自定义指令)
2017/06/12 Javascript
浅谈JS中的常用选择器及属性、方法的调用
2017/07/28 Javascript
vue数字类型过滤器的示例代码
2017/09/07 Javascript
Vue组件之Tooltip的示例代码
2017/10/18 Javascript
前端必备插件之纯原生JS的瀑布流插件Macy.js
2017/11/22 Javascript
jQuery实现table表格checkbox全选的方法分析
2018/07/04 jQuery
JS通过位运算实现权限加解密
2018/08/14 Javascript
详解关于Vue版本不匹配问题(Vue packages version mismatch)
2018/09/17 Javascript
浅谈 Webpack 如何处理图片(开发、打包、优化)
2019/05/15 Javascript
vue动态子组件的两种实现方式
2019/09/01 Javascript
浅析vue-cli3配置webpack-bundle-analyzer插件【推荐】
2019/10/23 Javascript
JS对日期操作封装代码实例
2019/11/08 Javascript
Element-ui 自带的两种远程搜索(模糊查询)用法讲解
2021/01/29 Javascript
Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解
2019/04/26 Python
python基于K-means聚类算法的图像分割
2019/10/30 Python
Python程序暂停的正常处理方法
2019/11/07 Python
Python argparse模块应用实例解析
2019/11/15 Python
详解python变量与数据类型
2020/08/25 Python
python代码实现猜拳小游戏
2020/11/30 Python
html5适合移动应用开发的12大特性
2014/03/19 HTML / CSS
美国最大的珠宝商之一:Littman Jewelers
2016/11/13 全球购物
I.T集团香港官方商城:ITeSHOP.com Hong Kong
2019/02/15 全球购物
婚礼证婚人证婚词
2014/01/13 职场文书
给儿子的表扬信
2014/01/15 职场文书
年会活动策划方案
2014/01/23 职场文书
三八红旗手事迹材料
2014/12/26 职场文书
教师节老师寄语
2015/05/28 职场文书