diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index 268e6e0..0b5c85d 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -2,10 +2,13 @@ namespace App\Http\Controllers\Admin; +use App\Helpers\ResponseCode; use App\Models\Admin; +use App\Models\Department; use App\Models\OperateLog; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Rap2hpoutre\FastExcel\FastExcel; use Spatie\Permission\Models\Role; class AdminController extends CommonController @@ -78,6 +81,9 @@ class AdminController extends CommonController * @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), description="手机号码"), * @OA\Parameter(name="department_id", in="query", @OA\Schema(type="integer"), description="所属部门ID"), * @OA\Parameter(name="position", in="query", @OA\Schema(type="string"), description="职位"), + * @OA\Parameter(name="birthday", in="query", @OA\Schema(type="string"), description="生日"), + * @OA\Parameter(name="email", in="query", @OA\Schema(type="string"), description="邮箱"), + * @OA\Parameter(name="status", in="query", @OA\Schema(type="string"), description="状态0禁用1启用"), * @OA\Parameter(name="sortnumber", in="query", @OA\Schema(type="integer"), description="同级排序,默认为0"), * @OA\Response( * response="200", @@ -237,4 +243,65 @@ class AdminController extends CommonController } } + /** + * @OA\Post ( + * path="/api/admin/import", + * tags={"后台管理"}, + * summary="导入数据", + * description="", + * @OA\Parameter(name="file", in="query", @OA\Schema(type="object"), required=true, description="文件"), + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Response( + * response="200", + * description="导入用户" + * ) + * ) + */ + public function import(Request $request) + { + $file = $request->file('file'); + //判断文件是否有效 + if (!($request->hasFile('file') && $file->isValid())) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']); + } + //获取文件大小 + $img_size = floor($file->getSize() / 1024); + if ($img_size >= 5 * 1024) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于5M']); + } + //过滤文件后缀 + $ext = $file->getClientOriginalExtension(); + if (!in_array($ext, ['xls', 'xlsx', 'csv'])) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']); + } + $tempFile = $file->getRealPath(); + $dataArray = (new FastExcel)->import($tempFile)->toArray(); + // 获取所有key + $keyList = array_keys($dataArray[0]); + if (!in_array('GID(登录用户名)', $keyList)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, 'GID(登录用户名)字段不存在']); + } + if (!in_array('部门', $keyList)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '部门字段不存在']); + } + if (!in_array('姓名', $keyList)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '姓名字段不存在']); + } + if (!in_array('手机号码', $keyList)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '手机号码字段不存在']); + } + $list = []; + foreach ($dataArray as $key => $value) { + $departmentId = Department::where('name', $value['部门'])->value('id'); + $whereArray = ['name' => $value['姓名']]; + $updateDataArray = ['name' => $value['姓名'], 'username' => $value['GID(登录用户名)'], + 'department_id' => $departmentId, + 'mobile' => $value['手机号码'], + 'password'=> \Illuminate\Support\Facades\Hash::make("Admin" . date("Y")) + ]; + Admin::updateOrCreate($whereArray, $updateDataArray); + } + return $this->success($list); + } + } diff --git a/app/Http/Controllers/Admin/AuthController.php b/app/Http/Controllers/Admin/AuthController.php index 29ef04f..37fda51 100644 --- a/app/Http/Controllers/Admin/AuthController.php +++ b/app/Http/Controllers/Admin/AuthController.php @@ -4,12 +4,15 @@ namespace App\Http\Controllers\Admin; use App\Helpers\ApiResponse; +use App\Helpers\ResponseCode; use App\Helpers\StarterResponseCode; use App\Http\Controllers\Controller; use App\Models\Admin; use App\Models\OperateLog; use App\Models\Permission; use App\Models\RoleHasPermission; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Validator; class AuthController extends Controller { @@ -54,16 +57,25 @@ class AuthController extends Controller */ public function login() { - $credentials = request(['username', 'password']); - - if (!$token = $this->guard()->attempt($credentials)) { - return $this->fail(StarterResponseCode::LOGIN_FAIL); + $all = \request()->all(); + $messages = [ + 'username.required' => '用户名必填', + 'password.required' => '密码必填', + ]; + $validator = Validator::make($all, [ + 'username' => 'required', + 'password' => 'required', + ], $messages); + if ($validator->fails()) { + return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } - - $user = $this->guard()->user(); - $token = $user->createToken("token")->plainTextToken; + $admin = Admin::where('username', $all['username'])->first(); + if (!$admin || !Hash::check($all['password'], $admin->password)) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '账号密码不正确']); + } + $token = $admin->createToken("token")->plainTextToken; // 加日志 - OperateLog::addLogs($user, "用户[{$user->name}]登陆成功"); + OperateLog::addLogs($admin, "用户[{$admin->name}]登陆成功"); return $this->respondWithToken($token); } @@ -82,7 +94,7 @@ class AuthController extends Controller */ public function me() { - $user = Admin::with('department','role')->find($this->guard()->user()->id); + $user = Admin::with('department', 'role')->find($this->guard()->user()->id); return $this->success($user); } diff --git a/app/Http/Controllers/Admin/BaseController.php b/app/Http/Controllers/Admin/BaseController.php deleted file mode 100644 index 98be520..0000000 --- a/app/Http/Controllers/Admin/BaseController.php +++ /dev/null @@ -1,346 +0,0 @@ -model = $model; - } - - /** - * @OA\Get( - * path="/api/admin/{table_name}/index", - * tags={"通用接口"}, - * summary="列表", - * description="", - * @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是"), - * @OA\Parameter(name="export_fields", in="query", @OA\Schema(type="string"), required=false, description="需要导出的字段数组"), - * @OA\Parameter(name="filter", in="query", @OA\Schema(type="string"), required=false, description="查询条件。数组"), - * @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组,填写输出指定数据"), - * @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 = $this->model->with($all['show_relation'] ?? [])->where(function ($query) use ($all) { - if (isset($all['filter']) && !empty($all['filter'])) { - foreach ($all['filter'] as $condition) { - $key = $condition['key'] ?? null; - $op = $condition['op'] ?? null; - $value = $condition['value'] ?? null; - if (!isset($key) || !isset($op) || !isset($value)) { - continue; - } - // 等于 - if ($op == 'eq') { - $query->where($key, $value); - } - // 不等于 - if ($op == 'neq') { - $query->where($key, '!=', $value); - } - // 大于 - if ($op == 'gt') { - $query->where($key, '>', $value); - } - // 大于等于 - if ($op == 'egt') { - $query->where($key, '>=', $value); - } - // 小于 - if ($op == 'lt') { - $query->where($key, '<', $value); - } - // 小于等于 - if ($op == 'elt') { - $query->where($key, '<=', $value); - } - // 模糊搜索 - if ($op == 'like') { - $query->where($key, 'like', '%' . $value . '%'); - } - // 否定模糊搜索 - if ($op == 'notlike') { - $query->where($key, 'not like', '%' . $value . '%'); - } - // 范围搜索 - if ($op == 'range') { - list($from, $to) = explode(',', $value); - if (empty($from) || empty($to)) { - continue; - } - $query->whereBetween($key, [$from, $to]); - } - } - } - })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc'); - if (isset($all['is_export']) && !empty($all['is_export'])) { - $list = $list->limit(5000)->get()->toArray(); - return Excel::download(new BaseFormExport($list, $all['export_fields'] ?? ''), $all['file_name'] ?? '' . date('YmdHis') . '.xlsx'); - } else { - // 输出 - $list = $list->paginate($all['page_size'] ?? 20); - } - return $this->success($list); - } - - /** - * @OA\Get( - * path="/api/admin/{table_name}/show", - * tags={"通用接口"}, - * summary="详情", - * description="", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), - * @OA\Parameter(name="show_relation", 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 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 = $this->model->with($all['show_relation'] ?? [])->find($all['id']); - return $this->success($detail); - } - - /** - * @OA\Post( - * path="/api/admin/{table_name}/save", - * tags={"通用接口"}, - * summary="更新", - * description="", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), 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 save() - { - $all = \request()->all(); - DB::beginTransaction(); - try { - if (isset($all['id'])) { - $model = $this->model->find($all['id']); - if (empty($model)) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); - } - } else { - $model = $this->model; - $all['admin_id'] = $this->getUserId(); - $all['department_id'] = $this->getUser()->department_id; - } - $model->fill($all); - $model->save(); - DB::commit(); - return $this->success($model); - } catch (\Exception $exception) { - DB::rollBack(); - return $this->fail([$exception->getCode(), $exception->getMessage()]); - } - } - - /** - * @OA\Get( - * path="/api/admin/{table_name}/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())]); - } - $model = $this->model->find($all['id']); - if (empty($model)) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); - } - $model->delete(); - return $this->success('删除成功'); - } - - /** - * @OA\Post( - * path="/api/admin/{table_name}/excel-show", - * tags={"通用接口"}, - * summary="导入预览", - * description="", - * @OA\Parameter(name="data", in="query", @OA\Schema(type="string"), required=true, description="统一数据键值对数组"), - * @OA\Parameter(name="file", 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 excelShow() - { - $file = \request()->file('file'); - $data = \request('data', []); - //判断文件是否有效 - if (!(\request()->hasFile('file') && $file->isValid())) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']); - } - //获取文件大小 - $img_size = floor($file->getSize() / 1024); - if ($img_size >= 50 * 1024) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于50M']); - } - //过滤文件后缀 - $ext = $file->getClientOriginalExtension(); - if (!in_array($ext, ['xls', 'xlsx', 'csv'])) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']); - } - $tempFile = $file->getRealPath(); - $dataArray = (new FastExcel)->import($tempFile)->toArray(); - // 数据过滤,只能导入数据表有的字段 - $tableName = $this->model->getTable(); - $rowTableFieldByComment = $this->getRowTableFieldsByComment($tableName); - $list = []; - foreach ($dataArray as $key => $value) { - foreach ($rowTableFieldByComment as $k => $v) { - if (isset($value[$v])) { - $list[$key][$k] = $value[$v]; - } - } - $list[$key] = array_merge($list[$key], $data); - } - return $this->success($list); - } - - /** - * @OA\Post( - * path="/api/admin/{table_name}/import", - * tags={"通用接口"}, - * summary="导入", - * description="", - * @OA\Parameter(name="data", 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 import() - { - $all = \request()->all(); - $messages = [ - 'data.required' => '数据必填', - ]; - $validator = Validator::make($all, [ - 'data' => 'required', - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $records = $all['data']; - DB::beginTransaction(); - try { - // 获取数据表的所有字段 - $tableName = $this->model->getTable(); - $existingColumns = $this->getRowTableFieldsByComment($tableName); - // 过滤掉不存在的字段 - $filteredRecords = array_map(function ($record) use ($existingColumns) { - return array_intersect_key($record, $existingColumns); - }, $records); - // 去除空数据 - $filteredRecords = array_filter($filteredRecords); - // 分段导入 - foreach (array_chunk($filteredRecords, 500) as $chunk) { - $this->model->insert($chunk); - } - DB::commit(); - return $this->success(['total' => count($records), 'filter_total' => count($filteredRecords)]); - } catch (\Exception $exception) { - DB::rollBack(); - return $this->fail([$exception->getCode(), $exception->getMessage()]); - } - } - - - /** - * 获取原始数据表的表结构和备注 - */ - public function getRowTableFieldsByComment($tableName, $except = [], $symbol = '-') - { - $columns = DB::select(DB::raw("SHOW FULL COLUMNS FROM {$tableName}")); - // 转数组 - $columns = json_decode(json_encode($columns), true); - $array = array_column($columns, 'Comment', 'Field'); - $list = []; - foreach ($array as $key => $item) { - // 去掉排除的数据 - if (in_array($key, $except)) { - continue; - } - // 根据分隔符取前面的字段名 - $item = explode($symbol, $item); - $list[$key] = $item[0]; - } - return $list; - } - -} diff --git a/app/Http/Controllers/Admin/BaseFormController.php b/app/Http/Controllers/Admin/BaseFormController.php deleted file mode 100644 index 0e786c4..0000000 --- a/app/Http/Controllers/Admin/BaseFormController.php +++ /dev/null @@ -1,299 +0,0 @@ -'名字','user.sex'=>'性别']"), - * @OA\Parameter(name="export_name", in="query", @OA\Schema(type="string"), required=false, description="导出的文件名"), - * @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="filter", 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="is_auth", in="query", @OA\Schema(type="string"), required=false, description="是否鉴权 0否1是"), - * @OA\Parameter(name="show_relation", 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(BaseFormRepository $repository) - { - $all = \request()->all(); - $list = $repository->buildWith($all['show_relation'] ?? [])->buildSeacher($all['filter'] ?? []) - ->where(function ($query) use ($all) { - // 权限 - if (isset($all['is_auth']) && !empty($all['is_auth'])) { - $user = $this->getUser(); - $departmentIds = Admin::roleAllowAdminIds($user); - $query->where(function ($qry) use ($departmentIds, $user) { - $qry->whereIn('department_id', $departmentIds)->orWhere('admin_id', $user->id); - }); - } - })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc'); - if (isset($all['is_export']) && !empty($all['is_export'])) { - $list = $list->limit(5000)->get()->toArray(); - return Excel::download(new BaseFormExport($list, $all['export_fields'] ?? ''), $all['file_name'] ?? '' . date('YmdHis') . '.xlsx'); - } else { - // 输出 - $list = $list->paginate($all['page_size'] ?? 20); - // json数组转换 - foreach ($list as $item) { - $repository->getJsonData($item); - } - } - return $this->success($list); - } - - - /** - * @OA\Get( - * path="/api/admin/base-form/show", - * tags={"自定义表单增删改查操作"}, - * summary="详情", - * description="", - * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"), - * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), - * @OA\Parameter(name="json_data_fields", 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 show(BaseFormRepository $repository) - { - $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 = $repository->buildWith()->find($all['id']); - // 获取所有一对多json附加数据 - $detail = $repository->getJsonData($detail); - return $this->success($detail); - } - - /** - * @OA\Post( - * path="/api/admin/base-form/save", - * tags={"自定义表单增删改查操作"}, - * summary="更新", - * description="", - * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"), - * @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"), - * @OA\Parameter(name="字段名_relation", 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 save(BaseFormRequest $request, BaseFormRepository $repository) - { - $all = $request->all(); - DB::beginTransaction(); - try { - if (isset($all['id'])) { - $model = BaseForm::find($all['id']); - } else { - $model = new BaseForm(); - $all['admin_id'] = $this->getUserId(); - $all['department_id'] = $this->getUser()->department_id; - } - // 保存前额外前置操作 - $allAfter = BaseFormExtendRepository::saveBefore($all); - $model->fill($allAfter); - $model->save(); - DB::commit(); - $model = $model->find($model->id); - // 更新模型关系 - $repository->updateRelation($all, $model); - // 保存后额外后置操作 - BaseFormExtendRepository::saveAfter($all, $model); - // 添加搜索 - SearchResult::addResources($model); - return $this->success($model); - } catch (\Exception $exception) { - DB::rollBack(); - return $this->fail([$exception->getCode(), $exception->getMessage()]); - } - } - - /** - * @OA\Get( - * path="/api/admin/base-form/destroy", - * tags={"自定义表单增删改查操作"}, - * summary="删除", - * description="", - * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"), - * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), - * @OA\Parameter(name="destroy_relation", 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 destroy(BaseFormRepository $repository) - { - $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())]); - } - $form = $repository->find($all['id']); - if (isset($all['destroy_relation']) && !empty($all['destroy_relation'])) { - // 删除关联关系 - foreach ($all['destroy_relation'] as $item) { - $form->$item()->delete(); - } - } - $form->delete(); - return $this->success('删除成功'); - } - - /** - * @OA\Post( - * path="/api/admin/base-form/excel-show", - * tags={"自定义表单增删改查操作"}, - * summary="导入预览", - * description="", - * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"), - * @OA\Parameter(name="file", 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 excelShow(Request $request) - { - $all = \request()->all(); - $messages = [ - 'table_name' => '表名必填', - 'file.required' => 'file必填', - 'file.file' => 'file必须是文件', - 'file.max' => '文件大小不能超过50MB', - 'file.mimes' => '仅允许上传以下格式的文件: zip, rar, ppt, pptx, xls, xlsx, doc, docx, png, gif, jpg, jpeg, pdf, mp4, mp3' - ]; - $validator = Validator::make($all, [ - 'table_name' => 'required', - 'file' => 'required|file|mimes:zip,rar,ppt,pptx,xls,xlsx,doc,docx,png,gif,jpg,jpeg,pdf,mp4,mp3|max:51200000', - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $file = $request->file('file'); - $tempFile = $file->getRealPath(); - $dataArray = (new FastExcel)->import($tempFile)->toArray(); - // 数据过滤,只能导入数据表有的字段 - $customFormField = new CustomFormField(); - $rowTableFieldByComment = $customFormField->getRowTableFieldsByComment($all['table_name']); - $list = []; - foreach ($dataArray as $key => $value) { - foreach ($rowTableFieldByComment as $k => $v) { - if (isset($value[$v])) { - $list[$key][$k] = $value[$v]; - } - } - } - return $this->success($list); - } - - /** - * @OA\Post( - * path="/api/admin/base-form/import", - * tags={"自定义表单增删改查操作"}, - * summary="导入", - * description="", - * @OA\Parameter(name="table_name", in="query", @OA\Schema(type="string"), required=false, description="表名"), - * @OA\Parameter(name="data", 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 import(BaseFormRepository $repository) - { - $all = \request()->all(); - $messages = [ - 'data.required' => '数据必填', - ]; - $validator = Validator::make($all, [ - 'data' => 'required', - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $baseForm = new BaseForm(); - DB::beginTransaction(); - try { - $list = []; - foreach ($all['data'] as $item) { - $list[] = array_merge($item, [ - 'admin_id' => $this->getUserId(), - 'department_id' => $this->getUser()->department_id, - 'created_at' => date('Y-m-d H:i:s') - ]); - } - // 分段导入 - foreach (array_chunk($list, 500) as $chunk) { - $baseForm->insert($chunk); - } - DB::commit(); - return $this->success(['total' => count($all['data'])]); - } catch (\Exception $exception) { - DB::rollBack(); - return $this->fail([$exception->getCode(), $exception->getMessage()]); - } - } - -} diff --git a/app/Http/Controllers/Admin/CustomFormController.php b/app/Http/Controllers/Admin/CustomFormController.php deleted file mode 100644 index a2e124e..0000000 --- a/app/Http/Controllers/Admin/CustomFormController.php +++ /dev/null @@ -1,362 +0,0 @@ -all(); - $list = CustomForm::where(function ($query) use ($all) { - })->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/real-table-index", - * tags={"自定义表单"}, - * summary="获取实体数据表", - * description="", - * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), - * @OA\Response( - * response="200", - * description="暂无" - * ) - * ) - */ - public function realTableindex() - { - $all = request()->all(); - $raws = Schema::getAllTables(); - $list = []; - foreach ($raws as $item) { - foreach ($item as $k => $v) { - if ($k != 'Table_type') $list[] = $v; - } - } - return $this->success($list); - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/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 = CustomForm::with('relation', 'fields')->find($all['id']); - return $this->success($detail); - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/real-table-show", - * tags={"自定义表单"}, - * summary="获取实体表字段详情", - * description="", - * @OA\Parameter(name="table_name", 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 realTableShow() - { - $all = \request()->all(); - $messages = [ - 'table_name.required' => 'table_name必填', - ]; - $validator = Validator::make($all, [ - 'table_name' => 'required' - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $detail = (new CustomFormField())->getRowTableFieldsByType($all['table_name'], true); - return $this->success($detail); - } - - /** - * @OA\Post( - * path="/api/admin/custom-form/save", - * tags={"自定义表单"}, - * summary="更新", - * description="", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="int"), required=true, description="Id(存在更新,不存在新增)"), - * @OA\Parameter(name="table_name", 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="relation", in="query", @OA\Schema(type="string"), required=false, description="关联关系二维数组,包含字段:id存在更新,不存在新增,custom_form_field,link_table_name关联表名,link_relation关联关系,local_key本地字段,foreign_key关联表字段"), - * @OA\Parameter(name="fields", in="query", @OA\Schema(type="string"), required=false, description="字段详情二维数组,包含字段:id存在更新,不存在新增。和其他字段详见字段更新接口"), - * @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 = [ - 'table_name.required' => '表名必填' - ]; - $validator = Validator::make($all, [ - 'table_name' => 'required' - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - DB::beginTransaction(); - try { - if (isset($all['id'])) { - $model = CustomForm::find($all['id']); - // 判断不能重名 - $check = $model->where('table_name', $all['table_name'])->where('id', '!=', $all['id'])->first(); - if ($check) return $this->fail([ResponseCode::ERROR_BUSINESS, '表名重复']); - } else { - $model = new CustomForm(); - $all['admin_id'] = $this->getUserId(); - $all['department_id'] = $this->getUser()->department_id; - // 判断不能重名 - $check = $model->hasTable($all['table_name']); - if ($check) return $this->fail([ResponseCode::ERROR_BUSINESS, '表名重复']); - } - // 表名必须是复数 - $plural = Str::plural($all['table_name']); - if ($plural != $all['table_name']) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '请用复数形式命名:' . $plural]); - } - $model->fill($all); - $model->save(); - DB::commit(); - // 关联关系 - if (isset($all['relation']) && !empty($all['relation'])) { - foreach ($all['relation'] as $detail) { - $detail['link_with_name'] = getFieldWithName($detail); - if (isset($detail['id'])) { - // 更新 - (CustomFormRelation::find($detail['id']))->fill($detail)->save(); - } else { - // 新增 - $model->relation()->create($detail); - } - } - } - if (isset($all['fields']) && !empty($all['fields'])) { - foreach ($all['fields'] as $detail) { - if (isset($detail['id'])) { - // 更新 - (CustomFormField::find($detail['id']))->fill($detail)->save(); - } else { - // 新增 - $model->fields()->create($detail); - } - } - } - return $this->success('更新成功'); - } catch (\Exception $exception) { - DB::rollBack(); - return $this->fail([$exception->getCode(), $exception->getMessage()]); - } - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/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())]); - } - $customForm = CustomForm::find($all['id']); - CustomForm::where('id', $all['id'])->delete(); - CustomFormField::where('custom_form_id', $all['id'])->delete(); - CustomFormRelation::where('custom_form_id', $all['id'])->delete(); - Schema::drop($customForm->table_name); - return $this->success('删除成功'); - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/relation-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 relationDestroy() - { - $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())]); - } - CustomFormRelation::where('id', $all['id'])->delete(); - return $this->success('删除成功'); - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/update-table", - * 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 updateTable() - { - $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())]); - } - $result = (new CustomForm())->updateTable($all['id']); - return $this->success($result); - } - - /** - * @OA\Get( - * path="/api/admin/custom-form/clone-table", - * tags={"自定义表单"}, - * summary="克隆表结构", - * description="", - * @OA\Parameter(name="table_name", 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 cloneTable() - { - $all = \request()->all(); - $messages = [ - 'table_name.required' => '表名必填', - ]; - $validator = Validator::make($all, [ - 'table_name' => 'required' - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $customForm = CustomForm::where('table_name', $all['table_name'])->first(); - if (empty($customForm)) { - return $this->fail([ResponseCode::ERROR_BUSINESS, '表名不存在']); - } - // 执行克隆表 - $model = CustomForm::create([ - 'name' => $customForm->name . '_clone', - 'table_name' => $customForm->table_name . '_clone', - 'admin_id' => $this->getUserId(), - 'department_id' => $this->getUser()->department_id, - ]); - // 克隆字段 - $customFormField = CustomFormField::where('custom_form_id', $customForm->id)->get()->toArray(); - foreach ($customFormField as $item) { - $item['custom_form_id'] = $model->id; - CustomFormField::create($item); - } - // 克隆关联关系 - $customFormRelation = CustomFormRelation::where('custom_form_id', $customForm->id)->get()->toArray(); - foreach ($customFormRelation as $value) { - $value['custom_form_id'] = $model->id; - CustomFormRelation::create($value); - } - return $this->success('克隆成功'); - } - -} diff --git a/app/Http/Controllers/Admin/CustomFormFieldController.php b/app/Http/Controllers/Admin/CustomFormFieldController.php deleted file mode 100644 index 6b46009..0000000 --- a/app/Http/Controllers/Admin/CustomFormFieldController.php +++ /dev/null @@ -1,227 +0,0 @@ -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="link_table_name", 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="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 config() - { - // 验证规则选项 - $validationRules = CustomFormField::$validationRules; - // 编辑类型选项 - $editToMigration = EditToMigration::get(); - return $this->success(['validation_rules' => $validationRules, 'edit_to_migration' => $editToMigration]); - } - -} diff --git a/app/Http/Controllers/Admin/GateController.php b/app/Http/Controllers/Admin/GateController.php new file mode 100644 index 0000000..85ca655 --- /dev/null +++ b/app/Http/Controllers/Admin/GateController.php @@ -0,0 +1,146 @@ +where('name', 'like', '%门岗%'); + })->get(); + return $this->success($admin); + } + + /** + * @OA\Get( + * path="/api/admin/gate/visit-list", + * tags={"门岗-拜访记录"}, + * summary="拜访记录", + * description="", + * @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词"), + * @OA\Parameter(name="audit_status", in="query", @OA\Schema(type="string"), required=false, description="审核状态-1待学习0待审核1通过(待进厂)2驳回3已进厂4已离厂"), + * @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="开始日期"), + * @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string"), required=false, description="结束日期"), + * @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="code", in="query", @OA\Schema(type="string"), required=false, description="编码"), + * @OA\Parameter(name="idcard", in="query", @OA\Schema(type="string"), required=false, description="身份证号码"), + * @OA\Parameter(name="car_no", in="query", @OA\Schema(type="string"), required=false, description="停车牌"), + * @OA\Parameter(name="person_no", in="query", @OA\Schema(type="string"), required=false, description="人牌"), + * @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是,默认0"), + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function visitList() + { + $all = request()->all(); + $list = Visit::with('logs.admin', 'logs.user', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department')->where(function ($query) use ($all) { + if (isset($all['keyword'])) { + $query->where('name', 'like', '%' . $all['keyword'] . '%'); + } + if (isset($all['audit_status'])) { + $query->where('audit_status', $all['audit_status']); + } + if (isset($all['code'])) { + $query->where('code', $all['code']); + } + if (isset($all['idcard'])) { + $query->where('idcard', $all['idcard']); + } + if (isset($all['start_date']) && isset($all['end_date'])) { + $query->whereBetween('date', [$all['start_date'], $all['end_date']]); + } + })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')->paginate($all['page_size'] ?? 20);; + return $this->success($list); + } + + /** + * @OA\Get( + * path="/api/admin/gate/use-code", + * tags={"门岗-核销"}, + * summary="核销", + * description="", + * @OA\Parameter(name="admin_id", in="query", @OA\Schema(type="string"), required=false, description="管理员id"), + * @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=false, description="编码"), + * @OA\Parameter(name="car_no", in="query", @OA\Schema(type="string"), required=false, description="停车牌"), + * @OA\Parameter(name="person_no", in="query", @OA\Schema(type="string"), required=false, description="人牌"), + * @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="1进厂2离厂"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function useCode() + { + $all = \request()->all(); + $messages = [ + 'code.required' => '编号必填', + 'type.required' => '类型必填', + 'admin_id.required' => '操作人必填' + ]; + $validator = Validator::make($all, [ + 'code' => 'required', + 'type' => 'required', + 'admin_id' => 'required' + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + $check = Visit::where('code', $all['code'])->first(); + if (empty($check)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '拜访记录不存在']); + } + $remark = '进厂'; + if ($all['type'] == 2) { + $remark = '离厂'; + } + $gateLog = GateLog::add($all['admin_id'], $all['code'], $all['person_no'] ?? [], $all['car_no'] ?? [], $remark); + if ($all['type'] == 1) { + // 入场 + Visit::where('code', $all['code'])->update(['audit_status' => 3, 'person_no' => $all['person_no'] ?? '', 'car_no' => $all['car_no'] ?? '']); + // 通知被访人 + $vars = ['date' => $check->date, 'name' => $check->name, 'phone_number' => $check->mobile]; + $template_id = 'zPtka4'; + $acceptAdmin = Admin::find($check->accept_admin_id); + sms($acceptAdmin->mobile, $vars, $template_id); + } + if ($all['type'] == 2) { + Visit::where('code', $all['code'])->update(['audit_status' => 4]); + } + return $this->success($gateLog); + } +} diff --git a/app/Http/Controllers/Admin/OperateLogController.php b/app/Http/Controllers/Admin/OperateLogController.php new file mode 100644 index 0000000..78920fe --- /dev/null +++ b/app/Http/Controllers/Admin/OperateLogController.php @@ -0,0 +1,48 @@ +all(); + $list = OperateLog::with('admin', 'department')->where(function ($query) use ($all) { + if (isset($all['admin_id'])) { + $query->where('admin_id', $all['admin_id']); + } + if (isset($all['name'])) { + $query->where('name', 'like', '%' . $all['name'] . '%'); + } + })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc') + ->paginate($all['page_size'] ?? 20); + return $this->success($list); + } +} diff --git a/app/Http/Controllers/Admin/ParameterController.php b/app/Http/Controllers/Admin/ParameterController.php index f14bb7a..6906b57 100644 --- a/app/Http/Controllers/Admin/ParameterController.php +++ b/app/Http/Controllers/Admin/ParameterController.php @@ -120,32 +120,35 @@ class ParameterController extends CommonController if (isset($all['id'])) { $detail = Parameter::with(['detail' => function ($query) { $query->orderBy('sort', 'asc'); - }]); - if (is_array($all['id'])) { - $detail = $detail->whereIn('id', $all['id'])->get(); - } else { - $detail = $detail->where('id', $all['id'])->first(); - } + }])->where(function ($query) use ($all) { + if (is_array($all['id'])) { + $query->whereIn('id', $all['id']); + } else { + $query->where('id', $all['id']); + } + })->first(); } if (isset($all['number'])) { $detail = Parameter::with(['detail' => function ($query) { $query->orderBy('sort', 'asc'); - }]); - if (is_array($all['number'])) { - $detail = $detail->whereIn('number', $all['number'])->get(); - } else { - $detail = $detail->where('number', $all['number'])->first(); - } + }])->where(function ($query) use ($all) { + if (is_array($all['number'])) { + $query->whereIn('number', $all['number']); + } else { + $query->where('number', $all['number']); + } + })->first(); } if (isset($all['pid'])) { $detail = Parameter::with(['detail' => function ($query) { $query->orderBy('sort', 'asc'); - }]); - if (is_array($all['pid'])) { - $detail = $detail->whereIn('pid', $all['pid'])->get(); - } else { - $detail = $detail->where('pid', $all['pid'])->first(); - } + }])->where(function ($query) use ($all) { + if (is_array($all['pid'])) { + $query->whereIn('pid', $all['pid']); + } else { + $query->where('pid', $all['pid']); + } + })->first(); } return $this->success($detail); } diff --git a/app/Http/Controllers/Admin/UploadController.php b/app/Http/Controllers/Admin/UploadController.php index 3de6c5a..9548679 100644 --- a/app/Http/Controllers/Admin/UploadController.php +++ b/app/Http/Controllers/Admin/UploadController.php @@ -5,7 +5,6 @@ namespace App\Http\Controllers\Admin; use App\Helpers\ResponseCode; use App\Models\Upload; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Validator; class UploadController extends CommonController { @@ -26,51 +25,36 @@ class UploadController extends CommonController */ public function uploadFile(Request $request) { - $all = \request()->all(); - $messages = [ - 'file.required' => 'file必填', - 'file.file' => 'file必须是文件', - 'file.max' => '文件大小不能超过50MB', - 'file.mimes' => '仅允许上传以下格式的文件: zip, rar, ppt, pptx, xls, xlsx, doc, docx, png, gif, jpg, jpeg, pdf, mp4, mp3' - ]; - $validator = Validator::make($all, [ - 'file' => 'required|file|mimes:zip,rar,ppt,pptx,xls,xlsx,doc,docx,png,gif,jpg,jpeg,pdf,mp4,mp3|max:51200000', - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } $file = $request->file('file'); //获取文件大小,单位B $fileSize = floor($file->getSize()); + if ($fileSize >= 200 * 1024 *1024) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于200M']); + } //过滤文件后缀 $ext = $file->getClientOriginalExtension(); - if ($ext == 'pdf') { - // 防止pdf的xss - $realPath = $file->getRealPath(); - $content = strtolower(file_get_contents($realPath)); - // 判断内容里有script字符串则返回错误,不区分大小写 - if (stripos($content, 'javascript') !== false) { - return $this->fail([ResponseCode::ERROR_PARAMETER, '非法文件']); - } + if (in_array($ext, ['zip','rar','ppt','pptx','xls','xlsx','doc','docx','png','gif','jpg','jpeg','pdf','mp4'])) { + // 保存目录 + $dir = 'files'; + // 文件名 + $fileName = time() . uniqid() . '.' . $ext; + $file->storeAs($dir,$fileName,['disk'=>'public']); + // 写入上传文件记录表 + $list = [ + 'original_name'=>$file->getClientOriginalName(), + 'folder'=>'storage/'.$dir, + 'name'=>$fileName, + 'extension'=>$ext, + 'size'=>$fileSize, + 'creator_id'=>$this->getUserId(), + 'created_at'=>date('Y-m-d H:i:s') + ]; + $id = Upload::insertGetId($list); + $uploadFile = Upload::find($id); + return $this->success($uploadFile); + } else { + return $this->fail([ResponseCode::ERROR_BUSINESS, '文件格式错误']); } - // 保存目录 - $dir = 'files'; - // 文件名 - $fileName = time() . uniqid() . '.' . $ext; - $file->storeAs($dir, $fileName, ['disk' => 'public']); - // 写入上传文件记录表 - $list = [ - 'original_name' => $file->getClientOriginalName(), - 'folder' => 'storage/' . $dir, - 'name' => $fileName, - 'extension' => $ext, - 'size' => $fileSize, - 'creator_id' => $this->getUserId(), - 'created_at' => date('Y-m-d H:i:s') - ]; - $id = Upload::insertGetId($list); - $uploadFile = Upload::find($id); - return $this->success($uploadFile); } } diff --git a/app/Http/Controllers/GateController.php b/app/Http/Controllers/GateController.php deleted file mode 100644 index fa33fb1..0000000 --- a/app/Http/Controllers/GateController.php +++ /dev/null @@ -1,399 +0,0 @@ -where('name', 'like', '%门岗%'); - })->get(); - return $this->success($admin); - } - - /** - * @OA\Get( - * path="/api/gate/visits", - * tags={"门岗端接口"}, - * summary="拜访信息列表", - * description="获取门岗端拜访信息列表", - * @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=false, description="查询日期,格式:Y-m-d,开始日期"), - * @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string"), required=false, description="查询日期,格式:Y-m-d,结束日期"), - * @OA\Parameter(name="status", in="query", @OA\Schema(type="string"), required=false, description="状态筛选:1通过(待进厂)3已进厂4已离厂"), - * @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="类型筛选:1访客2访客车辆3物流车辆"), - * @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词搜索(人牌,身份证,核销码)"), - * @OA\Parameter(name="page", in="query", @OA\Schema(type="integer"), required=false, description="页码,默认1"), - * @OA\Parameter(name="page_size", in="query", @OA\Schema(type="integer"), required=false, description="每页数量,默认20"), - * @OA\Response( - * response="200", - * description="成功获取拜访信息列表", - * @OA\JsonContent( - * @OA\Property(property="code", type="integer", example=200), - * @OA\Property(property="message", type="string", example="success"), - * @OA\Property(property="data", type="object", - * @OA\Property(property="current_page", type="integer"), - * @OA\Property(property="data", type="array", @OA\Items(type="object")), - * @OA\Property(property="total", type="integer") - * ) - * ) - * ) - * ) - */ - public function visits(Request $request) - { - try { - $startDate = $request->input('start_date', date('Y-m-d')); - $endDate = $request->input('end_date', date('Y-m-d')); - - $status = $request->input('status'); - $type = $request->input('type'); - $keyword = $request->input('keyword'); - $page = $request->input('page', 1); - $pageSize = $request->input('page_size', 20); - - $query = Visit::with([ - 'visitArea', - 'visitTime', - 'acceptDepartment', - 'acceptAdmin', - 'visitAudits', - 'acceptAdminSign' - ])->whereBetween('date', [$startDate, $endDate]); - - // 状态筛选 - if ($status) { - $query->where('audit_status', $status); - } else { - // 默认显示待进厂和已进厂的记录 - $query->whereIn('audit_status', [Visit::AUDIT_STATUS_APPROVED, Visit::AUDIT_STATUS_ENTERED, Visit::AUDIT_STATUS_LEFT]); - } - - // 类型筛选 - if ($type) { - $query->where('type', $type); - } - - // 关键词搜索 - if ($keyword) { - $query->where(function ($q) use ($keyword) { - $q->where('person_no', "{$keyword}") - ->orWhere('idcard', "{$keyword}") - ->orWhere('code', "{$keyword}"); - }); - } - - $visits = $query->orderBy('created_at', 'desc') - ->paginate($pageSize, ['*'], 'page', $page); - - return $this->success($visits); - } catch (\Exception $e) { - Log::error('门岗端获取拜访列表失败:' . $e->getMessage()); - return $this->fail([ResponseCode::ERROR_INSIDE, __('gate.get_visit_list_failed')]); - } - } - - /** - * @OA\Post( - * path="/api/admin/gate/visits/detail", - * tags={"门岗端接口"}, - * summary="拜访详情信息", - * description="获取指定拜访的详细信息", - * @OA\RequestBody( - * required=true, - * @OA\JsonContent( - * @OA\Property(property="id", type="integer", description="拜访ID") - * ) - * ), - * @OA\Response( - * response="200", - * description="成功获取拜访详情", - * @OA\JsonContent( - * @OA\Property(property="code", type="integer", example=200), - * @OA\Property(property="message", type="string", example="success"), - * @OA\Property(property="data", type="object") - * ) - * ) - * ) - */ - public function visitDetail(Request $request) - { - try { - $id = $request->input('id'); - - if (!$id) { - return $this->fail([ResponseCode::ERROR_PARAMETER, __('gate.visit_id_required')]); - } - - $visit = Visit::with([ - 'visitArea', - 'visitTime', - 'acceptDepartment', - 'acceptAdmin', - 'acceptGoodsAdmin', - 'accompany', - 'visitAudits', - 'logs', - 'acceptAdminSign' - ])->find($id); - - if (!$visit) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.visit_not_found')]); - } - - return $this->success($visit); - } catch (\Exception $e) { - Log::error('门岗端获取拜访详情失败:' . $e->getMessage()); - return $this->fail([ResponseCode::ERROR_INSIDE, __('gate.get_visit_detail_failed')]); - } - } - - /** - * @OA\Post( - * path="/api/admin/gate/visits/update", - * tags={"门岗端接口"}, - * summary="更新拜访信息", - * description="门岗端更新拜访信息(按字段进行更新:绑定ID卡、修改进/离厂状态、上传货车图片等)", - * @OA\RequestBody( - * required=true, - * @OA\JsonContent( - * @OA\Property(property="id", type="integer", description="拜访ID"), - * @OA\Property(property="person_no", type="string", description="人牌号(提供则更新并记录绑定日志)"), - * @OA\Property(property="car_no", type="string", description="停车牌号(提供则更新)"), - * @OA\Property(property="audit_status", type="integer", enum={3,4}, description="审核状态:3进厂(需当前为已通过),4离厂(需当前为已进厂)"), - * @OA\Property(property="vehicle_images", type="array", description="货车图片ID数组(仅物流车辆可提供,需为有效上传ID)", @OA\Items(type="integer")), - * @OA\Property(property="remark", type="string", description="备注信息(用于进/离厂日志可选附加)") - * ) - * ), - * @OA\Response( - * response="200", - * description="更新成功", - * @OA\JsonContent( - * @OA\Property(property="code", type="integer", example=200), - * @OA\Property(property="message", type="string", example="操作成功"), - * @OA\Property(property="data", type="object") - * ) - * ) - * ) - */ - public function updateVisit(Request $request) - { - try { - $id = $request->input('id'); - - if (!$id) { - return $this->fail([ResponseCode::ERROR_PARAMETER, __('gate.visit_id_required')]); - } - - $visit = Visit::find($id); - if (!$visit) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.visit_not_found')]); - } - - $remark = $request->input('remark', ''); - $personNo = $request->input('person_no'); - $carNo = $request->input('car_no'); - $vehicleImages = $request->input('vehicle_images'); // null 表示未尝试更新 - $newAuditStatus = $request->input('audit_status'); // 可选:3进厂,4离厂 - - DB::beginTransaction(); - - $hasAnyChange = false; - - // 绑定/更新人牌、车牌 - if (!is_null($personNo) || !is_null($carNo)) { - if (!is_null($personNo)) { - $visit->person_no = $personNo; - } - if (!is_null($carNo)) { - $visit->car_no = $carNo; - } - $visit->save(); - $hasAnyChange = true; - - $this->createVisitLog( - $visit, - VisitLog::TYPE_ENTER, - __('gate.bind_card_log', [ - 'person_no' => $visit->person_no ?: '', - 'car_no' => $visit->car_no ?: '' - ]) - ); - } - - // 审核状态变更:支持进厂(Approved -> Entered) 与 离厂(Entered -> Left) - if (!is_null($newAuditStatus)) { - if ((int) $newAuditStatus === Visit::AUDIT_STATUS_ENTERED) { - if ($visit->audit_status != Visit::AUDIT_STATUS_APPROVED) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.only_approved_visits_can_enter')]); - } - $visit->audit_status = Visit::AUDIT_STATUS_ENTERED; - $visit->save(); - $hasAnyChange = true; - - $this->createVisitLog($visit, VisitLog::TYPE_ENTER, __('gate.enter_log') . ($remark ? ':' . $remark : '')); - $this->createGateLog($visit, 'enter', $remark); - } elseif ((int) $newAuditStatus === Visit::AUDIT_STATUS_LEFT) { - if ($visit->audit_status != Visit::AUDIT_STATUS_ENTERED) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.only_entered_visitors_can_leave')]); - } - $visit->audit_status = Visit::AUDIT_STATUS_LEFT; - $visit->save(); - $hasAnyChange = true; - - $this->createVisitLog($visit, VisitLog::TYPE_LEAVE, __('gate.leave_log') . ($remark ? ':' . $remark : '')); - $this->createGateLog($visit, 'leave', $remark); - } - } - - // 货车图片更新(仅物流车辆) - if (!is_null($vehicleImages)) { - if ($visit->type != Visit::TYPE_LOGISTICS_CAR) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.only_logistics_cars_can_upload_images')]); - } - - if (!is_array($vehicleImages) || empty($vehicleImages)) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.vehicle_images_required')]); - } - - $visit->vehicle_images = $vehicleImages; - $visit->save(); - $hasAnyChange = true; - - $this->createVisitLog($visit, VisitLog::TYPE_ENTER, __('gate.upload_vehicle_images_log', ['count' => count($vehicleImages)])); - } - - if (!$hasAnyChange) { - DB::rollBack(); - return $this->fail([ResponseCode::ERROR_PARAMETER, __('gate.invalid_action_type')]); - } - - DB::commit(); - return $this->success($visit); - } catch (\Exception $e) { - DB::rollBack(); - Log::error('门岗端更新拜访信息失败:' . $e->getMessage()); - return $this->fail([ResponseCode::ERROR_INSIDE, __('gate.operation_failed') . ':' . $e->getMessage()]); - } - } - - - /** - * 创建拜访日志 - */ - private function createVisitLog($visit, $type, $remark = '') - { - VisitLog::log($visit->id, $type, $remark); - } - - /** - * 创建门岗日志 - */ - private function createGateLog($visit, $action, $remark = '') - { - $personNo = $visit->person_no ? [$visit->person_no] : []; - $carNo = $visit->car_no ? [$visit->car_no] : []; - - GateLog::create([ - 'code' => $visit->code, - 'remark' => $remark, - 'person_no' => $personNo, - 'car_no' => $carNo, - 'created_at' => now(), - 'updated_at' => now(), - ]); - } - - /** - * @OA\Get( - * path="/api/admin/gate/visits/use-code", - * tags={"门岗端接口"}, - * summary="核销", - * description="", - * @OA\Parameter(name="admin_id", in="query", @OA\Schema(type="string"), required=false, description="管理员id"), - * @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=false, description="关键词(人牌,身份证,核销码)"), - * @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="1进厂2离厂"), - * @OA\Response( - * response="200", - * description="暂无" - * ) - * ) - */ - public function useCode() - { - $all = \request()->all(); - $messages = [ - 'keyword.required' => __('gate.validation.keyword_required'), - 'type.required' => __('gate.validation.type_required'), - 'admin_id.required' => __('gate.validation.admin_id_required') - ]; - $validator = Validator::make($all, [ - 'keyword' => 'required', - 'type' => 'required', - 'admin_id' => 'required' - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $code = \request('code'); - $idcard = \request('idcard'); - if (empty($idcard) && empty($code)) { - return $this->fail([ResponseCode::ERROR_PARAMETER, __('gate.validation.code_or_idcard_required')]); - } - $check = Visit::where(function ($query) use ($all) { - $query->where('person_no', "{$all['keyword']}") - ->orWhere('idcard', "{$all['keyword']}") - ->orWhere('code', "{$all['keyword']}"); - })->first(); - if (empty($check)) { - return $this->fail([ResponseCode::ERROR_BUSINESS, __('gate.business.visit_not_found')]); - } - $remark = __('gate.operation.enter_factory'); - if ($all['type'] == 2) { - $remark = __('gate.operation.leave_factory'); - } - $gateLog = GateLog::add($all['admin_id'], $all['code'], $all['person_no'] ?? [], $all['car_no'] ?? [], $remark); - if ($all['type'] == 1) { - // 入场 - Visit::where('code', $all['code'])->update(['audit_status' => 3, 'person_no' => $all['person_no'] ?? '', 'car_no' => $all['car_no'] ?? '']); - } - if ($all['type'] == 2) { - Visit::where('code', $all['code'])->update(['audit_status' => 4]); - } - return $this->success($gateLog); - } - -} diff --git a/routes/api.php b/routes/api.php index f0c00dd..e24b59b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -19,22 +19,14 @@ Route::middleware('auth:sanctum')->get('/user', function (Request $request) { }); -Route::any('test', [\App\Http\Controllers\Admin\OtherController::class, "test"]); - - - -// 后台管理接口路由组(支持多语言) -Route::group(['prefix' => 'admin', 'middleware' => ['sanctum.jwt:admin', 'set.locale']], function () { - // 门岗端接口路由组 - Route::prefix('gate')->group(function () { - Route::get("user-list", [\App\Http\Controllers\GateController::class, "uerList"]); - Route::get('visits', [\App\Http\Controllers\GateController::class, 'visits']); - Route::post('visits/detail', [\App\Http\Controllers\GateController::class, 'visitDetail']); - Route::get("visits/use-code", [\App\Http\Controllers\GateController::class, "useCode"]); - - Route::post('visits/update', [\App\Http\Controllers\GateController::class, 'updateVisit']); - }); - +// 门岗管理 +Route::get("admin/gate/user-list", [\App\Http\Controllers\Admin\GateController::class, "uerList"]); +Route::get("admin/gate/visit-list", [\App\Http\Controllers\Admin\GateController::class, "visitList"]); +Route::get("admin/gate/use-code", [\App\Http\Controllers\Admin\GateController::class, "useCode"]); +Route::post("admin/import", [\App\Http\Controllers\Admin\AdminController::class, "import"]); + +// 后台 +Route::group(["namespace" => "Admin", "prefix" => "admin", "middleware" => "sanctum.jwt:admin,rbac"], function () { // 统计 Route::get("chart/month", [\App\Http\Controllers\Admin\ChartController::class, "month"]); Route::get("chart/time", [\App\Http\Controllers\Admin\ChartController::class, "time"]); @@ -92,7 +84,6 @@ Route::group(['prefix' => 'admin', 'middleware' => ['sanctum.jwt:admin', 'set.lo Route::get("blacklist/destroy", [\App\Http\Controllers\Admin\BlacklistController::class, "destroy"]); }); - // 前台 Route::get('mobile/user/login', [\App\Http\Controllers\Mobile\UserController::class, 'login']); Route::get('mobile/user/config', [\App\Http\Controllers\Mobile\UserController::class, 'config']);