发票管理apiadmin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

85 lines
2.7 KiB

<?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());
}
}
}