feat: 添加新控制器

main
yin5th 2024-11-18 19:40:29 +08:00
parent f3242717c6
commit b15058d546
15 changed files with 1376 additions and 700 deletions

160
app/controller/en/Article.php Executable file
View File

@ -0,0 +1,160 @@
<?php
namespace app\controller\en;
use app\model\{Article as MArticle, Category};
class Article extends Base
{
//列表页
public function index()
{
$categoryId = input('param.category_id/d', 0);
if($categoryId <= 0){
return $this->error('错误页面');
}
$category = Category::getById($categoryId);
if(empty($category)){
return $this->error('错误页面');
}
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
$this->data['category'] = $category;
$this->data['categoryId'] = $categoryId;
$this->templateAssign($category);
return $this->view($category['template_list'] ?? '');
}
//详情
public function detail($id=0)
{
if($id <= 0){
return $this->error('错误页面');
}
$article = MArticle::getById($id);
if(empty($article)){
return $this->error('无此文章');
}
MArticle::updateById($id, ['views' => $article['views'] + 1]);
$category = Category::getById($article['category_id']);
$keywords = $article['seo_keywords'] ? $article['seo_keywords'] : $this->system['seo_keywords'];
$description = $article['seo_description'] ? $article['seo_description'] : $this->system['seo_description'];
$this->setSeo($article['title'], $keywords, $description);
$this->data['article'] = $article;
$this->data['category'] = $category;
$this->data['categoryId'] = $category['id'];
$this->templateDetailAssign($article, $category);
return $this->view($category['template_detail'] ?? '');
}
// 列表数据绑定
private function templateAssign($category)
{
$template = strtolower($category['template_list'] ?? '');
$TopCId = Category::firstGradeById($category['id']);
if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$categoryChildren = Category::getChildrenByParentId($topCategory['id']);
$this->data['topCategory'] = $topCategory;
$this->data['categoryChildren'] = $categoryChildren;
switch($template) {
case 'products' :
$this->assignProducts($topCategory, $category, $categoryChildren);
break;
case 'news_center' :
case 'news' :
$this->assignNews($topCategory, $category, $categoryChildren);
break;
default :
$this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20);
}
}
// 详情数据绑定
private function templateDetailAssign($article, $category)
{
$template = strtolower($category['template_detail'] ?? '');
$TopCId = Category::firstGradeById($category['id']);
if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$this->data['topCategory'] = $topCategory;
switch ($template) {
case 'product':
$this->assignDetailForProduct($article, $topCategory);
break;
default :
$this->data['prev'] = MArticle::getPrevArticleByIdAndCategories($article['id'], [$article['category_id']], true, $article['sort'], true);
$this->data['next'] = MArticle::getNextArticleByIdAndCategories($article['id'], [$article['category_id']], true, $article['sort'], true);
}
}
// 产品 - 展示当前分类和所有子类产品
private function assignProducts($topCategory, $category, $categoryChildren)
{
$keyword = input('param.keyword', '');
$cateIds[] = $category['id'];
if($topCategory['id'] == $category['id']) {
$children = $categoryChildren;
} else {
$children = Category::getChildrenByParentId($category['id']);
}
foreach ($children as $child) {
$cateIds[] = $child['id'];
}
$items = MArticle::getListPageByCategories($cateIds, $category['number'] ? $category['number'] : 20, $keyword);
$items->appends(['category_id'=>$category['id']]);
$this->data['items'] = $items;
$this->data['keyword'] = $keyword;
}
// 新闻
private function assignNews($topCategory, $category, $categoryChildren)
{
if($topCategory['id'] == $category['id']) {
// 新闻中心
$cateList = [];
$newsChildrenFlip = array_flip(Category::$CIdList['news_children']);
foreach ($categoryChildren as $cate) {
$num = 3;
if($cate['id'] == Category::$CIdList['news_children']['dynamics']) {
$num = 4;
}
$cate['items'] = MArticle::getLatestByCategory($cate['id'], $num, 1);
$cateList[$newsChildrenFlip[$cate['id']]] = $cate;
}
$this->data['cateList'] = $cateList;
} else {
// 新闻子栏目
$keyword = input('param.keyword', '');
$this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20, $keyword);
$this->data['keyword'] = $keyword;
}
}
// 产品详情
private function assignDetailForProduct($article, $topCategory)
{
$cateIds[] = $article['category_id'];
$currentCateId = input('param.source', 0);
$categoryList = Category::getChildrenByParentId($topCategory['id']);
if($currentCateId == $topCategory['id']) {
foreach ($categoryList as $cate) {
$cateIds[] = $cate['id'];
}
}
$this->data['categoryChildren'] = $categoryList;
$this->data['prev'] = MArticle::getPrevArticleByIdAndCategories($article['id'], $cateIds, true, $article['sort'], true);
$this->data['next'] = MArticle::getNextArticleByIdAndCategories($article['id'], $cateIds, true, $article['sort'], true);
$this->data['currentCateId'] = $currentCateId;
}
}

52
app/controller/en/Base.php Executable file
View File

@ -0,0 +1,52 @@
<?php
namespace app\controller\en;
use app\controller\BaseController;
use app\model\System;
/**
* 控制器基础类
*/
class Base extends BaseController
{
//需要向模板传递的值
protected $data = [];
//系统配置信息
protected $system = [];
// 初始化
protected function initialize()
{
$this->middleware = ['csrf'];
$this->system = System::getSystem();
$this->data['system'] = $this->system;
$this->setCsrfToken();
}
//设置SEO信息
protected function setSeo($title, $keywords, $description)
{
$this->data['seoTitle'] = $title;
$this->data['seoKeywords'] = $keywords;
$this->data['seoDescription'] = $description;
}
//设置默认SEO信息
protected function setDefaultSeo()
{
$this->data['seoTitle'] = $this->system['seo_title'];
$this->data['seoKeywords'] = $this->system['seo_keywords'];
$this->data['seoDescription'] = $this->system['seo_description'];
}
//模板
protected function view($template = '')
{
return view($template)->assign($this->data);
}
protected function setCsrfToken()
{
$this->data['_token'] = session('_token') ?? '';
}
}

View File

@ -0,0 +1,193 @@
<?php
declare (strict_types = 1);
namespace app\controller\en;
use think\{App, Validate};
use think\exception\ValidateException;
/**
* 控制器基础类
*/
abstract class 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()
{
}
/**
* 验证数据
* @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);
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @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 = [])
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
return $this->redirect(url('error/jump',$result));
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @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)
{
if (is_null($url)) {
$referer = $_SERVER['HTTP_REFERER'] ?? null;
if (empty($referer)) {
$url = $this->request->isAjax() ? '' : '/';
} else {
$url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
}
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
return $this->redirect(url('error/jump', $result));
}
/**
* 返回封装后的API数据到客户端
* 以json格式抛出异常
* @access protected
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return void
*/
protected function json($code = 0, $msg = 'ok', $data= [])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data
];
return json($result);
}
/**
* URL重定向
* @access protected
* @param string $url 跳转的URL表达式
* @param array|integer $params 其它URL参数
* @param integer $code http code
* @param array $with 隐式传参
* @return void
*/
protected function redirect($url)
{
if(!is_string($url)){
$url = $url->__toString();
}
return redirect($url);
}
}

48
app/controller/en/Error.php Executable file
View File

@ -0,0 +1,48 @@
<?php
namespace app\controller\en;
class Error extends BaseController
{
public function __call($method, $args)
{
if(request()->isAjax()) {
return $this->json(404, 'error request!');
} else {
$referer = $_SERVER['HTTP_REFERER'] ?? null;
if (empty($referer)) {
$url = '/';
} else {
$domain = $this->request->domain();
$urlInfo = parse_url($referer);
$scheme = $urlInfo['scheme'] ?? '';
$requestSrc = '';
if (!empty($scheme)) {
$requestSrc = $scheme.'://'.($urlInfo['host'] ?? '');
}
if($domain != $requestSrc) {
$url = '/';
} else {
$url = 'javascript:history.back(-1);';
}
}
$result = [
'code' => 404,
'msg' => 'Invalid request! No related resources found.',
'data' => [],
'url' => $url,
'wait' => 5,
];
return view('error/400')->assign($result);
}
}
public function jump()
{
$param = request()->param();
return view()->assign($param);
}
}

36
app/controller/en/Index.php Executable file
View File

@ -0,0 +1,36 @@
<?php
namespace app\controller\en;
use app\model\{Category, Block, Article, Slide};
class Index extends Base
{
public function index()
{
$category = Category::getIndex();
$categoryId = $category['id'] ?? 0;
$this->data['categoryId'] = $categoryId;
$this->setSeo($this->system['seo_title'], $this->system['seo_keywords'], $this->system['seo_description']);
$blocks = Block::getByCategoryId($categoryId);
$blocks = Block::analysisBlock($blocks);
$this->data['blocks'] = $blocks;
// 轮播图
$this->data['slides'] = Slide::getList();
// 营销网络栏目ID
$this->data['marketingCId'] = Category::$CIdList['marketing'];
// 关联产品分类
$productsCenterCId = Category::$CIdList['products'];
$this->data['productsCenter'] = Category::getById($productsCenterCId);
$this->data['products'] = Category::getChildrenByParentId($productsCenterCId);
// 关联新闻
$this->data['newsCenter'] = Category::getById(Category::$CIdList['news']);
$newsCIdList = [Category::$CIdList['news_children']['enterprise'], Category::$CIdList['news_children']['industry']];
$newsList = Category::getListByIds($newsCIdList);
foreach ($newsList as &$cate) {
$cate['items'] = Article::getLatestByCategory($cate['id'], 4, 1);
}
unset($cate);
$this->data['newsList'] = $newsList;
return $this->view();
}
}

42
app/controller/en/Message.php Executable file
View File

@ -0,0 +1,42 @@
<?php
namespace app\controller\en;
use app\model\Message as MMessage;
use app\validate\Message as VMessage;
use think\exception\ValidateException;
/**
* 留言
* Class Message
* @package app\controller
*/
class Message extends Base
{
// 新增留言
public function add()
{
if(request()->isPost()) {
$msgData = [
'company_name' => trim(input('post.company_name', '')),
'name' => trim(input('post.name', '')),
'phone' => trim(input('post.phone', '')),
'email' => trim(input('post.email', '')),
'content' => trim(input('post.content', '')),
];
// 安全过滤
$msgData = array_map('strip_tags', $msgData);
try {
validate(VMessage::class)->check($msgData);
$msgData['ip'] = request()->ip();
$msgData['create_time'] = time();
MMessage::create($msgData);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
} else {
return $this->json(1, '非法请求');
}
}
}

145
app/controller/en/Page.php Executable file
View File

@ -0,0 +1,145 @@
<?php
namespace app\controller\en;
use app\model\{Achievement, AchievementInfo, Category, Block, Article, History, Model};
class Page extends Base
{
// 默认单页页面
public function index($categoryId)
{
$category = Category::getById($categoryId);
if ($category) {
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
} else {
return $this->error('页面错误');
}
$this->data['categoryId'] = $categoryId;
$this->data['category'] = $category;
$this->data['blocks'] = Block::getByCategoryId($categoryId);
$this->templateDetailAssign($category);
return $this->view($category['template_detail']);
}
private function templateDetailAssign($category)
{
$template = $category['template_detail'] ?? '';
$TopCId = Category::firstGradeById($category['id']);
if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$childCategory = Category::getChildrenByParentId($topCategory['id']);
$this->data['topCategory'] = $topCategory;
$this->data['childCategory'] = $childCategory;
switch ($template) {
case 'about' :
$this->assignAbout($childCategory);
break;
case 'service' :
$this->assignService($childCategory);
break;
case 'marketing' :
$this->assignMarketing($childCategory);
break;
case 'contact' :
$this->assignContact($childCategory);
break;
default :
$this->data['blocks'] = Block::getByCategoryId($category['id']);
}
}
// 获取单页栏目IDs
private function getBlockCateIds($categoryItems)
{
$blockCateIds = [];
foreach ($categoryItems as $cate) {
if($cate['model_id'] == Model::PAGE) {
$blockCateIds[] = $cate['id'];
}
}
return $blockCateIds;
}
// 走进超宇
private function assignAbout($childCategory)
{
$honorTopCId = Category::$CIdList['honors_manage'] ?? 0;
$historyCId = Category::$CIdList['history_manage'] ?? 0;
$historyCate = Category::getById($historyCId);
$honors = [];
$blocks = [];
$blockCateIds = $this->getBlockCateIds($childCategory);
if($honorTopCId) {
$honors = Category::getChildrenByParentId($honorTopCId);
foreach ($honors as &$honor) {
$honor['items'] = Article::getListByCategoryIds([$honor['id']], $honor['number'] ? $honor['number'] : 20, '', [], 1);
}
unset($honor);
}
$blockList = Block::getByCategoryIds($blockCateIds);
$aboutChildrenFlip = array_flip(Category::$CIdList['about_children']);
foreach ($childCategory as $cate) {
$blocks[$aboutChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
}
$this->data['blocks'] = $blocks;
$this->data['honors'] = $honors;
$this->data['historyList'] = array_reverse(History::getByCategoryId($historyCId, true, $historyCate['number'] ?? -1));
}
// 品质与服务
private function assignService($childCategory)
{
$blocks = [];
$blockCateIds = $this->getBlockCateIds($childCategory);
$blockList = Block::getByCategoryIds($blockCateIds);
$serviceChildrenFlip = array_flip(Category::$CIdList['service_children']);
foreach ($childCategory as $cate) {
$blocks[$serviceChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
}
$this->data['blocks'] = $blocks;
}
// 营销网络
private function assignMarketing($childCategory)
{
$blocks = [];
$blockCateIds = $this->getBlockCateIds($childCategory);
$blockList = Block::getByCategoryIds($blockCateIds);
$marketingChildrenFlip = array_flip(Category::$CIdList['marketing_children']);
foreach ($childCategory as $cate) {
$blocks[$marketingChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
}
$achievementCate = Category::getById(Category::$CIdList['achievement_manage']);
$achievementList = [];
if ($achievementCate) {
$achievementList = Achievement::getListByCategoryId($achievementCate['id'], $achievementCate['number'] ? $achievementCate['number'] : 10, true);
}
$this->data['blocks'] = $blocks;
$this->data['achievementList'] = $achievementList;
}
// 联系我们
private function assignContact($childCategory)
{
$blocks = [];
$blockCateIds = $this->getBlockCateIds($childCategory);
$blockList = Block::getByCategoryIds($blockCateIds);
$contactChildrenFlip = array_flip(Category::$CIdList['contact_children']);
foreach ($childCategory as $cate) {
$blocks[$contactChildrenFlip[$cate['id']]] = $blockList[$cate['id']] ?? [];
}
$jobsCate = Category::getById(Category::$CIdList['jobs_manage']);
$jobList = Article::getLatestByCategory($jobsCate['id'], $jobsCate['number'] ? $jobsCate['number'] : 10, 1);
$this->data['blocks'] = $blocks;
$this->data['jobList'] = $jobList;
}
}