Blame view

天文台pc/daocheng-api/app/controller/KioskApi.php 8.07 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
  <?php
  declare(strict_types=1);
  
  namespace app\controller;
  
  use app\BaseController;
  use app\service\KioskService;
  use think\App;
3a3dc915   王天杨   feat: 稻城亚丁项目批量更新
9
  use think\file\UploadedFile;
bc518174   王天杨   提交两个项目文件
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  use think\Response;
  
  class KioskApi extends BaseController
  {
      protected KioskService $svc;
  
      public function __construct(App $app)
      {
          parent::__construct($app);
          $this->svc = new KioskService();
      }
  
      public function bundle(): Response
      {
          try {
              $data = $this->svc->getBundle();
              return json(['code' => 0, 'data' => $data]);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      /** 天文知识库专用列表(与 bundle 内 knowledge 同源,便于页面独立刷新) */
      public function knowledge(): Response
      {
          try {
              return json(['code' => 0, 'data' => ['entries' => $this->svc->getKnowledgeList()]]);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      public function dataDisplay(): Response
      {
          try {
              return json(['code' => 0, 'data' => $this->svc->getDataDisplay()]);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      public function observatoryHistory(): Response
      {
          try {
              return json(['code' => 0, 'data' => $this->svc->getObservatoryHistory()]);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      public function saveHomeBackgrounds(): Response
      {
          $urls = $this->request->post('urls');
          if (!is_array($urls)) {
              return json(['code' => 400, 'msg' => 'urls must be array'], 400);
          }
          $clean = array_values(array_filter($urls, fn ($u) => is_string($u) && $u !== ''));
          try {
              $this->svc->setJsonKv(\app\common\KioskDefaults::KEY_HOME_BG, $clean);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      public function saveWelcome(): Response
      {
          $w = $this->request->post();
          $payload = [
              'zh-CN' => (string) ($w['zh-CN'] ?? ''),
              'en'    => (string) ($w['en'] ?? ''),
              'bo'    => (string) ($w['bo'] ?? ''),
          ];
          try {
              $this->svc->setJsonKv(\app\common\KioskDefaults::KEY_WELCOME, $payload);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      public function saveGuide(): Response
      {
          $g = $this->request->post('guide');
          if (!is_array($g)) {
              return json(['code' => 400, 'msg' => 'guide must be object'], 400);
          }
          try {
              $this->svc->setJsonKv(\app\common\KioskDefaults::KEY_GUIDE, $g);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
3a3dc915   王天杨   feat: 稻城亚丁项目批量更新
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      /** 共享天文台视频源(HLS/WebRTC/RTSP + url + note) */
      public function saveVideoSource(): Response
      {
          $body = $this->request->post();
          $protocol = (string) ($body['protocol'] ?? 'HLS');
          if (!in_array($protocol, ['HLS', 'WebRTC', 'RTSP'], true)) {
              return json(['code' => 400, 'msg' => 'protocol must be HLS, WebRTC or RTSP'], 400);
          }
          $payload = [
              'protocol' => $protocol,
              'url'      => (string) ($body['url'] ?? ''),
              'note'     => (string) ($body['note'] ?? ''),
          ];
          try {
              $this->svc->setJsonKv(\app\common\KioskDefaults::KEY_VIDEO_SOURCE, $payload);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
      /** 轮播背景 URL 列表(与首页背景分开存储) */
      public function saveCarouselBackgrounds(): Response
      {
          $urls = $this->request->post('urls');
          if (!is_array($urls)) {
              return json(['code' => 400, 'msg' => 'urls must be array'], 400);
          }
          $clean = array_values(array_filter($urls, fn ($u) => is_string($u) && $u !== ''));
          try {
              $this->svc->setJsonKv(\app\common\KioskDefaults::KEY_CAROUSEL_BG, $clean);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
bc518174   王天杨   提交两个项目文件
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      public function saveKnowledge(): Response
      {
          $entries = $this->request->post('entries');
          if (!is_array($entries)) {
              return json(['code' => 400, 'msg' => 'entries must be array'], 400);
          }
          try {
              $this->svc->replaceKnowledge($entries);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  
3a3dc915   王天杨   feat: 稻城亚丁项目批量更新
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
      /** 知识库封面 / 视频 multipart 上传,返回 `/uploads/knowledge/...` 供 sync 写入库表 */
      public function uploadKnowledgeMedia(): Response
      {
          /** @var UploadedFile|null $file */
          $file = $this->request->file('file');
          if (!$file) {
              return json([
                  'code' => 400,
                  'msg'  => '未收到文件(大文件请调大 PHP post_max_size、upload_max_filesize 与 Nginx client_max_body_size)',
              ], 400);
          }
  
          $maxBytes = 2147483648; // 2GiB,约可覆盖多数 10 分钟以内压缩视频
  
          if (!$file->isValid()) {
              return json([
                  'code' => 400,
                  'msg'  => '上传未成功(常见原因:超过 PHP upload_max_filesize / post_max_size)',
              ], 400);
          }
  
          if ($file->getSize() > $maxBytes) {
              return json(['code' => 400, 'msg' => '文件超过 2GB 上限'], 400);
          }
  
          $ext = strtolower($file->getOriginalExtension());
          if ($ext === 'jpeg') {
              $ext = 'jpg';
          }
          if ($ext === '') {
              $mime = strtolower($file->getOriginalMime());
              $ext = match ($mime) {
                  'image/jpeg' => 'jpg',
                  'image/png'  => 'png',
                  'image/gif'  => 'gif',
                  'image/webp' => 'webp',
                  'video/mp4'  => 'mp4',
                  'video/webm' => 'webm',
                  'video/quicktime' => 'mov',
                  'video/x-matroska' => 'mkv',
                  default      => '',
              };
          }
  
          $allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'webm', 'mov', 'mkv'];
          if (!in_array($ext, $allowed, true)) {
              return json(['code' => 400, 'msg' => '不支持的文件类型'], 400);
          }
  
          $dir = public_path('uploads/knowledge');
          if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
              return json(['code' => 500, 'msg' => '无法创建上传目录'], 500);
          }
  
          $name = bin2hex(random_bytes(12)) . '.' . $ext;
  
          try {
              $file->move($dir, $name);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
  
          $url = '/uploads/knowledge/' . $name;
  
          return json(['code' => 0, 'msg' => 'ok', 'data' => ['url' => $url]]);
      }
  
bc518174   王天杨   提交两个项目文件
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
      public function seedKnowledge(): Response
      {
          $entries = \app\common\KioskDefaults::knowledge();
          if ($entries === []) {
              return json([
                  'code' => 400,
                  'msg'  => '演示知识种子已关闭,请使用 knowledge/sync 同步真实条目,勿调用本接口清空库表',
              ], 400);
          }
          try {
              $this->svc->replaceKnowledge($entries);
              return json(['code' => 0, 'msg' => 'ok']);
          } catch (\Throwable $e) {
              return json(['code' => 500, 'msg' => $e->getMessage()], 500);
          }
      }
  }