heart_cabin_api/app/admin/controller/News/News.php
2023-08-23 16:06:54 +08:00

203 lines
6.2 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
namespace app\admin\controller\News;
use app\BaseController;
use app\common\model\News\News as ModelNews;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
class News extends BaseController
{
/**
* 获取新闻列表
*/
public function getNewsList(Request $request, $isExport = false): array
{
$params = $request->param();
$con = [];
$con = Tool::getOptionalQuery(['news_type', '='], ['news_title', 'LIKE'],);
$query = ModelNews::where($con)
->field([
'news_id',
'news_guid',
'news_type',
'news_cover',
'news_title',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content'
])
->order('news_sort', 'asc');
return $isExport ? $query->select()->toArray() : msg("获取新闻列表成功!", $query);
}
/**
* 添加新闻
*/
public function addNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_type|新闻类型' => 'require',
'news_title|新闻标题' => 'require',
'news_cover|新闻封面' => 'require',
'news_sort|新闻排序' => 'require'
]);
$model = ModelNews::create($params, [
'news_type',
'news_title',
'news_cover',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content',
'news_guid',
'news_create_user_guid',
'news_update_user_guid'
]);
Db::commit();
Tool::unlockTable();
return msg('添加成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 编辑新闻
*/
public function editNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_type|新闻类型' => 'require',
'news_title|新闻标题' => 'require',
'news_cover|新闻封面' => 'require',
'news_sort|新闻排序' => 'require'
]);
$model = ModelNews::where('news_guid', $params['news_guid'])->find();
if (!$model) throwErrorMsg("该新闻不存在", 1);
$model->allowField([
'news_type',
'news_title',
'news_cover',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content',
'news_update_user_guid'
])->save($params);
Db::commit();
Tool::unlockTable();
return msg('编辑成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 删除新闻
*/
public function deleteNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_guid' => 'require',
]);
$news = ModelNews::where([
'news_guid' => explode(',', $params['news_guid'])
])->select();
$news->delete();
Db::commit();
Tool::unlockTable();
return msg('删除成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 导出Excel
*/
public function exportExcel(Request $request): void
{
ModelNews::exportExcel(self::getNewsList($request, true));
}
/**
* 下载导入模板
*/
public function downloadTemplate(Request $request): void
{
$params = $request->param();
$data = [
array_values(ModelNews::EXCELFIELD),
[
'公司新闻',
'想学嵌入式Linux领免费的瑞萨RZ/G2L开发板',
'https://www.myir-tech.com/attached/image/20230609/1-1.jpg',
'学习嵌入式系统开发是一个渐进的过程一般我们从51单片机开始逐步迁移到STM32微控制器然后学习使用FreeRTOS操作系统最终进入嵌入式Linux领域。以下是一个典型的学习路线51单片机作为嵌入式系统的入门级平台学习51单片机可以帮助我们了解基本的嵌入式开发概念和编程技巧。可以学习使用C语言编写简单的驱动程序、控制IO口和外设等。 ',
'米尔科技',
'',
'2023.6.9',
'0',
'1',
'<p style="text-align: start;"><strong>需要报名RZ/G2L免费开发板请扫码</strong></p><p style="text-align: center;"><img src="https://www.myir-tech.com/attached/image/20230609/22.png" alt="" data-href="" style=""></p>',
]
];
$excel = (new Excel())->exporTsheet($data);
$excel->save('新闻导入模板.xlsx');
}
/**
* 导入excel
*/
public function importExcel(Request $request): array
{
$file = new UploadFile('uploads', 'fileExt:xlsx');
$file->putFile('news');
$msg = ModelNews::importExcel($file);
return [
'code' => 0,
'msg' => $msg
];
}
}