/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  https://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */
'use strict';$(document).ready(function(){$('#op_clean').bind('click',function(){windowModal('清理缓存',ulink('setting/clean'))});$('#a_run_auto_backstage').bind('click',function(){windowModal('正在激活自动采集...',ulink('admin/backstage/run_auto_backstage'))});$('#a_collect_now').bind('click',function(){globalOp.collectorWindow('实时采集','admin/backstage/collect',null,{lg:1})});$('#upgrade_db').bind('click',function(){var obj=$(this);ajaxOpen({type:'get',dataType:'json',url:ulink('install/upgrade/db'),success:function(data){if(data.code==1){obj.html(data.msg?data.msg:'升级成功');window.setTimeout(function(){window.location.reload()},1000)}else{obj.html(data.msg?data.msg:'升级失败')}}})});$('body').on('click','#op_upgrade',function(){var obj=$(this);if(obj.attr('upgrading')==1){return!1}
$('#upgrade_error').html('').hide();var versionFile=obj.attr('data-version-file');if(versionFile=='zip'){obj.html('正在下载压缩包...')}else{obj.html('正在检索更新文件...')}
ajaxOpen({type:'get',dataType:'json',url:ulink('upgrade/download?version_file=_vfile_',{'_vfile_':versionFile}),success:function(data){obj.attr('upgrading',1);if(versionFile=='zip'){if(data.code==1){data=data.data?data.data:{};var size=toInt(data.size);size=size/(1024*1024);size=Math.floor(size*100)/100;obj.html('正在下载... &nbsp;<span class="size">0MB</span> / '+size+'MB');var upgradeZipClass=new UpgradeZipClass(data.size,data.blocks);upgradeZipClass.down_zip(1)}else{obj.html(data.msg?data.msg:'压缩包下载失败')}}else{if(data.code==1){var fileList=new Array();if(data.data.files){for(var i in data.data.files){fileList.push(data.data.files[i])}}
if(fileList.length>0){obj.html('正在更新...');var upgradeClass=new UpgradeClass(fileList);upgradeClass.down_file(0)}else{obj.html('没有需要更新的文件')}}else{var upgradeClass=new UpgradeClass(null);upgradeClass.down_complete()}}}});return!1});$('#refresh_admin_index').bind('click',function(){$('#skycaiji_admin_index').parents('.box').eq(0).find('.overlay').show();ajaxOpen({type:'get',dataType:'json',async:!0,timeout:10000,url:ulink('backstage/adminIndex?refresh=1'),success:function(data){var html=data.html?data.html:'';$('#skycaiji_admin_index').html(html);$('#skycaiji_admin_index').parents('.box').eq(0).find('.overlay').hide()},error:function(){var times=$('#refresh_admin_index').attr('data-times');times=toInt(times);if(times<3){$('#refresh_admin_index').attr('data-times',times+1).trigger('click')}}})});$('#box_open_basedir .close').bind('click',function(){confirmRight('忽略该问题？',function(){ajaxOpen({type:'get',dataType:'json',url:ulink('backstage/ignoreOpenBasedir'),success:function(data){$('#box_open_basedir').remove()}})})});$('#upgrade_check').html('正在检测更新...');ajaxOpen({type:'get',dataType:'json',async:!0,timeout:10000,url:ulink('backstage/newVersion'),success:function(data){data=data.data;if(data){if(data.is_new_version){var html='<a href="javascript:;" id="op_upgrade" data-version-file="'+(data.version_file?data.version_file:'')+'">检测到新版本V'+data.new_version+'点击升级</a>';if(data.version_link){html+=' &nbsp;<a href="'+data.version_link[1]+'" target="_blank" style="color:red;">'+data.version_link[0]+'</a>'}
$('#upgrade_check').html(html)}else{$('#upgrade_check').html('暂无更新')}
if(data.is_new_admin_index){$('#refresh_admin_index').trigger('click')}}else{$('#upgrade_check').html('')}},error:function(){$('#upgrade_check').html('<a href="javascript:;" onclick="window.location.reload();">检测失败</a>')}});$('.content-header h1').append('<div class="loading-sm" style="margin-left:6px;"></div>');ajaxOpen({type:'get',dataType:'json',async:!0,timeout:60000,url:ulink('backstage/checkUp'),success:function(data){data=data.data;if(data){var list=[];if(data.cliPhpVersion){$('#cli_php_version').show().find('span').html(data.cliPhpVersion)}
if(data.swoolePhpVersion){$('#swoole_php_version').show().find('span').html(data.swoolePhpVersion)}
if(data.phpInvalid){list.push('<a href="'+ulink('setting/caiji')+'" target="_blank">cli命令行</a>')}
if(data.swooleInvalid){list.push('<a href="'+ulink('setting/caiji')+'" target="_blank">swoole服务</a>')}
if(data.swoolePhpInvalid){list.push('<a href="'+ulink('setting/caiji')+'" target="_blank">swoole快捷启动</a>')}
if(data.pageRenderInvalid){list.push('<a href="'+ulink('setting/page_render')+'" target="_blank">页面渲染</a>')}
if(list.length>0){list=list.join(' , ');$('#invalid_list').find('[data-box]').html(list);$('#invalid_list').fadeIn()}
if(data.repairTables){$('#repair_tables').find('[data-box]').html(data.repairTables);$('#repair_tables').fadeIn();$('#repair_tables button').bind('click',function(){windowModal('正在修复表...',ulink('backstage/repairTables'),{ajax:{type:'post',data:{'tables':data.repairTables}}})})}
if(data.tongji){for(var i in data.tongji){$('#data_'+i).text(data.tongji[i])}}}},complete:function(){$('.content-header h1').find('.loading-sm').remove()}})});function UpgradeZipClass(size,blocks){this.size=size?toInt(size):0;this.blocks=blocks?toInt(blocks):0;this.nextBlockNo=0;this.existBlocks=0}
UpgradeZipClass.prototype={constructor:UpgradeZipClass,down_zip:function(blockNo){var $_o=this;blockNo=toInt(blockNo);$_o.nextBlockNo=0;var url=ulink('upgrade/downZip?block_no='+blockNo);ajaxOpen({type:'get',dataType:'json',url:url,success:function(data){if(data.code==1){var dataData=data.data;$_o.nextBlockNo=toInt(dataData.next_block_no);$_o.existBlocks=toInt(dataData.exist_blocks);var existSize=toInt(dataData.exist_size);existSize=existSize/(1024*1024);existSize=Math.floor(existSize*100)/100;$('#op_upgrade').find('.size').text(existSize+'MB')}else{$('#upgrade_error').show();if(data.msg){$('#upgrade_error').append(data.msg+"\r\n")}else{$('#upgrade_error').append('下载失败：压缩包文件块'+(blockNo+1)+"\r\n")}}},error:function(){$('#upgrade_error').show();$('#upgrade_error').append('下载失败：压缩包文件块'+(blockNo+1)+"\r\n")},complete:function(){if($_o.existBlocks>=$_o.blocks){$_o.down_complete()}else{if($_o.nextBlockNo>0){$_o.down_zip($_o.nextBlockNo)}else{$('#op_upgrade').removeAttr('upgrading').html('请刷新界面重新下载失败的文件！')}}}})},down_complete:function(){$('#op_upgrade').html('正在更新文件...');ajaxOpen({type:'get',dataType:'json',url:ulink('upgrade/downZipComplete?'+globalOp.urlUsertoken()),success:function(data){if(data.code==1){$('#op_upgrade').html('更新成功')}else{$('#op_upgrade').html('更新失败')}}})}}
function UpgradeClass(fileList){this.fileList=fileList;this.fileNum=fileList?fileList.length:0;this.downNum=0}
UpgradeClass.prototype={constructor:UpgradeClass,down_file:function(index){var $_o=this;var file=$_o.fileList[index];ajaxOpen({type:'get',dataType:'json',url:ulink('upgrade/downFile'),data:{filename:file.file,filemd5:file.md5},success:function(data){if(data.code==1){$_o.downNum++;$('#op_upgrade').html('正在更新... '+$_o.downNum+'/'+$_o.fileNum)}else{$('#op_upgrade').html('更新失败');$('#upgrade_error').show();if(data.msg){$('#upgrade_error').append(data.msg+"\r\n")}else{$('#upgrade_error').append('更新失败：'+file.file+"\r\n")}}},error:function(){$('#op_upgrade').html('更新失败');$('#upgrade_error').show();$('#upgrade_error').append('获取失败：'+file.file+"\r\n")},complete:function(){if(index+1>=$_o.fileNum){if($_o.downNum>=$_o.fileNum){$_o.down_complete()}else{$('#op_upgrade').removeAttr('upgrading').html('请刷新界面重新下载失败的文件！')}}else{$_o.down_file(index+1)}}})},down_complete:function(){$('#op_upgrade').html('正在校验更新文件...');ajaxOpen({type:'get',dataType:'json',url:ulink('upgrade/downComplete?'+globalOp.urlUsertoken()),success:function(data){if(data.code==1){$('#op_upgrade').html('更新成功')}else{$('#op_upgrade').html('更新失败');$('#upgrade_error').show();for(var fi in data.data){$('#upgrade_error').append('文件校验失败：'+data.data[fi]+"\r\n")}}}})}}