request->params([ ['contract_name', ''], ['contract_type', 'general'] ]); // 获取上传文件 $file = $this->request->file('file'); if (!$file) { return fail('请选择要上传的文件'); } $data['file'] = $file; $result = (new DocumentTemplateServiceBasic())->uploadTemplate($data); return success('上传成功', $result); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 解析占位符 * @return Response */ public function parse(): Response { try { $filePath = $this->request->param('file_path', ''); if (empty($filePath)) { return fail('文件路径不能为空'); } $absolutePath = public_path() . '/' . $filePath; if (!file_exists($absolutePath)) { return fail('文件不存在'); } $placeholders = (new DocumentTemplateServiceBasic())->parsePlaceholders($absolutePath); return success('解析成功', ['placeholders' => $placeholders]); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 配置数据源 * @return Response */ public function configDataSource(): Response { try { $data = $this->request->params([ ['contract_id', 0], ['config', []] ]); if (empty($data['contract_id'])) { return fail('合同ID不能为空'); } if (empty($data['config'])) { return fail('配置数据不能为空'); } $result = (new DocumentTemplateServiceBasic())->configDataSource( $data['contract_id'], $data['config'] ); return success('配置成功'); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 获取模板列表 * @return Response */ public function lists(): Response { try { $data = $this->request->params([ ['page', 1], ['limit', 20], ['name', ''], ['type', ''] ]); $where = []; if (!empty($data['name'])) { $where[] = ['name', 'like', '%' . $data['name'] . '%']; } if (!empty($data['type'])) { $where[] = ['type', '=', $data['type']]; } $list = \app\model\contract\Contract::where($where) ->field('id, name, type, file_path, status, created_at') ->order('created_at desc') ->paginate([ 'list_rows' => $data['limit'], 'page' => $data['page'] ]); return success('获取成功', $list); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 获取模板详情 * @param int $id * @return Response */ public function info(int $id): Response { try { $contract = \app\model\contract\Contract::find($id); if (!$contract) { return fail('模板不存在'); } // 获取数据源配置 $configs = \app\model\document\DocumentDataSourceConfig::where('contract_id', $id) ->field('id, placeholder, table_name, field_name, field_type, is_required, default_value') ->select() ->toArray(); $result = $contract->toArray(); $result['configs'] = $configs; return success('获取成功', $result); } catch (\Exception $e) { return fail($e->getMessage()); } } /** * 删除模板 * @param int $id * @return Response */ public function del(int $id): Response { try { $contract = \app\model\contract\Contract::find($id); if (!$contract) { return fail('模板不存在'); } // 删除文件 if (!empty($contract['file_path'])) { $filePath = public_path() . '/' . $contract['file_path']; if (file_exists($filePath)) { unlink($filePath); } } // 删除数据源配置 \app\model\document\DocumentDataSourceConfig::where('contract_id', $id)->delete(); // 删除合同记录 $contract->delete(); return success('删除成功'); } catch (\Exception $e) { return fail($e->getMessage()); } } }