glhcp/server/app/admin/controller/content/Demand.php

145 lines
3.6 KiB
PHP

<?php
namespace app\admin\controller\content;
use app\admin\logic\content\HelpCategoryLogic;
use app\admin\logic\content\HelpLogic;
use app\admin\validate\content\HelpValidate;
use app\common\basics\AdminBase;
use app\common\server\JsonServer;
use app\common\model\Demand As thisModel;
use think\facade\Validate;
class Demand extends AdminBase
{
/**
* @NOTES: 列表
* @author: 张无忌
*/
public function lists()
{
if ($this->request->isAjax()) {
try {
$page = input('page/d', 1);
$limit = input('limit/d', 10);
$keyword = input('keyword/s');
$page = $page ?: 1;
$limit = $limit ?: 10;
$where = [];
$order = [
'id' => 'desc'
];
if (!empty($keyword)) {
$where[] = ['name', 'like', '%'.trim($keyword).'%'];
}
$count = thisModel::where($where)->count();
$list = thisModel::withCount(['reports'])->field(['id', 'name', 'create_time'])
->where($where)
->order($order)
->page($page,$limit)
->select()
->toArray();
$data = [
'lists' => $list,
'page_no' => $page,
'page_size' => $limit,
'count' => $count,
];
return JsonServer::success('获取成功', $data);
} catch (\Exception $e) {
return JsonServer::error('获取失败');
}
}
return view();
}
/**
* @NOTES: 添加
* @author: 张无忌
*/
public function add()
{
if ($this->request->isAjax()) {
$input = input('post.');
$rule = [
'name|需求名称' => 'require|min:2',
'content|介绍' => 'require',
];
$validate = Validate::rule($rule);
if (!$validate->check($input)) {
return JsonServer::error($validate->getError());
}
thisModel::create([
'name' => $input['name'],
'content' => $input['content'],
'create_time' => time(),
]);
return JsonServer::success('添加成功');
}
return view();
}
/**
* @NOTES: 编辑
* @author: 张无忌
*/
public function edit()
{
$id = input('id');
$item = thisModel::where('id', $id)->find();
if ($this->request->isAjax()) {
$input = input('post.');
$rule = [
'name|需求名称' => 'require|min:2',
'content|介绍' => 'require',
];
$validate = Validate::rule($rule);
if (!$validate->check($input)) {
return JsonServer::error($validate->getError());
}
$item->save([
'name' => $input['name'],
'content' => $input['content'],
]);
return JsonServer::success('编辑成功');
}
return view('', [
'detail' => $item,
]);
}
/**
* @NOTES: 删除
* @author: 张无忌
*/
public function del()
{
if ($this->request->isAjax()) {
thisModel::where('id', input('id/d'))->delete();
return JsonServer::success('删除成功');
}
return JsonServer::error('异常');
}
}