Admin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\aicontent\controller;
  3. use think\addons\Controller;
  4. use addons\aicontent\model\AiTask;
  5. use addons\aicontent\service\ModelFactory;
  6. use addons\aicontent\Aicontent;
  7. /**
  8. * Backend admin controller for the AI Content Assistant plugin.
  9. * Routes under: /addons/aicontent/admin/
  10. */
  11. class Admin extends Controller
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = [];
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. // Verify MaCMS admin session.
  19. // MaCMS requires renaming admin.php (e.g. to manage.php / abcd.php).
  20. // The actual entry filename is only known at runtime via $_SERVER['SCRIPT_NAME']
  21. // and exposed as the JS global ADMIN_PATH — there is no cookie for it.
  22. // For non-AJAX requests we therefore return a plain JSON error + exit,
  23. // since we cannot reliably construct the login URL.
  24. if (session('admin_auth') !== '1' || empty(session('admin_info'))) {
  25. echo json_encode(['code' => 0, 'message' => lang('Session expired. Please log in to the admin panel and try again.')]);
  26. exit;
  27. }
  28. if ($this->request->isPost()) {
  29. $token = input('_csrf_token', '');
  30. $expected = Aicontent::generateCsrfToken();
  31. if (!hash_equals($expected, $token)) {
  32. echo json_encode(['code' => 0, 'message' => lang('Invalid request token.')]);
  33. exit;
  34. }
  35. }
  36. }
  37. /**
  38. * Dashboard — task history and stats.
  39. * GET /addons/aicontent/admin/index
  40. */
  41. public function index()
  42. {
  43. $page = (int) input('page', 1);
  44. $history = AiTask::getHistory($page, 20);
  45. $stats = [
  46. 'total' => AiTask::count(),
  47. 'done' => AiTask::where('status', AiTask::STATUS_DONE)->count(),
  48. 'pending' => AiTask::where('status', AiTask::STATUS_PENDING)->count(),
  49. 'error' => AiTask::where('status', AiTask::STATUS_ERROR)->count(),
  50. ];
  51. return $this->fetch('admin/index', [
  52. 'tasks' => $history['list'],
  53. 'total' => $history['total'],
  54. 'page' => $page,
  55. 'stats' => $stats,
  56. 'providers' => ModelFactory::getProviders(),
  57. 'jsLang' => Aicontent::jsLangJson(),
  58. ]);
  59. }
  60. /**
  61. * Manual content generation form.
  62. * GET /addons/aicontent/admin/generate
  63. * POST /addons/aicontent/admin/generate (handled by Api controller)
  64. */
  65. public function generate()
  66. {
  67. $config = get_addon_config('aicontent');
  68. $providers = ModelFactory::getProviders();
  69. // Build models-per-provider JSON for JS dynamic dropdown
  70. $modelsMap = [];
  71. foreach (array_keys($providers) as $slug) {
  72. $modelsMap[$slug] = ModelFactory::getModelsForProvider($slug);
  73. }
  74. return $this->fetch('admin/generate', [
  75. 'config' => $config,
  76. 'providers' => $providers,
  77. 'modelsMap' => json_encode($modelsMap),
  78. 'jsLang' => Aicontent::jsLangJson(),
  79. ]);
  80. }
  81. /**
  82. * View a single task result.
  83. * GET /addons/aicontent/admin/view?id=123
  84. */
  85. public function view()
  86. {
  87. $id = (int) input('id');
  88. $task = AiTask::get($id);
  89. if (!$task) {
  90. $this->error(lang('Task not found'));
  91. }
  92. $result = null;
  93. if ($task->result) {
  94. $result = json_decode($task->result, true);
  95. }
  96. return $this->fetch('admin/view', [
  97. 'task' => $task->toArray(),
  98. 'result' => $result,
  99. ]);
  100. }
  101. /**
  102. * Delete a task record.
  103. * POST /addons/aicontent/admin/delete
  104. */
  105. public function delete()
  106. {
  107. $id = (int) input('id');
  108. $task = AiTask::get($id);
  109. if (!$task) {
  110. return json(['code' => 0, 'message' => lang('Task not found')]);
  111. }
  112. $task->delete();
  113. return json(['code' => 1, 'message' => lang('Deleted successfully')]);
  114. }
  115. }