DocsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin;
  4. use App\Controllers\BaseController;
  5. use App\Models\Docs;
  6. use App\Services\LLM;
  7. use App\Utils\Tools;
  8. use Exception;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use Psr\Http\Message\ResponseInterface;
  11. use Slim\Http\Response;
  12. use Slim\Http\ServerRequest;
  13. use function time;
  14. final class DocsController extends BaseController
  15. {
  16. private static array $details =
  17. [
  18. 'field' => [
  19. 'op' => '操作',
  20. 'id' => 'ID',
  21. 'date' => '日期',
  22. 'title' => '标题',
  23. ],
  24. ];
  25. /**
  26. * 后台文档页面
  27. *
  28. * @throws Exception
  29. */
  30. public function index(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  31. {
  32. return $response->write(
  33. $this->view()
  34. ->assign('details', self::$details)
  35. ->fetch('admin/docs/index.tpl')
  36. );
  37. }
  38. /**
  39. * 后台文档创建页面
  40. *
  41. * @throws Exception
  42. */
  43. public function create(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  44. {
  45. return $response->write(
  46. $this->view()
  47. ->fetch('admin/docs/create.tpl')
  48. );
  49. }
  50. /**
  51. * 后台添加文档
  52. */
  53. public function add(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  54. {
  55. $title = $request->getParam('title');
  56. $content = $request->getParam('content');
  57. if ($content === '' || $title === '') {
  58. return $response->withJson([
  59. 'ret' => 0,
  60. 'msg' => '文档标题或内容不能为空',
  61. ]);
  62. }
  63. $doc = new Docs();
  64. $doc->date = Tools::toDateTime(time());
  65. $doc->title = $title;
  66. $doc->content = $content;
  67. if (! $doc->save()) {
  68. return $response->withJson([
  69. 'ret' => 0,
  70. 'msg' => '文档添加失败',
  71. ]);
  72. }
  73. return $response->withJson([
  74. 'ret' => 1,
  75. 'msg' => '文档添加成功',
  76. ]);
  77. }
  78. /**
  79. * 使用LLM生成文档
  80. *
  81. * @throws GuzzleException
  82. */
  83. public function generate(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  84. {
  85. $content = LLM::genTextResponse($request->getParam('question'));
  86. return $response->withJson([
  87. 'ret' => 1,
  88. 'msg' => '文档生成成功',
  89. 'content' => $content,
  90. ]);
  91. }
  92. /**
  93. * 文档编辑页面
  94. *
  95. * @throws Exception
  96. */
  97. public function edit(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  98. {
  99. $doc = (new Docs())->find($args['id']);
  100. return $response->write(
  101. $this->view()
  102. ->assign('doc', $doc)
  103. ->fetch('admin/docs/edit.tpl')
  104. );
  105. }
  106. /**
  107. * 后台编辑文档提交
  108. *
  109. * @param ServerRequest $request
  110. * @param Response $response
  111. * @param array $args
  112. *
  113. * @return Response|ResponseInterface
  114. */
  115. public function update(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  116. {
  117. $doc = (new Docs())->find($args['id']);
  118. $doc->title = $request->getParam('title');
  119. $doc->content = $request->getParam('content');
  120. $doc->date = Tools::toDateTime(time());
  121. if (! $doc->save()) {
  122. return $response->withJson([
  123. 'ret' => 0,
  124. 'msg' => '文档更新失败',
  125. ]);
  126. }
  127. return $response->withJson([
  128. 'ret' => 1,
  129. 'msg' => '文档更新成功',
  130. ]);
  131. }
  132. /**
  133. * 后台删除文档
  134. */
  135. public function delete(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  136. {
  137. $doc = (new Docs())->find($args['id']);
  138. if (! $doc->delete()) {
  139. return $response->withJson([
  140. 'ret' => 0,
  141. 'msg' => '删除失败',
  142. ]);
  143. }
  144. return $response->withJson([
  145. 'ret' => 1,
  146. 'msg' => '删除成功',
  147. ]);
  148. }
  149. /**
  150. * 后台文档页面 AJAX
  151. */
  152. public function ajax(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  153. {
  154. $docs = (new Docs())->orderBy('id')->get();
  155. foreach ($docs as $doc) {
  156. $doc->op = '<button type="button" class="btn btn-red" id="delete-doc-' . $doc->id . '"
  157. onclick="deleteDoc(' . $doc->id . ')">删除</button>
  158. <a class="btn btn-blue" href="/admin/docs/' . $doc->id . '/edit">编辑</a>';
  159. }
  160. return $response->withJson([
  161. 'docs' => $docs,
  162. ]);
  163. }
  164. }