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();
}
//
then.js is cool
最新推荐文章于 2024-02-15 18:51:02 发布