优化后端

This commit is contained in:
2025-05-16 13:04:17 +08:00
parent 4f8f621c2c
commit 9df71d0faf
19 changed files with 1835 additions and 694 deletions
+14 -1
View File
@@ -16,12 +16,15 @@ class Article extends Base
if (Request::isPost()) {
$category = input('post.category');
$page = input('post.page', 1);
$limit = input('post.limit', 20);
$limit = input('post.limit', 10);
$title = input('post.title');
$author = input('post.author');
$query = Db::table('yz_article')
->where('delete_time', null)
->where('status', '<>', 3);
// 分类筛选
if (!empty($category)) {
// 先获取分类ID
$cateInfo = Db::table('yz_article_category')
@@ -34,6 +37,16 @@ class Article extends Base
$query = $query->where('cate', $cateInfo['id']);
}
}
// 标题搜索
if (!empty($title)) {
$query = $query->where('title', 'like', '%'.$title.'%');
}
// 作者搜索
if (!empty($author)) {
$query = $query->where('author', 'like', '%'.$author.'%');
}
// 获取总记录数
$count = $query->count();
+94 -64
View File
@@ -13,25 +13,30 @@ use app\admin\model\YzAdminConfig;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\Route;
use think\facade\Route;
use think\App;
class Base{
class Base
{
protected $app;
protected $request;
public $adminId = null;
public $config = [];
public $aUser = [];
public function __construct(){
public function __construct()
{
date_default_timezone_set('PRC');
# 获取配置
$YzAdminConfig = new YzAdminConfig();
$this->config = $YzAdminConfig->getAll();
# 获取账户,账户判断
$this->adminId = Cookie::get('admin_id');
if(empty($this->adminId)){
header('Location:'.$this->config['admin_route'].'Login/index');
if (empty($this->adminId)) {
header('Location:' . $this->config['admin_route'] . 'Login/index');
exit;
}
$this->aUser = Db::table('yz_admin_user')->where('uid',$this->adminId)->find();
$this->aUser = Db::table('yz_admin_user')->where('uid', $this->adminId)->find();
if (empty($this->aUser)) {
Cookie::delete('admin_id');
$this->error('管理员账户不存在');
@@ -41,14 +46,14 @@ class Base{
$this->error('管理员已被禁用');
}
# 获取用户组权限
$group = Db::table('yz_admin_user_group')->where(['group_id'=>$this->aUser['group_id']])->find();
if(empty($group)){
$group = Db::table('yz_admin_user_group')->where(['group_id' => $this->aUser['group_id']])->find();
if (empty($group)) {
$this->error('对不起,您没有权限');
}
# 获取当前链接,查询是否有权限
$controller = request()->controller();
$action = request()->action();
$key = $controller.'/'.$action;
$key = $controller . '/' . $action;
View::assign([
'aUser' => $this->aUser,
'config' => $this->config
@@ -57,78 +62,103 @@ class Base{
/**
* 返回json对象
*/
protected function returnCode($code,$data=[],$count=10){
protected function returnCode($code, $data = [], $count = 10)
{
header('Content-type:application/json');
if($code == 0){
if ($code == 0) {
$arr = array(
'code'=>$code,
'msg'=>'操作成功',
'count'=> $count,
'code' => $code,
'msg' => '操作成功',
'count' => $count,
'data' => $data
);
}else if($code >= 1 && $code <= 100){
} else if ($code >= 1 && $code <= 100) {
$arr = array(
'code' => $code,
'msg' => $data
'code' => $code,
'msg' => $data
);
}else{
} else {
$appapi = new AppApi();
$arr = array(
'code'=>$code,
'msg'=>$appapi::errorTip($code)
'code' => $code,
'msg' => $appapi::errorTip($code)
);
}
echo json_encode($arr);
if($code != 0){
if ($code != 0) {
exit;
}
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function success($msg = '')
{
$result = [
'code' => 1,
'msg' => $msg
];
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function success($msg = '')
{
$result = [
'code' => 1,
'msg' => $msg
];
$type = $this->getResponseType();
if ($type == 'html'){
$response = view(Config::get('app.dispatch_success_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
$type = $this->getResponseType();
if ($type == 'html') {
$response = view(Config::get('app.dispatch_success_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function error($msg = '')
{
$result = [
'code' => 0,
'msg' => $msg
];
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function error($msg = '')
{
$result = [
'code' => 0,
'msg' => $msg
];
$response = view(Config::get('app.dispatch_error_tmpl'), $result);
throw new HttpResponseException($response);
}
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return Request::isJson() || Request::isAjax() ? 'json' : 'html';
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return Request::isJson() || Request::isAjax() ? 'json' : 'html';
}
public function initialize(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 检查是否是直接访问具体页面
$controller = $this->request->controller();
$action = $this->request->action();
// 如果不是访问index控制器,且不是通过iframe加载,且不是ajax请求
if (
$controller != 'Index' &&
!$this->request->isAjax() &&
!$this->request->header('X-Requested-With') &&
!$this->request->param('iframe')
) { // 添加iframe参数检查
// 重定向到index页面,并带上当前页面参数
$currentUrl = $controller . '/' . $action;
redirect(url('index/index', ['page' => $currentUrl]))->send();
exit;
}
}
}