yunzer/app/index/controller/IndexController.php
2025-06-07 09:15:14 +08:00

428 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
/**
* 后台管理系统-首页
*/
namespace app\index\controller;
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\Resources\ResourcesCategory;
use app\index\model\Articles\ArticlesCategory;
use app\index\model\Resources\Resources;
use app\index\model\Articles\Articles;
use app\index\model\MailConfig;
use \think\facade\Filesystem;
use app\index\model\Attachments;
class IndexController extends BaseController
{
/**
* 首页
*/
public function index()
{
// 获取banner列表
$bannerList = Banner::where('delete_time', null)
->order('sort DESC, id DESC')
->select()
->toArray();
View::assign('bannerList', $bannerList);
return View::fetch();
}
/**
* 获取站点资讯列表
*/
public function siteNewslist()
{
// 获取站点资讯分类顶级分类id为1的子分类
$categories = ArticlesCategory::where('cid', 1)
->where('delete_time', null)
->select()
->toArray();
$articles = [];
$categoryData = [];
// 提取分类名称和ID用于前端tab显示
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name']
];
// 获取该分类下的文章限制4条
$articles = Articles::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 2)
->order('id', 'desc')
->field('id, cate, title, image, author, publishdate, views')
->limit(4)
->select()
->toArray();
}
return json([
'code' => 1,
'msg' => '获取成功',
'articles' => $articles,
'categories' => $categoryData
]);
}
/**
* 获取技术文章列表
*/
public function technicalArticleslist()
{
// 获取技术文章分类顶级分类id为3的子分类
$categories = ArticlesCategory::where('cid', 3)
->where('delete_time', null)
->select()
->toArray();
// 组装分类数据,方便后续查找
$categoryData = [];
$categoryImageMap = [];
$articlesByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name']
];
$categoryImageMap[$category['id']] = $category['image'] ?? '';
// 获取每个分类下的文章限制12条
$articles = Articles::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 2)
->order('id', 'desc')
->field('id, cate, title, image, author, publishdate, views')
->limit(12)
->select()
->toArray();
// 替换image为空的文章
foreach ($articles as &$article) {
if (empty($article['image']) && !empty($categoryImageMap[$article['cate']])) {
$article['image'] = $categoryImageMap[$article['cate']];
}
}
unset($article);
$articlesByCategory[$category['id']] = $articles;
}
return json([
'code' => 1,
'msg' => '获取成功',
'articles' => $articlesByCategory,
'categories' => $categoryData
]);
}
/**
* 获取banner列表
*/
public function bannerlist()
{
// 获取启用状态的banner列表按排序倒序
$bannerList = Banner::where('delete_time', null)
->order('sort DESC, id DESC')
->select()
->toArray();
return json(['code' => 1, 'msg' => '获取成功', 'banner' => $bannerList]);
}
/**
* 获取资源下载列表
*/
public function resourcesList()
{
// 获取资源分类顶级分类id为2的子分类
$categories = ResourcesCategory::where('cid', 2)
->where('delete_time', null)
->select()
->toArray();
// 组装分类数据
$categoryData = [];
$categoryImageMap = [];
$resourcesByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name']
];
$categoryImageMap[$category['id']] = $category['image'] ?? '';
// 获取每个分类下的资源限制8条
$resources = Resources::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 1)
->order('id', 'desc')
->field('id, cate, title, desc, downloads, create_time, icon, views, uploader')
->limit(8)
->select()
->toArray();
// 替换thumbnail为空的资源
foreach ($resources as &$resource) {
if (empty($resource['thumbnail']) && !empty($categoryImageMap[$resource['cate']])) {
$resource['thumbnail'] = $categoryImageMap[$resource['cate']];
}
}
unset($resource);
$resourcesByCategory[$category['id']] = $resources;
}
// 合并所有分类的资源
$allResources = [];
foreach ($resourcesByCategory as $resources) {
$allResources = array_merge($allResources, $resources);
}
// 按上传时间排序
usort($allResources, function ($a, $b) {
return $b['create_time'] - $a['create_time'];
});
// 只取最新的8条
$allResources = array_slice($allResources, 0, 8);
return json([
'code' => 1,
'msg' => '获取成功',
'resources' => $allResources,
'categories' => $categoryData
]);
}
/**
* 获取程序下载列表
*/
public function programList()
{
// 获取程序分类顶级分类id为4的子分类
$categories = ResourcesCategory::where('cid', 1)
->where('delete_time', null)
->select()
->toArray();
// 组装分类数据
$categoryData = [];
$categoryImageMap = [];
$programsByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name']
];
$categoryImageMap[$category['id']] = $category['image'] ?? '';
// 获取每个分类下的程序限制8条
$programs = Resources::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 1)
->order('id', 'desc')
->field('id, cate, title, desc, downloads, create_time, icon, views, uploader')
->limit(8)
->select()
->toArray();
// 替换thumbnail为空的程序
foreach ($programs as &$program) {
if (empty($program['thumbnail']) && !empty($categoryImageMap[$program['cate']])) {
$program['thumbnail'] = $categoryImageMap[$program['cate']];
}
}
unset($program);
$programsByCategory[$category['id']] = $programs;
}
// 合并所有分类的程序
$allPrograms = [];
foreach ($programsByCategory as $programs) {
$allPrograms = array_merge($allPrograms, $programs);
}
// 按上传时间排序
usort($allPrograms, function ($a, $b) {
return $b['create_time'] - $a['create_time'];
});
// 只取最新的8条
$allPrograms = array_slice($allPrograms, 0, 8);
return json([
'code' => 1,
'msg' => '获取成功',
'programs' => $allPrograms,
'categories' => $categoryData
]);
}
/**
* 获取游戏下载列表
*/
public function gameList()
{
// 获取游戏分类顶级分类id为8的子分类
$categories = ResourcesCategory::where('cid', 8)
->where('delete_time', null)
->select()
->toArray();
// 组装分类数据
$categoryData = [];
$categoryImageMap = [];
$programsByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name'],
'image' => $category['image'] ?? ''
];
// 获取每个分类下的游戏限制8条
$programs = Resources::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 1)
->order('id', 'desc')
->field('id, cate, title, desc, downloads, create_time, icon, views, uploader, number, url, code')
->limit(8)
->select()
->toArray();
// 处理游戏数据
foreach ($programs as &$program) {
// 如果没有图标,使用分类图片
if (empty($program['icon']) && !empty($category['image'])) {
$program['icon'] = $category['image'];
}
// 格式化时间
$program['create_time'] = date('Y-m-d H:i:s', $program['create_time']);
}
unset($program);
$programsByCategory[$category['id']] = $programs;
}
// 合并所有分类的游戏
$allPrograms = [];
foreach ($programsByCategory as $programs) {
$allPrograms = array_merge($allPrograms, $programs);
}
// 按上传时间排序
usort($allPrograms, function ($a, $b) {
return strtotime($b['create_time']) - strtotime($a['create_time']);
});
// 只取最新的8条
$allPrograms = array_slice($allPrograms, 0, 8);
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'games' => $allPrograms,
'categories' => $categoryData
]
]);
}
//保存附件信息到数据库
private function saveAttachment($name, $type, $size, $src)
{
$data = [
'name' => $name,
'type' => $type,
'size' => $size,
'src' => $src,
'create_time' => time(),
'update_time' => time()
];
return Attachments::insertGetId($data);
}
//上传图片接口
public function update_imgs()
{
// 获取上传的文件
$file = request()->file();
$files = request()->file('file');
// 检查是否有文件上传
if (empty($file)) {
return json(['code' => 1, 'msg' => '没有文件上传']);
}
try {
// 验证上传的文件
validate([
'image' => 'filesize:51200|fileExt:jpg,png,gif,jpeg,webp'
])->check($file);
// 存储文件到public磁盘的uploads目录
$info = Filesystem::disk('public')->putFile('uploads', $files);
// 处理文件路径,统一使用正斜杠
$info = str_replace("\\", "/", $info);
$img = '/storage/' . $info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 1, $fileSize, $img); // 1: 图片
// 返回成功信息
return json([
'code' => 0,
'data' => [
'url' => $img
],
'msg' => '上传成功'
]);
} catch (\think\exception\ValidateException $e) {
// 捕获验证异常并返回错误信息
return json(['code' => 1, 'msg' => $e->getMessage()]);
} catch (\Exception $e) {
// 捕获其他异常
return json(['code' => 1, 'msg' => '上传失败:' . $e->getMessage()]);
}
}
}