更新程序版本1优化

This commit is contained in:
taoser 2021-03-11 15:49:28 +08:00
parent 3264e4b42c
commit 378ef5f3ab
5 changed files with 109 additions and 92 deletions

View File

@ -21,6 +21,8 @@ use think\facade\Db;
use think\exception\ValidateException;
use app\admin\controller\Uzip;
use think\facade\Cache;
use taoler\com\Api;
use think\facade\Config;
class Upgrade extends AdminController
{
@ -34,11 +36,11 @@ class Upgrade extends AdminController
public function __construct()
{
$this->sys_version_num = Db::name('system')->where('id',1)->value('sys_version_num');
$this->sys_version = Config::get('taoler.version');
$this->sys = Db::name('system')->where('id',1)->find();
}
/** 升级界面 */
public function index()
{
@ -88,63 +90,56 @@ class Upgrade extends AdminController
//升级前的版本检测
public function check($url)
{
$url = $url.'?ver='.$this->sys_version_num;
$ch =curl_init ();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch,CURLOPT_POST, 1);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpCode != '200'){
return json(['code'=>0,'msg'=>'连接服务器失败,稍后重试...']);
}
$versions = json_decode($data);
//判断状态
$url = $url.'?ver='.$this->sys_version;
//$versions = json_decode(Api::urlGet($url));
$versions = Api::urlGet($url);
//var_dump($versions);
//判断服务器状态
$version_code = $versions->code;
if($version_code == 0){
if($version_code == -1){
return json(['code'=>$version_code,'msg'=>$versions->msg]);
}
if($version_code == 1){
return json(['code'=>$versions->code,'msg'=>$versions->msg,'version'=>$versions->version,'upnum'=>$versions->up_num]);
}
if($version_code == 0){
return json(['code'=>$versions->code,'msg'=>$versions->msg]);
}
//版本比较
$version_num = $versions->version;
$up_num =$versions->up_num;
/*
$version_num = $versions->version; //最新版本
$up_num =$versions->up_num; //可更新版本数
$res = version_compare($version_num,$this->sys_version_num,'>');
if($res){
return json(['code'=>1,'msg'=>'发现新版本','version'=>$version_num,'upnum'=>$up_num]);
} else {
return json(['code'=>0,'msg'=>'暂时还没更新哦! ==8']);
}
*/
}
/**
* 自动更新
* 在线更新
*/
public function upload()
{
$data = Request::param();
$data = Request::only(['url','key']);
if(empty($data['key'])){
return json(["code"=>0,"msg"=>"请配置正确升级key"]);
}
$url = $data['url'].'?domain='.Request::domain().'&key='.$data['key'].'&ver='.$this->sys_version_num;
$url = $data['url'].'?url='.$this->sys['domain'].'&key='.$data['key'].'&ver='.$this->sys_version;
//var_dump($url);
$versions = Api::urlGet($url);
$ch =curl_init ();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch,CURLOPT_POST, 1);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpCode != '200'){
return json(['code'=>0,'msg'=>'连接服务器失败,稍后重试...']);
}
//获取远程文件
$versions = json_decode($data);
//判断状态
//判断服务器状态
$version_code = $versions->code;
if($version_code == 0){
if($version_code == -1){
return json(['code'=>$version_code,'msg'=>$versions->msg]);
}
@ -154,7 +149,7 @@ class Upgrade extends AdminController
//判断远程文件是否可用存在
$header = get_headers($file_url, true);
if(!isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'))){
return json(["code"=>0,"msg"=>"获取远程文件失败"]);
return json(["code"=>-1,"msg"=>"获取远程文件失败"]);
}
if(!is_dir($this->upload_dir)){
@ -165,7 +160,7 @@ class Upgrade extends AdminController
$cpfile = copy($file_url,$package_file);
if(!$cpfile)
{
return json(["code"=>0,"msg"=>"下载升级文件失败"]);
return json(["code"=>-1,"msg"=>"下载升级文件失败"]);
}
//记录下日志
@ -185,10 +180,12 @@ class Upgrade extends AdminController
$upres = $this->execute_update($package_file);
//更新版本
Db::name('system')->update(['sys_version_num'=>$version_num,'id'=>1]);
//$res = Config::set(['version' => $version_num,'salt' => 'taoler'], 'taoler');
//var_dump($res);
if($upres){
return json(["code"=>1,"msg"=>"升级成功"]);
return json(["code"=>0,"msg"=>"升级成功"]);
}else {
return json(["code"=>0,"msg"=>"升级失败"]);
return json(["code"=>-1,"msg"=>"升级失败"]);
}
}
@ -262,6 +259,7 @@ class Upgrade extends AdminController
//更新版本
Db::name('system')->update(['sys_version_num'=>$version_num,'id'=>1]);
//Config::set(['version' => $version_num], 'taoler');
if($upres){
return json(["code"=>1,"msg"=>"升级成功"]);
}else {

View File

@ -24,7 +24,7 @@
<div class="layui-form-item">
<label class="layui-form-label">网站域名</label>
<div class="layui-input-block">
<input type="text" name="domain" lay-verify="url" value="{$sysInfo.domain}" class="layui-input">
<input type="text" name="" lay-verify="url" value="{$sysInfo.domain}" readonly class="layui-input layui-disabled">
</div>
</div>
<div class="layui-form-item">

View File

@ -34,7 +34,7 @@
</div>
<div class="layui-form-item">
<label class="layui-form-label">在线升级</label>
<div class="layui-input-inline layui-input-company" id="ver_nums" >当前版本Taole v_{$ver_num.sys_version_num}</div>
<div class="layui-input-inline layui-input-company" id="ver_nums" >当前版本Taole v_{:config('taoler.version')}</div>
<div class="layui-upload">
{if condition="checkRuleButton('admin/Upgrade/check')"}
<button type="button" class="layui-btn layui-btn-sm" data-url="{$ver_num.upcheck_url}" id="upload-check">检查更新</button>
@ -122,12 +122,12 @@
}
});
//检测更新
//检测更新系统
$('#upload-check').on('click',function(){
var url = $(this).attr('data-url'),
loading = layer.load(2, {
shade: [0.2, '#000'],
time: 2000,
//time: 2000,
});
$.ajax({
type:"post",
@ -135,17 +135,31 @@
data:{"url":url},
daType:"json",
success:function (data){
if (data.code == 1) {
if (data.code == 0) {
//已件最新版本
layer.close(loading);
$('div#ver_nums').after('<div class="layui-input-inline layui-input-company">发现<span style="color:red">'+data.upnum+'</span>个新版本:<span style="color:red">可更新至v'+data.version+'</span></div> <button class="layui-btn layui-btn-sm" lay-submit lay-filter="update_system" data-url="{$ver_num.upgrade_url}" id="update_system">确认升级</button>');
$('button#upload-check').hide();
layer.msg(data.msg,{
icon:6,
time:2000
});
} else if (data.code == 1) {
//可升级
layer.close(loading);
$('div#ver_nums').after('<div class="layui-input-inline layui-input-company">发现<span style="color:red">'+data.upnum+'</span>个新版本:<span style="color:red">可更新至v'+data.version+'</span></div>');
//$('div#ver_nums').after('<div class="layui-input-inline layui-input-company">发现<span style="color:red">'+data.upnum+'</span>个新版本:<span style="color:red">可更新至v'+data.version+'</span></div> <button class="layui-btn layui-btn-sm" lay-submit lay-filter="update_system" data-url="{$ver_num.upgrade_url}" id="update_system">确认升级</button>');
$('button#upload-check').hide();
//更新系统
layer.confirm('可更新至v'+data.version+' 确定升级?',{icon: 3, title:'升级系统'}, function(index){
uploads(); //更新
layer.close(index); //关闭load加载层
});
} else {
//服务器错误
layer.close(loading);
layer.open({
title:'添加失败',
title:'服务错误',
content:data.msg,
icon:5,
anim:6
@ -156,22 +170,19 @@
return false;
});
//确定升级操作
$(document).on('click','#update_system',function(){
var url = $(this).attr('data-url'),
//更新
function uploads(){
var url = "{$ver_num.upgrade_url}",
key = $('input[name=key]').attr("value"),
loading = layer.load(2, {
shade: [0.2, '#000'],
});
load = layer.load(); //loading
$.ajax({
type:"post",
url:"{:url('admin/upgrade/upload')}",
data:{"url":url,"key":key},
daType:"json",
success:function (data){
if (data.code == 1) {
layer.close(loading);
if (data.code == 0) {
layer.close(load);
layer.msg(data.msg,{
icon:6,
time:2000
@ -179,7 +190,7 @@
location.reload();
});
} else {
layer.close(loading);
layer.close(load);
layer.open({
title:'升级失败',
content:data.msg,
@ -189,8 +200,16 @@
}
}
});
}
//确定升级操作
$(document).on('click','#update_system',function(){
//var url = $(this).attr('data-url'),
// key = $('input[name=key]').attr("value"),
uploads();
return false;
});
//设置key
$('#upgrade-key').on('click',function(){
var key = $('input[name=key]').val();

View File

@ -74,7 +74,7 @@ class Index extends BaseController
$pass = md5(substr_replace(md5($data['admin_pass']),$salt,0,6));
$webname = $data['webname'];
$webtitle = $data['webtitle'];
$web = Request::domain();
$web = Request::host();
//数据库配置
$dbhost = $data['DB_HOST'];
$dbuser = $data['DB_USER'];

View File

@ -21,7 +21,7 @@ class Api
if($httpCode == '200'){
return json_decode($res);
} else {
return json(['code'=>-1,'msg'=>'远程服务器失败']);
return json_decode('{"code":-1,"msg":"远程服务器失败,稍后重试"}'); //转换为对象
}
}
@ -29,17 +29,17 @@ class Api
{
$ch =curl_init ();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HEADER, 0); //启用时会将头文件的信息作为数据流输出。 参数为1表示输出信息头,为0表示不输出
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 1表示传输数据为0表示直接输出显示。
//curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, false); //启用时会将头文件的信息作为数据流输出。 参数为1表示输出信息头,为0表示不输出
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpCode == '200'){
return json_decode($data);
} else {
//return json(['code'=>-1,'msg'=>'远程服务器失败']);
return false;
//$status ='{"code":-1,"msg":"远程服务器失败"}'; //字符串
return json_decode('{"code":-1,"msg":"远程服务器失败,稍后重试"}'); //转换为对象
}
}