then.js is cool


 var CON_SERVER_ADDRESS = "";//服务器地址
//声明nodejs功能对象
var path = require('path');
var http = require('http');
var fs = require('fs');
var exec = require('child_process').execFile;
//var then = require('/thenjs/thenjs.js');
//初始化当前exe运行的路径,后面很多地方要用
var execPath = path.dirname(process.execPath);

//读取本地配置文件,查看是否设置了服务器地址
function ReadServerAddress_Async(next)
{
    appconfig.read("_CON_SERVER_ADDRESS", function (addr)
    {
        //console.info(addr);
        if (addr == "")
        {
            window.location.replace("choose-server.html");
        }
        else
        {
            CON_SERVER_ADDRESS = addr; //赋值
            console.info("$$ 读取服务器地址:", addr);
            next(null, CON_SERVER_ADDRESS);
        }
    });
}
//读取本地的最大版本文件,不含路径,应该返回 sonicc-9.05.zip
function GetClientMaxAppName_Async(next)
{

    fs.readdir(execPath + "\\app", function (err, files)
    {
        try
        {
            var clientVersion = 0;
            var maxVersionFileName = "";
            for (var i = 0; i < files.length; i++)
            {
                var ffn = path.basename(files[i], ".zip");
                var tVersion = parseFloat(ffn.split('-')[1]);
                //console.info("tVersion = " + tVersion);
                if (tVersion > clientVersion)
                    maxVersionFileName = ffn;
            }
            console.info("$$ 读取客户机最大版本:", maxVersionFileName);
            next(null, maxVersionFileName);

        }
        catch (ex)
        {
            _WriteError(ex);
            //出错后不回调是对的,因为退出按钮已经显示
        }
    });
}
//启动本地最新程序
function RunMaxVersionClientFileName()
{
    var then = require('./node_modules/thenjs');
    then(function (next)
    {
        GetClientMaxAppName_Async(next);          //读取客户端最大版本的文件
    })
    .then(function (next, res)
    {
        console.info("$$ 准备启动读取读取最大版本文件", res);
        var nwjsPath = execPath + "\\nw.exe ";
        var maxVerFl = execPath + "\\app\\" + res + ".zip ";

        const spawn = require('child_process').spawn;
        const child = spawn(nwjsPath, [maxVerFl], {
            detached: true, //表示启动的进程是独立的
        });
    })
}
//检查服务器版本,是不是需要下载
function CheckServerFile_Async(ClientMaxFileName, next)
{
    $.GetOnlyAsync(CON_SERVER_ADDRESS + "/w8/home/CheckNeedUpdateClient?clientFileName=" + ClientMaxFileName, true,
        function (res)
        {
            if (res.xException != null && res.xException != '')
            {
                _WriteError("连接服务器成功,但服务器内部异常:", ex);
            }
            else
            {
                next(null, res);
            }
        },
        function (res)
        {
            _WriteError("连接服务器异常:", ex);
        });
}
//下载最新文件
function DownloadFileFromServer (url, dest, next)
{
    var file = fs.createWriteStream(dest);
    var request = http.get(url, function (res)
    {
        var fsize = res.headers['content-length'];
        res.on('data', function (data)
        {
            file.write(data);
            var progressCounter = Math.round(100 - (((fsize - file.bytesWritten) / fsize) * 100), 2);
            $("#xProgressInfo").html(progressCounter + "%");
        });
        res.on('end', function ()
        {
            file.end();
            next(null, null);
        });
    }).on('error', function (err)
    {
        fs.unlink(dest);
        next(null, err.message);
    });
};

$(document).ready(function ()
{
    var then = require('./node_modules/thenjs');
    then(function (next)
    {
        ReadServerAddress_Async(next);          //读取服务器地址,如果没有则转向
    })
    .then(function (next, res)
    {
        GetClientMaxAppName_Async(next);        //读取客户端最大版本的文件
    })
    .then(function (next, res)
    {
        console.info("$$ 客户机最大版本号", res);
        CheckServerFile_Async(res, next)
    })
    .then(function (next, res)
    {
        console.info("$$ 看看是否需要下载 = " , res);
        if (res.xNeed)
        {
            _WriteMsg("下载文件:" + res.xFileName);
            DownloadFileFromServer(CON_SERVER_ADDRESS + "/w8/zo-content/client-down-package/" + res.xFileName,execPath + "\\app\\" + res.xFileName,next);
        }
        else
        {
            _WriteMsg("无需更新,准备启动...");
            next(null, null);
        }
    })
    .then(function (next, res)
    {
        console.info("$$ 准备启动");
        RunMaxVersionClientFileName();
    })
});




//辅助性函数//
function _WriteMsg(msg)
{
    $("#xsubMsg").html(msg);

}
function _WriteError(msg, ex)
{
    $("#xsubMsg").html(msg + "<br>" + ex);
    $("#btnQuitAndClear,#btnQuit").show();
}
//
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值