更新代码

This commit is contained in:
2025-05-19 17:34:40 +08:00
parent 1587524031
commit 7bd072add9
32 changed files with 1465 additions and 139 deletions
@@ -3,12 +3,14 @@
* 文章控制器
*/
namespace app\index\controller;
use app\index\controller\Base;
use app\index\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\index\model\Articles\Articles;
use app\index\model\Articles\ArticlesCategory;
class Article extends Base
class ArticlesController extends BaseController
{
// 文章列表页
public function list()
@@ -27,8 +29,7 @@ class Article extends Base
}
// 获取文章列表
$articles = Db::table('yz_article')
->where($where)
$articles = Articles::where($where)
->order('id DESC')
->paginate([
'list_rows' => 10,
@@ -38,16 +39,14 @@ class Article extends Base
// 获取分类信息
$category = null;
if ($cateId > 0) {
$category = Db::table('yz_article_category')
->where('id', $cateId)
$category = ArticlesCategory::where('id', $cateId)
->where('delete_time', null)
->where('status', 3)
->find();
}
// 获取所有分类
$categories = Db::table('yz_article_category')
->where('delete_time', null)
$categories = ArticlesCategory::where('delete_time', null)
->where('status', 3)
->select()
->toArray();
@@ -66,36 +65,32 @@ class Article extends Base
public function detail()
{
$id = Request::param('id/d', 0);
$article = Db::table('yz_article')->where('id', $id)->find();
$article = Articles::where('id', $id)->find();
if (!$article) {
return json(['code' => 0, 'msg' => '文章不存在或已被删除']);
}
// 获取分类名称
$cateName = Db::table('yz_article_category')
->where('id', $article['cate'])
$cateName = ArticlesCategory::where('id', $article['cate'])
->value('name');
// 获取上一篇和下一篇文章
$prevArticle = Db::table('yz_article')
->where('id', '<', $id)
$prevArticle = Articles::where('id', '<', $id)
->where('delete_time', null)
->where('status', '<>', 3)
->order('id DESC')
->find();
$nextArticle = Db::table('yz_article')
->where('id', '>', $id)
$nextArticle = Articles::where('id', '>', $id)
->where('delete_time', null)
->where('status', '<>', 3)
->order('id ASC')
->find();
// 获取相关文章(同分类的其他文章)
$relatedArticles = Db::table('yz_article')
->alias('a')
->join('yz_article_category c', 'a.cate = c.id')
$relatedArticles = Articles::alias('a')
->join('articles_category c', 'a.cate = c.id')
->where('a.cate', $article['cate'])
->where('a.id', '<>', $id)
->where('a.delete_time', null)
@@ -148,8 +143,7 @@ class Article extends Base
$id = Request::param('id/d', 0);
// 更新点赞数
$result = Db::table('yz_article')
->where('id', $id)
$result = Articles::where('id', $id)
->where('delete_time', null)
->inc('likes', 1)
->update();
@@ -177,8 +171,7 @@ class Article extends Base
}
// 检查文章是否存在
$article = Db::table('yz_article')
->where('id', $articleId)
$article = Articles::where('id', $articleId)
->where('delete_time', null)
->where('status', 3)
->find();
@@ -228,8 +221,7 @@ class Article extends Base
// 获取总访问量
$totalViews = Db::table('yz_article')
->where('id', $id)
$totalViews = Articles::where('id', $id)
->value('views');
return json([
@@ -256,16 +248,16 @@ class Article extends Base
try {
// 更新访问次数
$article = Db::table('yz_article')->where('id', $id)->find();
$article = Articles::where('id', $id)->find();
if (!$article) {
return json(['code' => 0, 'msg' => '文章不存在']);
}
// 更新访问次数
Db::table('yz_article')->where('id', $id)->inc('views')->update();
Articles::where('id', $id)->inc('views')->update();
// 获取更新后的访问次数
$newViews = Db::table('yz_article')->where('id', $id)->value('views');
$newViews = Articles::where('id', $id)->value('views');
return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
} catch (\Exception $e) {
+139
View File
@@ -0,0 +1,139 @@
<?php
declare (strict_types = 1);
namespace app\index\controller;
use think\App;
use think\facade\View;
use think\facade\Request;
use think\facade\Config;
/**
* 前台控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
/**
* 初始化
*/
protected function initialize()
{
// 设置通用变量
View::assign([
'site_name' => '网站名称',
'site_description' => '网站描述',
'site_keywords' => '网站关键词',
'config' => [
'admin_name' => Config::get('site.name', '云泽科技'),
'admin_phone' => Config::get('site.phone', '400-123-4567'),
'admin_email' => Config::get('site.email', 'admin@example.com'),
'admin_wechat' => Config::get('site.wechat_qrcode', '/static/images/wechat_qrcode.jpg'),
'logo' => Config::get('site.logo', '/static/images/logo.png'),
'logo1' => Config::get('site.logo1', '/static/images/logo1.png'),
'admin_route' => Config::get('site.admin_route', '/admin/')
]
]);
}
/**
* 获取控制器名称(移除Controller后缀)
* @return string
*/
public function getControllerName()
{
$className = get_class($this);
$className = substr($className, strrpos($className, '\\') + 1);
return str_replace('Controller', '', $className);
}
/**
* 渲染模板输出
* @param string $template 模板文件
* @param array $vars 模板变量
* @return string
*/
protected function fetch($template = '', $vars = [])
{
return View::fetch($template, $vars);
}
/**
* 操作成功跳转
* @param string $msg 提示信息
* @param string $url 跳转地址
* @param mixed $data 返回数据
* @param integer $wait 跳转等待时间
* @return void
*/
protected function success($msg = '', $url = null, $data = '', $wait = 3)
{
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url
]);
}
return View::fetch('common/success', [
'msg' => $msg,
'url' => $url,
'data' => $data,
'wait' => $wait
]);
}
/**
* 操作失败跳转
* @param string $msg 提示信息
* @param string $url 跳转地址
* @param mixed $data 返回数据
* @param integer $wait 跳转等待时间
* @return void
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3)
{
if (Request::isAjax()) {
return json([
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url
]);
}
return View::fetch('common/error', [
'msg' => $msg,
'url' => $url,
'data' => $data,
'wait' => $wait
]);
}
}
@@ -3,19 +3,19 @@
* 后台管理系统-首页
*/
namespace app\index\controller;
use app\index\controller\Base;
use app\index\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Env;
use think\facade\Config;
use app\index\model\Banner;
use app\index\model\ResourcesCategory;
use app\index\model\ArticlesCategory;
use app\index\model\Resources;
use app\index\model\Articles;
use app\index\model\Resources\ResourcesCategory;
use app\index\model\Articles\ArticlesCategory;
use app\index\model\Resources\Resources;
use app\index\model\Articles\Articles;
class Index extends Base
class IndexController extends BaseController
{
/**
* 首页
@@ -3,15 +3,15 @@
* 程序下载控制器
*/
namespace app\index\controller;
use app\index\controller\Base;
use app\index\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\index\model\Resources;
use app\index\model\ResourcesCategory;
use app\index\model\Resources\Resources;
use app\index\model\Resources\ResourcesCategory;
use app\index\model\Attachments;
class Program extends Base
class ProgramController extends BaseController
{
// 程序列表页
public function list()
@@ -1,5 +1,5 @@
<?php
namespace app\index\model;
namespace app\index\model\Articles;
use think\Model;
@@ -1,5 +1,5 @@
<?php
namespace app\index\model;
namespace app\index\model\Articles;
use think\Model;
@@ -1,5 +1,5 @@
<?php
namespace app\index\model;
namespace app\index\model\Resources;
use think\Model;
@@ -1,5 +1,5 @@
<?php
namespace app\index\model;
namespace app\index\model\Resources;
use think\Model;