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.
161 lines
4.3 KiB
161 lines
4.3 KiB
4 years ago
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\Api;
|
||
|
|
||
|
use App\Http\Resources\DictionaryCollection;
|
||
|
use App\Http\Resources\Dictionary;
|
||
|
use App\Models\DictionaryT;
|
||
|
use Illuminate\Http\Request;
|
||
|
|
||
|
/**
|
||
|
* @title 字典管理
|
||
|
* @description 字典管理
|
||
|
* @package App\Http\Controllers\Api
|
||
|
* @author zcstatham
|
||
|
* @time 2021/2/3
|
||
|
*/
|
||
|
class DictionaryController extends BaseController
|
||
|
{
|
||
|
|
||
|
protected $rules = [
|
||
|
'store' => [
|
||
|
'rules' => [
|
||
|
'name' => 'required',
|
||
|
'title' => 'required',
|
||
|
],
|
||
|
'custom' => 'unique,App\Models\DictionaryT,pid,name'
|
||
|
|
||
|
],
|
||
|
'show' => [
|
||
|
'rules' => [
|
||
|
'id' => 'bail|min:1',
|
||
|
],
|
||
|
'custom' => 'exists,App\Models\DictionaryT,id'
|
||
|
],
|
||
|
'update' => [
|
||
|
'rules' => [
|
||
|
'id' => 'bail|min:1',
|
||
|
'name' => 'required',
|
||
|
'title' => 'required',
|
||
|
],
|
||
|
'custom' => [
|
||
|
'exists,App\Models\DictionaryT,id',
|
||
|
'custom' => 'unique,App\Models\DictionaryT,pid,name,id'
|
||
|
]
|
||
|
],
|
||
|
'destroy' => [
|
||
|
'rules' => [
|
||
|
'id' => 'bail|min:1',
|
||
|
],
|
||
|
'custom' => [
|
||
|
'exists,App\Models\DictionaryT,id',
|
||
|
]
|
||
|
]
|
||
|
];
|
||
|
|
||
|
public function __construct(Request $request)
|
||
|
{
|
||
|
$this->model = new DictionaryT();
|
||
|
parent::__construct($request);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @title 字典列表
|
||
|
* @description 字典列表
|
||
|
* @return \Illuminate\Http\JsonResponse
|
||
|
* @author zcstatham
|
||
|
*/
|
||
|
public function index()
|
||
|
{
|
||
|
$query = $this->model->query();
|
||
|
|
||
|
if(isset($this->params['name']) && $this->params['name'] != ''){
|
||
|
$query->where('name', $this->params['name']);
|
||
|
}
|
||
|
|
||
|
if(isset($this->params['title']) && $this->params['title'] > 0){
|
||
|
$query->where('title', $this->params['title']);
|
||
|
}
|
||
|
|
||
|
if(isset($this->params['status']) && $this->params['status'] >= 0){
|
||
|
$query->where('status', $this->params['status']);
|
||
|
} else {
|
||
|
$query->where('status', 1);
|
||
|
}
|
||
|
|
||
|
$data = $query->paginate();
|
||
|
|
||
|
return $this->success(new DictionaryCollection($data));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @title 字典保存
|
||
|
* @description 字典保存
|
||
|
* @return \Illuminate\Http\JsonResponse
|
||
|
* @author zcstatham
|
||
|
*/
|
||
|
public function store()
|
||
|
{
|
||
|
$this->model->pid = $this->params['pid'] ?? 0;
|
||
|
$this->model->name = $this->params['name'];
|
||
|
$this->model->title = $this->params['title'];
|
||
|
$this->model->status = $this->params['status'] ?? '1';
|
||
|
if($this->model->save()){
|
||
|
return $this->success(new Dictionary($this->model));
|
||
|
} else {
|
||
|
return $this->error(500, '字典信息保存失败');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @title 字典详情
|
||
|
* @description 字典详情
|
||
|
* @param $id
|
||
|
* @return mixed
|
||
|
* @author zcstatham
|
||
|
*/
|
||
|
public function show($id)
|
||
|
{
|
||
|
return $this->success(new Dictionary($this->model->where('id', $id)->first()));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @title 字典更新
|
||
|
* @description 字典更新
|
||
|
* @param $id
|
||
|
* @return \Illuminate\Http\JsonResponse
|
||
|
* @author zcstatham
|
||
|
*/
|
||
|
public function update($id)
|
||
|
{
|
||
|
$this->model = $this->model->where('id', $id)->first();
|
||
|
$this->model->pid = $this->params['pid'] ?? $this->model->pid;
|
||
|
$this->model->name = $this->params['name'];
|
||
|
$this->model->title = $this->params['title'];
|
||
|
$this->model->status = $this->params['status'] ?? $this->model->status;
|
||
|
if($this->model->save()){
|
||
|
return $this->success(new Dictionary($this->model));
|
||
|
} else {
|
||
|
return $this->error(500, '字典信息保存失败');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @title 字典删除
|
||
|
* @description 字典删除
|
||
|
* @param $id
|
||
|
* @return \Illuminate\Http\JsonResponse
|
||
|
* @author zcstatham
|
||
|
*/
|
||
|
public function destroy($id)
|
||
|
{
|
||
|
$this->model = $this->model->where('id', $id)->first();
|
||
|
$this->model->status = 0;
|
||
|
if($this->model->save()){
|
||
|
return $this->success();
|
||
|
} else {
|
||
|
return $this->error(500, '字典信息保存失败');
|
||
|
}
|
||
|
}
|
||
|
}
|