building-sign/app/command/stubs/controller.stub

178 lines
4.3 KiB
Plaintext
Raw Normal View History

2023-01-09 08:41:41 +00:00
<?php
declare (strict_types = 1);
namespace {%namespace%};
use Exception;
use app\model\Log;
use think\Collection;
use think\response\View;
use think\response\Json;
use app\controller\manager\Base;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
class {%className%} extends Base
{
protected $noNeedLogin = ['index', 'add', 'edit', 'del', 'modify'];
/**
* 列表
*
* @throws Exception
*/
public function index()
{
if ($this->request->isPost()) {
// $params = input();
$res = [
'total' => 5,
'current' => 1,
'size' => 10,
'list' => new Collection(),
];
$res['list'] = [
['id' => 1, 'name' => '张伟', 'status' => '111', 'sort' => 11],
['id' => 2, 'name' => '王兴龙', 'status' => '222', 'sort' => 12],
['id' => 3, 'name' => '菜盘', 'status' => '333', 'sort' => 13],
['id' => 4, 'name' => '老郑', 'status' => '444', 'sort' => 14],
['id' => 5, 'name' => '大帅比', 'status' => '555', 'sort' => 15]
];
return $this->json(0, 'success', $res);
}
return $this->view();
}
/**
* 添加
*
* @return Json|View
*/
public function add()
{
if ($this->request->isPost()) {
try {
$input = input('post.');
// 添加逻辑 TODO
return $this->json();
} catch (Exception $e) {
return $this->json(4001, '添加失败');
}
}
return $this->view();
}
/**
* 编辑
*
* @return \think\response\Json|\think\response\View
*/
public function edit()
{
$id = input('id');
//通过ID查询
$item = ['id' => 11, 'name' => '标题', 'cover' => '/xxx/xxx/xxx.jpg', 'content' => '我是大帅比'];
if (empty($item)) {
return $this->json(4000, '没有相关的商品记录!');
}
if ($this->request->isPost()) {
try {
$input = input('post.');
// 更新逻辑
return $this->json();
} catch (Exception $e) {
return $this->json(5000, $e->getMessage());
}
}
$this->data['item'] = $item;
$this->data['id'] = $id;
return $this->view();
}
/**
* 更新属性
*
* @throws ModelNotFoundException
* @throws DbException
* @throws DataNotFoundException
* @throws Exception
*/
public function modify()
{
if (!$this->request->isPost()) {
return $this->json(4000, '非法请求');
}
$item = input('post.');
$validate = $this->validateByApi($item, [
'field' => 'require',
'value' => 'require',
]);
if ($validate !== true) {
return $validate;
}
// 通过ID查询
if (!$info = []) {
// return $this->json(4001, '记录不存在');
}
$update = [$item['field'] => $item['value']];
try {
// $info->save($update);
return $this->json();
} catch (ValidateException $e) {
return $this->json(4001, $e->getError());
} catch (Exception $e) {
return $this->json(5000, '修改失败');
}
}
/**
* 删除
*
* @return \think\response\Json
*/
public function del(): Json
{
if (!$this->request->isPost()) {
return $this->json(4000, '非法请求');
}
$ids = $this->request->param('ids/a', []);
if (empty($ids)) {
$ids[] = $this->request->param('id/d', 0);
$ids = array_filter($ids);
}
try {
if (count($ids)) {
//删除逻辑 TODO
Log::write(get_class(), 'del', '删除操作涉及到的ID为'.implode(',', $ids));
}
} catch (Exception $e) {
return $this->json(5000, $e->getMessage());
}
return $this->json();
}
}