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.

149 lines
5.2 KiB

11 months ago
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Config;
use Illuminate\Support\Facades\DB;
11 months ago
use Illuminate\Support\Facades\Validator;
11 months ago
use App\Helpers\ResponseCode;
11 months ago
/**
11 months ago
* 配置
11 months ago
*/
11 months ago
class ConfigController extends CommonController
11 months ago
{
/**
* @OA\Get(
11 months ago
* path="/api/admin/config/index",
* tags={"配置"},
* summary="列表",
* description="",
11 months ago
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
11 months ago
* description="暂无"
11 months ago
* )
* )
11 months ago
*/
11 months ago
public function index()
{
$all = request()->all();
11 months ago
$list = Config::where(function ($query) use ($all){
})->orderBy($all['sort_name']??'id',$all['sort_type']??'desc')
->paginate($all['page_size']??20);
return $this->success($list);
11 months ago
}
/**
* @OA\Get(
11 months ago
* path="/api/admin/config/show",
* tags={"配置"},
* summary="详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
11 months ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
11 months ago
* description="暂无"
11 months ago
* )
* )
*/
public function show()
{
11 months ago
$all = \request()->all();
$messages = [
'id.required' => 'Id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
],$messages);
if($validator->fails()){
return $this->fail([ResponseCode::ERROR_PARAMETER,implode(',',$validator->errors()->all())]);
}
$detail = Config::find($all['id']);
11 months ago
return $this->success($detail);
}
/**
* @OA\Post(
11 months ago
* path="/api/admin/config/save",
* tags={"配置"},
* summary="更新",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名字"),
* @OA\Parameter(name="key", in="query", @OA\Schema(type="string"), required=true, description="标示"),
* @OA\Parameter(name="value", in="query", @OA\Schema(type="string"), required=false, description="值"),
11 months ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
11 months ago
* description="暂无"
11 months ago
* )
* )
*/
public function save()
{
11 months ago
$all = \request()->all();
$messages = [
'name.required' => '名称必填'
];
$validator = Validator::make($all, [
'name' => 'required'
],$messages);
if($validator->fails()){
return $this->fail([ResponseCode::ERROR_PARAMETER,implode(',',$validator->errors()->all())]);
}
11 months ago
DB::beginTransaction();
try {
11 months ago
if(isset($all['id'])){
$model = Config::find($all['id']);
}else{
$model = new Config();
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
11 months ago
}
11 months ago
$model->fill($all);
$model->save();
11 months ago
DB::commit();
11 months ago
return $this->success('更新成功');
}catch (\Exception $exception) {
11 months ago
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
11 months ago
}
11 months ago
11 months ago
/**
11 months ago
* @OA\Get(
11 months ago
* path="/api/admin/config/destroy",
* tags={"配置"},
* summary="删除",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
11 months ago
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
11 months ago
* description="暂无"
11 months ago
* )
* )
*/
public function destroy()
{
11 months ago
$all = \request()->all();
$messages = [
'id.required' => 'Id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
],$messages);
if($validator->fails()){
return $this->fail([ResponseCode::ERROR_PARAMETER,implode(',',$validator->errors()->all())]);
11 months ago
}
11 months ago
Config::where('id', $all['id'])->delete();
return $this->success('删除成功');
11 months ago
}
11 months ago
}