var CON_SERVER_ADDRESS = "" ;
var path = require ('path' );
var http = require ('http' );
var fs = require ('fs' );
var exec = require ('child_process' ).execFile;
var execPath = path.dirname(process.execPath);
function ReadServerAddress_Async (next)
{ appconfig.read("_CON_SERVER_ADDRESS" , function (addr) {if (addr == "" ){window.location.replace("choose-server.html" );}else {CON_SERVER_ADDRESS = addr; console.info("$$ 读取服务器地址:" , addr);next(null , CON_SERVER_ADDRESS);}});
}
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 ]);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();
}