优化api监测,添加统计代码

This commit is contained in:
taoser 2021-07-16 17:42:07 +08:00
parent c7735fb8c5
commit 63f3bd01b5
12 changed files with 157 additions and 378 deletions

View File

@ -7,8 +7,8 @@
* 后台http://adm.aieok.com
* 账号test
* 密码test123
* 版本TaoLer 1.7.16
* 日期2021.7.15
* 版本TaoLer 1.7.17
* 日期2021.7.16
#### 项目地址

View File

@ -91,4 +91,100 @@ abstract class BaseController
return $v->failException(true)->check($data);
}
/**
* 操作错误跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作成功跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_success_tmpl')]);
throw new HttpResponseException($response);
}
}

View File

@ -59,34 +59,16 @@ class Index extends AdminController
public function home()
{
//版本检测
$url = $this->sys['upcheck_url'].'?pn='.$this->pn.'&ver='.$this->sys_version;
$versions = Api::urlGet($url);
if($versions->code == 1){
if($versions->up_num > 0){
$versions = "当前有{$versions->up_num}个版本需更新,当前可更新至{$versions->version}";
}
} else {
$versions ='当前无可更新版本';
}
$verCheck = Api::urlPost($this->sys['upcheck_url'],['pn'=>$this->pn,'ver'=>$this->sys_version]);
$versions = $verCheck->code ? "{$verCheck->up_num}个版本需更新,当前可更新至{$verCheck->version}" : $verCheck->msg;
//评论、帖子状态
$comm = Db::name('comment')->field('id')->where(['delete_time'=>0,'status'=>0])->select();
$forum = Db::name('article')->field('id')->where(['delete_time'=>0,'status'=>0])->select();
$comms = count($comm);
$forums = count($forum);
//运行时间
$now = time();
$count = $now-$this->sys['create_time'];
$days = floor($count/86400);
$hos = floor(($count%86400)/3600);
$mins = floor(($count%3600)/60);
$years = floor($days/365);
if($years >= 1){
$days = floor($days%365);
}
$runTime = $years ? "{$years}{$days}{$hos}{$mins}" : "{$days}{$hos}{$mins}";
$cpy = ($this->getCyl() > 1) ? Lang::get('Authorized') : Lang::get('Free version');
View::assign(['runTime'=>$runTime,'versions'=>$versions,'comms'=>$comms,'forums'=>$forums,'cpy'=>$cpy]);
View::assign(['versions'=>$versions,'comms'=>$comms,'forums'=>$forums]);
return View::fetch();
}

View File

@ -17,16 +17,14 @@ class Set extends AdminController
protected function initialize()
{
parent::initialize();
$this->sysInfo = $this->getSystem();
$this->syscy = $this->getCyl();
}
//网站设置显示
public function index()
{
$mailserver = MailServer::find(1);
$template = Files::getDirName('../view');
View::assign(['sysInfo'=>$this->sysInfo,'syscy'=>$this->syscy,'mailserver'=>$mailserver,'template'=>$template]);
View::assign(['sysInfo'=>$this->sysInfo,'mailserver'=>$mailserver,'template'=>$template]);
return View::fetch('set/system/website');
}
@ -34,9 +32,9 @@ class Set extends AdminController
public function website()
{
if(Request::isPost()){
$data = Request::only(['webname','domain','template','cache','upsize','uptype','blackname','webtitle','keywords','descript','icp','copyright']);
$data = Request::only(['webname','domain','template','cache','upsize','uptype','blackname','webtitle','keywords','descript','icp','showlist','copyright']);
$system = new System();
$result = $system->sets($data,$this->syscy);
$result = $system->sets($data,$this->sysInfo['clevel']);
if($result == 1){
return json(['code'=>0,'msg'=>'更新成功']);
} else {
@ -69,40 +67,6 @@ class Set extends AdminController
}
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
//上传logo
public function upload()
{

View File

@ -39,7 +39,7 @@ class Upgrade extends AdminController
parent::initialize();
$this->sys_version = Config::get('taoler.version');
$this->pn = Config::get('taoler.appname');
$this->sys = Db::name('system')->where('id',1)->find();
$this->sys = $this->getSystem();
}
@ -63,7 +63,7 @@ class Upgrade extends AdminController
if(empty($data['key'])){
return json(['code'=>0,'msg'=>'请填写正确的key']);
}
$res = Db::name('system')->update(['key'=>$data['key'],'id'=>1]);
$res = Db::name('system')->cache('system')->update(['key'=>$data['key'],'id'=>1]);
if($res){
$res = ['code'=>0,'msg'=>'保存成功'];
} else {
@ -97,9 +97,11 @@ class Upgrade extends AdminController
//升级前的版本检测
public function check()
{
$url = $this->sys['upcheck_url'].'?pn='.$this->pn.'&ver='.$this->sys_version;
$versions = Api::urlGet($url);
$cy = Api::urlPost($this->sys['base_url'],['u'=>$this->sys['domain']]);
if($cy->code == 0 && $cy->level !== $this->sys['clevel']){
Db::name('system')->cache('system')->update(['clevel'=>$cy->level,'id'=>1]);
}
$versions = Api::urlPost($this->sys['upcheck_url'],['pn'=>$this->pn,'ver'=>$this->sys_version]);
//判断服务器状态
$version_code = $versions->code;
if($version_code == -1){
@ -138,8 +140,7 @@ class Upgrade extends AdminController
*/
public function upload()
{
$url = $this->sys['upgrade_url'].'?url='.$this->sys['domain'].'&key='.$this->sys['key'].'&pn='.$this->pn.'&ver='.$this->sys_version;
$versions = Api::urlGet($url);
$versions = Api::urlPost($this->sys['upgrade_url'],['url'=>$this->sys['domain'],'key'=>$this->sys['key'],'pn'=>$this->pn,'ver'=>$this->sys_version]);
Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'check','progress'=>'0%','msg'=>'===>升级检测开始===>']);
//判断服务器状态
$version_code = $versions->code;

View File

@ -263,7 +263,7 @@
<td>当前授权</td>
<td>
<script type="text/html" template>
{$cpy}
{$syscy}
</script>
</td>
</tr>

View File

@ -102,13 +102,19 @@
<input type="text" name="icp" value="{$sysInfo.icp}" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">访问统计</label>
<div class="layui-input-block">
<input type="text" name="showlist" value="{$sysInfo.showlist}" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">版权信息</label>
<div class="layui-input-inline" style="width: 400px;">
<input type="text" name="copyright" value="{$sysInfo.copyright}" class="layui-input">
</div>
<div class="layui-input-inline layui-input-company">提示:</div>
<div class="layui-form-mid layui-word-aux">经授权用户,可不限制功能使用,但严禁私自改写版权脚本。一旦发现,永远禁止升级并追诉相关责任。</div>
<div class="layui-form-mid layui-word-aux">授权版本,不限制功能,但严禁私自改写此处版权脚本,一旦发现,永久关闭升级服务!!</div>
<div class="layui-form-mid layui-word-aux"></div>
</div>
<div class="layui-form-item">

View File

@ -5,62 +5,19 @@ namespace app\common\controller;
use think\Controller;
use think\App;
use think\Response;
use think\exception\ValidateException;
use think\Validate;
use think\exception\HttpResponseException;
use think\facade\Session;
use think\facade\Cache;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use taoser\think\Auth;
use taoler\com\Files;
use taoler\com\Api;
use think\facade\Lang;
/**
* 控制器基础类
*/
abstract class AdminController
class AdminController extends \app\BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
@ -71,64 +28,6 @@ abstract class AdminController
$this->getIndexUrl();
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
//获取层级
protected function getCyl()
{
/*
$cylevel = Cache::get('cylevel');
if(!$cylevel){
$sys = $this->getSystem();
$url = $sys['base_url'].'?u='.$sys['domain'];
$cy = Api::urlGet($url);
halt($cy);
if($cy && $cy->code == 0){
$cylevel = $cy->level;
} else {
$cylevel = 0;
}
Cache::set('cylevel',$cylevel,3600);
}
*/
return 0;
}
/**
* 获取侧边栏菜单
*/
@ -170,7 +69,8 @@ abstract class AdminController
}
//清除缓存Cache
public function clearData(){
public function clearData()
{
$dir = app()->getRootPath().'runtime/admin/temp';
$cache = app()->getRootPath().'runtime/cache';
if(is_dir($cache)){
@ -201,11 +101,29 @@ abstract class AdminController
//得到当前系统安装前台域名
protected function getIndexUrl()
{
$sysUrl = $this->getSystem();
$domain = $this->getHttpUrl($sysUrl['domain']);
$syscy = $this->getCyl();
View::assign(['domain'=>$domain,'insurl'=>$sysUrl['domain'],'syscy'=>$syscy]);
$sys = $this->getSystem();
$domain = $this->getHttpUrl($sys['domain']);
$syscy = $sys['clevel'] ? Lang::get('Authorized') : Lang::get('Free version');
$runTime = $this->getRunTime();
View::assign(['domain'=>$domain,'insurl'=>$sys['domain'],'syscy'=>$syscy,'runTime'=>$runTime]);
}
protected function getRunTime()
{
//运行时间
$now = time();
$sys = $this->getSystem();
$count = $now-$sys['create_time'];
$days = floor($count/86400);
$hos = floor(($count%86400)/3600);
$mins = floor(($count%3600)/60);
$years = floor($days/365);
if($years >= 1){
$days = floor($days%365);
}
$runTime = $years ? "{$years}{$days}{$hos}{$mins}" : "{$days}{$hos}{$mins}";
return $runTime;
}
}

View File

@ -4,207 +4,29 @@ declare (strict_types = 1);
namespace app\common\controller;
use think\App;
use think\Response;
use think\facade\View;
use think\facade\Db;
use think\exception\ValidateException;
use think\exception\HttpResponseException;
use think\facade\Session;
use think\facade\Cache;
use app\BaseController as BaseCtrl;
/**
* 控制器基础类
*/
abstract class BaseController
class BaseController extends BaseCtrl
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 用户id
* @var int
*/
protected $uid;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
$this->uid = Session::get('user_id');
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
$this->uid = Session::get('user_id');
//系统配置
$this->showSystem();
//显示分类导航
$this->showNav();
$this->showUser();
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
/**
* 操作错误跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作成功跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_success_tmpl')]);
throw new HttpResponseException($response);
}
//判断是否已登录?
protected function isLogged()
{

View File

@ -124,9 +124,6 @@ CREATE TABLE `tao_auth_group_access` (
KEY `uid_group_id` (`uid`,`group_id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tao_auth_group_access
-- ----------------------------
-- ----------------------------
-- Table structure for tao_auth_group_copy
@ -307,10 +304,6 @@ CREATE TABLE `tao_collection` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='文章收藏表';
-- ----------------------------
-- Records of tao_collection
-- ----------------------------
-- ----------------------------
-- Table structure for tao_comment
-- ----------------------------
@ -351,11 +344,6 @@ CREATE TABLE `tao_cunsult` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tao_cunsult
-- ----------------------------
INSERT INTO `tao_cunsult` VALUES ('2', '2', '', '', '3', '1612162069', '0');
-- ----------------------------
-- Table structure for tao_friend_link
-- ----------------------------
@ -487,9 +475,11 @@ CREATE TABLE `tao_system` (
`is_comment` enum('0','1') NOT NULL DEFAULT '1' COMMENT '是否开启评论1开启0关闭',
`is_reg` enum('0','1') NOT NULL DEFAULT '1' COMMENT '是否开放注册1开启0禁止',
`icp` varchar(50) NOT NULL DEFAULT '' COMMENT '备案',
`showlist` varchar(255) NOT NULL COMMENT '统计代码',
`blackname` varchar(255) NOT NULL COMMENT '注册黑名单',
`sys_version_num` varchar(5) NOT NULL COMMENT '系统版本',
`key` varchar(60) DEFAULT NULL COMMENT 'key',
`clevel` tinyint(1) NOT NULL DEFAULT '0',
`api_url` varchar(80) NOT NULL COMMENT 'api',
`base_url` varchar(50) NOT NULL DEFAULT '',
`upcheck_url` varchar(255) NOT NULL COMMENT '版本检测',
@ -502,7 +492,7 @@ CREATE TABLE `tao_system` (
-- ----------------------------
-- Records of tao_system
-- ----------------------------
INSERT INTO `tao_system` VALUES ('1', 'TaoLer社区演示站', '轻论坛系统', 'http://www.xxx.com', 'taoler', '/storage/logo/logo.png', '10', '2048', 'png|gif|jpg|jpeg|zip|rarr', '<a href="https://www.aieok.com" target="_blank">TaoLer</a>', 'TaoLer,轻社区系统,bbs,论坛,Thinkphp6,layui,fly模板,', '这是一个Taoler轻社区论坛系统', '1', '1', '1', '0.0.0.0', '管理员|admin|审核员|超级|垃圾', '1.6.3', '', 'http://api.aieok.com', 'http://api.aieok.com/v1/cy', 'http://api.aieok.com/v1/upload/check', 'http://api.aieok.com/v1/upload/api', '1581221008', '1577419197');
INSERT INTO `tao_system` VALUES ('1', 'TaoLer社区演示站', '轻论坛系统', 'http://www.xxx.com', 'taoler', '/storage/logo/logo.png', '10', '2048', 'image:png|gif|jpg|jpeg,application:zip|rar,video:mp4,audio:mp3|m4a|mp4', '<a href="https://www.aieok.com" target="_blank">TaoLer</a>', 'TaoLer,轻社区系统,bbs,论坛,Thinkphp6,layui,fly模板,', '这是一个Taoler轻社区论坛系统', '1', '1', '1', '0.0.0.0-1', '', '管理员|admin|审核员|超级|垃圾', '1.6.3', '','0', 'http://api.aieok.com', 'http://api.aieok.com/v1/cy', 'http://api.aieok.com/v1/upload/check', 'http://api.aieok.com/v1/upload/api', '1581221008', '1577419197');
-- ----------------------------
-- Table structure for tao_user

View File

@ -7,7 +7,7 @@ return [
//应用名,此项不可更改
'appname' => 'TaoLer',
//版本配置
'version' => '1.7.16',
'version' => '1.7.17',
//加盐
'salt' => 'taoler',
//数据库备份目录

View File

@ -1,5 +1,5 @@
<div class="fly-footer html5plus-hide">
<p> Copyright &copy; {:date('Y')}{$sysInfo.copyright|raw}v{:config('taoler.version')}</p>
<p> Copyright &copy; {:date('Y')}{$sysInfo.copyright|raw}v{:config('taoler.version')} {$sysInfo.showlist}</p>
<p>
{volist name="footlinks" id="vo"}
<a href="{$vo.slid_href}" target="_blank">{$vo.slid_name}</a>