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.
 
 
 
 
 
 

138 lines
3.9 KiB

<?php
declare (strict_types = 1);
namespace {%namespace%};
use app\model\{%modelName%};
use app\validate\{%modelName%}Validate;
use think\exception\ValidateException;
class {%className%} extends BaseController
{
/**
* 显示资源列表
*
* @return \think\response\Json
* @throws \app\exception\ModelException
*/
public function index{%actionSuffix%}({%modelName%} ${%modelInstance%}): \think\response\Json
{
$where = [
'seller_id' => $this->admin['seller_id'],
'is_del' => 1,
];
$limit = 10;
$limitParam = (int)input('limit');
if($limitParam){
$limit = $limitParam;
}
// TODO
// 添加其他逻辑
${%modelInstance%}List = ${%modelInstance%}->get{%modelName%}List($where,$limit);
return json(pageReturn(${%modelInstance%}List));
}
/**
* 保存新建的资源
*
* @return \think\response\Json
* @throws \app\exception\ModelException
*/
public function save{%actionSuffix%}({%modelName%} ${%modelInstance%}): \think\response\Json
{
if(request()->isPost()){
$param = input('post.');
// 数据验证
try{
validate({%modelName%}Validate::class)->scene('save')->check($param);
}catch(ValidateException $e){
return jsonReturn(-1, $e->getError());
}
// TODO
// 其他逻辑
$res = ${%modelInstance%} -> add{%modelName%}($param);
return json($res);
}
return jsonReturn(-3,Lang::get('请求方法错误'));
}
/**
* 显示指定的资源
*
* @return \think\response\Json
* @throws \app\exception\ModelException
* @throws \app\exception\ModelEmptyException
*/
public function read{%actionSuffix%}({%modelName%} ${%modelInstance%}): \think\response\Json
{
$id = (int)input('id');
if(!$id){
// TODO
// 修改错误消息
return jsonReturn(-1,'ErrorMsg');
}
$where = [
'id' => $id,
'seller_id' => $this->admin['seller_id']
];
// TODO
// 其他逻辑
$res = ${%modelInstance%}->get{%modelName%}($where);
return json($res);
}
/**
* 保存更新的资源
* @return \think\response\Json
* @throws \app\exception\ModelException
*/
public function update{%actionSuffix%}({%modelName%} ${%modelInstance%}): \think\response\Json
{
if(request()->isPost()){
$param = input('post.');
try {
validate({%modelName%}Validate::class)->scene('update')->check($param);
} catch (ValidateException $e) {
return jsonReturn(-1, $e->getError());
}
$where = [
'id' => $param['id'],
'seller_id' => $this->admin['seller_id'],
'is_del' => 1,
];
$res = ${%modelInstance%} -> update{%modelName%}($where,$param);
return json($res);
}
return jsonReturn(-3,Lang::get('请求方法错误'));
}
/**
* 删除指定资源
*
* @throws \app\exception\ModelException
*/
public function delete{%actionSuffix%}({%modelName%} ${%modelInstance%}): \think\response\Json
{
if(request()->isPost()){
$id = (int)input('id');
if(!$id){
// TO DO
// 替换错误提示
return jsonReturn(-1,'ErrorMsg');
}
$where = [
'id' => $id,
'seller_id' => $this->admin['seller_id']
];
$res = ${%modelInstance%}->del{%modelName%}($where);
return json($res);
}
return jsonReturn(-3,Lang::get('请求方法错误'));
}
}