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.
348 lines
9.2 KiB
348 lines
9.2 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\adminapi\controller\document;
|
|
|
|
use core\base\BaseAdminController;
|
|
use app\service\admin\document\DocumentTemplateService;
|
|
use think\facade\Request;
|
|
|
|
/**
|
|
* Word文档模板解析控制器
|
|
* Class DocumentTemplate
|
|
* @package app\adminapi\controller\document
|
|
*/
|
|
class DocumentTemplate extends BaseAdminController
|
|
{
|
|
/**
|
|
* 获取模板列表
|
|
* @return \think\Response
|
|
*/
|
|
public function lists()
|
|
{
|
|
$data = $this->request->params([
|
|
["contract_status", ""],
|
|
["contract_type", ""],
|
|
["contract_name", ""],
|
|
["created_at", ["", ""]]
|
|
]);
|
|
return success((new DocumentTemplateService())->getPage($data));
|
|
}
|
|
|
|
/**
|
|
* 模板详情
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function info(int $id)
|
|
{
|
|
return success((new DocumentTemplateService())->getInfo($id));
|
|
}
|
|
|
|
/**
|
|
* 上传Word模板文件
|
|
* @return \think\Response
|
|
*/
|
|
public function uploadTemplate()
|
|
{
|
|
try {
|
|
// 获取上传的文件
|
|
$file = Request::file('file');
|
|
if (!$file) {
|
|
return fail('FILE_UPLOAD_REQUIRED');
|
|
}
|
|
|
|
// 获取其他表单数据
|
|
$data = $this->request->params([
|
|
['contract_name', ''],
|
|
['contract_type', ''],
|
|
['remarks', '']
|
|
]);
|
|
|
|
$result = (new DocumentTemplateService())->uploadTemplate($file, $data);
|
|
return success('UPLOAD_SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解析Word模板占位符
|
|
* @return \think\Response
|
|
*/
|
|
public function parsePlaceholder()
|
|
{
|
|
$data = $this->request->params([
|
|
["template_path", ""],
|
|
["template_id", 0]
|
|
]);
|
|
|
|
try {
|
|
$result = (new DocumentTemplateService())->parsePlaceholder($data);
|
|
return success('PARSE_SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存占位符配置
|
|
* @return \think\Response
|
|
*/
|
|
public function savePlaceholderConfig()
|
|
{
|
|
$data = $this->request->params([
|
|
["template_id", 0],
|
|
["configs", []]
|
|
]);
|
|
|
|
if (empty($data['template_id'])) {
|
|
return fail('模板ID不能为空');
|
|
}
|
|
|
|
if (empty($data['configs']) || !is_array($data['configs'])) {
|
|
return fail('配置数据不能为空');
|
|
}
|
|
|
|
try {
|
|
// 保存配置(现在会同时保存到JSON字段和独立的school_document_data_source_config表)
|
|
(new DocumentTemplateService())->savePlaceholderConfig($data['template_id'], $data['configs']);
|
|
return success('配置保存成功');
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取可用数据源列表(安全白名单)
|
|
* @return \think\Response
|
|
*/
|
|
public function getDataSources()
|
|
{
|
|
try {
|
|
$result = (new DocumentTemplateService())->getDataSources();
|
|
return success('SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成Word文档
|
|
* @return \think\Response
|
|
*/
|
|
public function generateDocument()
|
|
{
|
|
$data = $this->request->params([
|
|
["template_id", 0],
|
|
["fill_data", []],
|
|
["output_filename", ""]
|
|
]);
|
|
|
|
$this->validate($data, 'app\validate\document\DocumentTemplate.generateDocument');
|
|
|
|
try {
|
|
$result = (new DocumentTemplateService())->generateDocument($data);
|
|
return success('GENERATE_SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 下载生成的文档
|
|
* @param int $log_id
|
|
* @return \think\Response
|
|
*/
|
|
public function downloadDocument(int $log_id)
|
|
{
|
|
try {
|
|
return (new DocumentTemplateService())->downloadDocument($log_id);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文档生成记录
|
|
* @return \think\Response
|
|
*/
|
|
public function getGenerateLog()
|
|
{
|
|
$data = $this->request->params([
|
|
["template_id", 0],
|
|
["user_id", 0],
|
|
["status", ""],
|
|
["created_at", ["", ""]]
|
|
]);
|
|
|
|
return success((new DocumentTemplateService())->getGenerateLog($data));
|
|
}
|
|
|
|
/**
|
|
* 预览模板内容
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function previewTemplate(int $id)
|
|
{
|
|
try {
|
|
$result = (new DocumentTemplateService())->previewTemplate($id);
|
|
return success('SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除模板
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function delete(int $id)
|
|
{
|
|
try {
|
|
(new DocumentTemplateService())->delete($id);
|
|
return success('DELETE_SUCCESS');
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 复制模板
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function copy(int $id)
|
|
{
|
|
try {
|
|
$result = (new DocumentTemplateService())->copy($id);
|
|
return success('COPY_SUCCESS', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量删除生成记录
|
|
* @return \think\Response
|
|
*/
|
|
public function batchDeleteLog()
|
|
{
|
|
$data = $this->request->params([
|
|
["ids", []]
|
|
]);
|
|
|
|
if (empty($data['ids'])) {
|
|
return fail('PARAM_ERROR');
|
|
}
|
|
|
|
try {
|
|
(new DocumentTemplateService())->batchDeleteLog($data['ids']);
|
|
return success('DELETE_SUCCESS');
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新模板状态
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function updateStatus(int $id)
|
|
{
|
|
$data = $this->request->params([
|
|
['contract_status', '']
|
|
]);
|
|
|
|
if (empty($data['contract_status'])) {
|
|
return fail('状态参数不能为空');
|
|
}
|
|
|
|
// 验证状态值是否有效
|
|
$validStatuses = ['draft', 'active', 'inactive'];
|
|
if (!in_array($data['contract_status'], $validStatuses)) {
|
|
return fail('无效的状态值');
|
|
}
|
|
|
|
try {
|
|
(new DocumentTemplateService())->updateStatus($id, $data['contract_status']);
|
|
return success('状态更新成功');
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存数据源配置
|
|
* @return \think\Response
|
|
*/
|
|
public function saveDataSourceConfig()
|
|
{
|
|
$data = $this->request->params([
|
|
['contract_id', 0],
|
|
['configs', []]
|
|
]);
|
|
|
|
if (empty($data['contract_id'])) {
|
|
return fail('合同ID不能为空');
|
|
}
|
|
|
|
if (empty($data['configs']) || !is_array($data['configs'])) {
|
|
return fail('配置数据不能为空');
|
|
}
|
|
|
|
try {
|
|
(new DocumentTemplateService())->saveDataSourceConfig($data['contract_id'], $data['configs']);
|
|
return success('保存成功');
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 重新识别占位符
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function reidentify(int $id)
|
|
{
|
|
try {
|
|
$result = (new DocumentTemplateService())->reidentifyPlaceholders($id);
|
|
return success('重新识别成功', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新模板Word文档
|
|
* @param int $id
|
|
* @return \think\Response
|
|
*/
|
|
public function updateFile(int $id)
|
|
{
|
|
try {
|
|
// 获取上传的文件
|
|
$file = Request::file('file');
|
|
if (!$file) {
|
|
return fail('请选择要上传的Word文档');
|
|
}
|
|
|
|
$result = (new DocumentTemplateService())->updateTemplateFile($id, $file);
|
|
return success('文档更新成功', $result);
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
}
|