|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Helpers\ResponseCode;
|
|
|
use App\Models\CustomFormField;
|
|
|
use App\Models\EditToMigration;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
use Overtrue\Pinyin\Pinyin;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 自定义表单
|
|
|
*/
|
|
|
class CustomFormFieldController extends CommonController
|
|
|
{
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/custom-form-field/index",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="列表",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="custom_form_id", in="query", @OA\Schema(type="string"), required=false, description="表id"),
|
|
|
* @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",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function index()
|
|
|
{
|
|
|
$all = request()->all();
|
|
|
$list = CustomFormField::where(function ($query) use ($all) {
|
|
|
if (isset($all['custom_form_id'])) {
|
|
|
$query->where('custom_form_id', $all['custom_form_id']);
|
|
|
}
|
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
|
->paginate($all['page_size'] ?? 20);
|
|
|
return $this->success($list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/custom-form-field/show",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="详情",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function show()
|
|
|
{
|
|
|
$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 = CustomFormField::find($all['id']);
|
|
|
return $this->success($detail);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/custom-form-field/save",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="更新",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"),
|
|
|
* @OA\Parameter(name="custom_form_id", in="query", @OA\Schema(type="string"), required=true, description="表单id"),
|
|
|
* @OA\Parameter(name="field", in="query", @OA\Schema(type="string"), required=true, description="字段标示(汉字转拼音接口自动填充)"),
|
|
|
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名字"),
|
|
|
* @OA\Parameter(name="search_input", in="query", @OA\Schema(type="string"), required=false, description="查询类型text输入框,radio单选,date_range日期范围选择,datetime_range日期时间范围选择"),
|
|
|
* @OA\Parameter(name="edit_input", in="query", @OA\Schema(type="string"), required=true, description="编辑类型,从配置接口获取"),
|
|
|
* @OA\Parameter(name="sort", in="query", @OA\Schema(type="string"), required=false, description="排序"),
|
|
|
* @OA\Parameter(name="help", in="query", @OA\Schema(type="string"), required=false, description="帮助文字"),
|
|
|
* @OA\Parameter(name="validation", in="query", @OA\Schema(type="string"), required=false, description="验证规则,config接口获取,一维数组"),
|
|
|
* @OA\Parameter(name="select_item", in="query", @OA\Schema(type="string"), required=false, description="选择框选项"),
|
|
|
* @OA\Parameter(name="list_show", in="query", @OA\Schema(type="string"), required=false, description="是否列表显示0和1"),
|
|
|
* @OA\Parameter(name="form_show", in="query", @OA\Schema(type="string"), required=false, description="是否表单显示0和1"),
|
|
|
* @OA\Parameter(name="is_fix", in="query", @OA\Schema(type="string"), required=true, description="是否固定0否1是"),
|
|
|
* @OA\Parameter(name="width", in="query", @OA\Schema(type="string"), required=true, description="列宽度"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function save()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'field.required' => '字段标示必填',
|
|
|
'name.required' => '名称必填',
|
|
|
'custom_form_id.required' => '表id必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'field' => 'required',
|
|
|
'name' => 'required',
|
|
|
'custom_form_id' => 'required'
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
if (isset($all['id'])) {
|
|
|
$model = CustomFormField::find($all['id']);
|
|
|
} else {
|
|
|
$model = new CustomFormField();
|
|
|
$all['admin_id'] = $this->getUserId();
|
|
|
$all['department_id'] = $this->getUser()->department_id;
|
|
|
}
|
|
|
// 检测字段合法性
|
|
|
$model->checkFields($all);
|
|
|
$model->fill($all);
|
|
|
$model->save();
|
|
|
DB::commit();
|
|
|
return $this->success("更新成功");
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/custom-form-field/destroy",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="删除",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function destroy()
|
|
|
{
|
|
|
$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())]);
|
|
|
}
|
|
|
CustomFormField::where('id', $all['id'])->delete();
|
|
|
return $this->success('删除成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/custom-form-field/pinyin",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="汉字转拼音",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="str", in="query", @OA\Schema(type="string"), required=true, description="汉字"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function pinyin()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'str.required' => '汉字必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'str' => 'required'
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$pinyin = Pinyin::permalink($all['str'], '');
|
|
|
return $this->success(['pinyin' => $pinyin]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/custom-form-field/config",
|
|
|
* tags={"自定义表单字段"},
|
|
|
* summary="验证规则选项和编辑类型选项",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function config()
|
|
|
{
|
|
|
// 验证规则选项
|
|
|
$validationRules = CustomFormField::$validationRules;
|
|
|
// 编辑类型选项
|
|
|
$editToMigration = EditToMigration::get();
|
|
|
return $this->success(['validation_rules' => $validationRules, 'edit_to_migration' => $editToMigration]);
|
|
|
}
|
|
|
|
|
|
}
|