2025-06-25 10:53:11 +08:00

212 lines
7.5 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @author 李志强
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\project\controller;
use app\base\BaseController;
use app\model\ProjectDemo as ProjectDemoList;
use app\model\DevelopmentLanguage as DevelopmentLanguageList;
use app\project\validate\ProjectDemoCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Demo extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
$list = ProjectDemoList::withoutField('')
->where('delete_time', null)
->order('name')
->order('create_time', 'desc')
->paginate($rows, false, ['query' => $param])
->each(function ($item, $key) {
$bi = Db::name('ProjectDemo')->where(['id' => $item->id])->find();
$item->name = $bi['name'];
$item->url = $bi['url'];
$item->url_cate = $bi['url_cate'];
$item->cate = $bi['cate'];
$item->remark = $bi['remark'];
$item->language = Db::name('development_language')->where('id', $bi['language'])->value('name');
})
->filter(function ($item) {
return $item->delete_time === null;
});
return table_assign(0, '', $list);
} else {
return view();
}
}
//添加
public function add()
{
$param = get_params();
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$projectdemo = (new ProjectDemoList())->detail($param['id']);
try {
validate(ProjectDemoCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = ProjectDemoList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $projectdemo);
}
return to_assign();
} else {
try {
validate(ProjectDemoCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
// $exist = ProjectDemoList::where('name', $param['name'])
// ->where('id', '<>', $param['id'])->find();
// if ($exist) {
// return to_assign(1, '该信息已存在');
// }
$param['create_time'] = date('Y-m-d H:i:s', time());
$param['admin_id'] = $this->uid;
$sid = ProjectDemoList::strict(false)->field(true)->insertGetId($param);
if ($sid) {
add_log('add', $sid, $param);
$log_data = array(
'module' => 'plans',
// 'id' => $sid,
'new_content' => $param['name'],
'field' => 'new',
'action' => 'add',
'admin_id' => $this->uid,
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
return to_assign(2, '添加成功!');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new ProjectDemoList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//编辑
public function edit()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$edit = ProjectDemoList::where('id', $id)->find();
if (empty($edit)) {
return to_assign(1, '信息不存在');
} else {
View::assign('edit', $edit);
return view();
}
}
//查看
public function view()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = (new ProjectDemoList())->detail($id);
// print_r($id);
if (empty($detail)) {
return to_assign(1, '2.信息不存在');
} else {
View::assign('detail', $detail);
// print_r($detail);
return view();
}
}
//删除
public function delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$id = get_params("id");
$detail = Db::name('ProjectDemo')->where('id', $id)->find();
if (Db::name('ProjectDemo')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'projectdemo',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功");
} else {
return to_assign(0, "删除失败");
}
} else {
return to_assign(1, "错误的请求");
}
}
//批量删除
public function batch_delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$ids = get_params("ids");
foreach ($ids as $id) {
$detail = Db::name('ProjectDemo')->where('id', $id)->find();
if (Db::name('ProjectDemo')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'projectdemo',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
}
return to_assign(0, "批量删除成功");
} else {
return to_assign(1, "错误的请求");
}
}
// 获取开发语言
public function getLanguage()
{
$languages = DevelopmentLanguageList::order('id', 'aec')->column('name', 'id');
$formattedLanguages = [];
foreach ($languages as $id => $language) {
$formattedLanguages[] = ["id" => $id, "language" => $language];
}
return json($formattedLanguages);
}
}