4 changed files with 122 additions and 0 deletions
@ -0,0 +1,85 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\controller\admin; |
||||
|
|
||||
|
use app\model\Config as ConfigModel; |
||||
|
use app\util\ReturnCode; |
||||
|
use app\validate\ConfigValidate; |
||||
|
use think\Response; |
||||
|
|
||||
|
class Config extends Base |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 首页列表显示 |
||||
|
* @return \think\Response |
||||
|
*/ |
||||
|
public function index(): Response |
||||
|
{ |
||||
|
try { |
||||
|
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT')); |
||||
|
$start = $this->request->get('page', 1); |
||||
|
|
||||
|
$listObj = (new ConfigModel())->where('delete_time', 0) |
||||
|
->withoutField('delete_time, content') |
||||
|
->paginate(['page' => $start, 'list_rows' => $limit]) |
||||
|
->toArray(); |
||||
|
|
||||
|
return $this->buildSuccess([ |
||||
|
'list' => $listObj['data'], |
||||
|
'count' => $listObj['total'], |
||||
|
]); |
||||
|
} catch (\Exception $e) { |
||||
|
return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 新增字段 |
||||
|
* @return Response |
||||
|
* @author zhaoxiang <zhaoxiang051405@gmail.com> |
||||
|
*/ |
||||
|
public function add(): Response |
||||
|
{ |
||||
|
try { |
||||
|
$postData = $this->request->post(); |
||||
|
$id = $postData['id'] ?? ''; |
||||
|
validate(ConfigValidate::class)->scene($id ? 'edit' : 'add')->check($postData); |
||||
|
$data = [ |
||||
|
'title' => $postData['title'], |
||||
|
'name' => $postData['name'], |
||||
|
'status' => $postData['status'], |
||||
|
'content' => json_encode($postData['content']), |
||||
|
]; |
||||
|
if ($id) { |
||||
|
$data['update_time'] = time(); |
||||
|
ConfigModel::update($data, ['id' => $id]); |
||||
|
} else { |
||||
|
$data['create_time'] = time(); |
||||
|
ConfigModel::create($data); |
||||
|
} |
||||
|
return $this->buildSuccess(); |
||||
|
} catch (\Exception $e) { |
||||
|
return $this->buildFailed(ReturnCode::CACHE_SAVE_ERROR, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 编辑添加数据 |
||||
|
* @return Response |
||||
|
*/ |
||||
|
public function editIndex(): Response |
||||
|
{ |
||||
|
try { |
||||
|
$id = $this->request->get('id', ''); |
||||
|
if (!$id) throw new \Exception('缺少必填参数'); |
||||
|
$data = (new ConfigModel())->withoutField('delete_time, update_time')->where('id', $id)->where('delete_time', 0)->find(); |
||||
|
$data['content'] = $data['content'] ? json_decode($data['content'], true) : $data['content']; |
||||
|
return $this->buildSuccess($data); |
||||
|
} catch (\Exception $e) { |
||||
|
return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use think\Model; |
||||
|
|
||||
|
class Config extends Model |
||||
|
{ |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\validate; |
||||
|
|
||||
|
use think\Validate; |
||||
|
|
||||
|
class ConfigValidate extends Validate |
||||
|
{ |
||||
|
|
||||
|
protected $rule = [ |
||||
|
'id' => 'require|number', |
||||
|
'title' => 'require', |
||||
|
'name' => 'require', |
||||
|
'content' => 'require' |
||||
|
]; |
||||
|
|
||||
|
protected $message = []; |
||||
|
|
||||
|
protected $scene = [ |
||||
|
'add' => ['title', 'name', 'content'], |
||||
|
'edit' => ['id', 'title', 'name', 'content'], |
||||
|
]; |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue