Blame view

稻城亚丁小程序/backend/app/controller/admin/DownloadAsset.php 2.73 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
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
  <?php
  declare(strict_types=1);
  
  namespace app\controller\admin;
  
  use app\BaseController;
  use think\facade\Db;
  use Throwable;
  
  class DownloadAsset extends BaseController
  {
      private const TYPES = ['certificate', 'guide'];
  
      public function get()
      {
          $map = [];
          try {
              $rows = Db::name('download_asset')->select()->toArray();
              foreach ($rows as $r) {
                  $type = strtolower(trim((string) ($r['type'] ?? '')));
                  if (!in_array($type, self::TYPES, true)) {
                      continue;
                  }
                  $map[$type] = [
                      'imageUrl'    => trim((string) ($r['image_url'] ?? '')),
                      'updatedAt'   => (string) ($r['update_time'] ?? ''),
                  ];
              }
          } catch (Throwable) {
              // 若未执行补丁 SQL,界面回落到默认图并提示先执行数据库补丁。
          }
  
          return json([
              'code' => 0,
              'msg'  => 'ok',
              'data' => [
                  'certificate' => $map['certificate']['imageUrl'] ?? '/static/certificate-dialog-bg.png',
                  'guide'       => $map['guide']['imageUrl'] ?? '/static/daocheng-yading-map.png',
                  'updatedAt'   => [
                      'certificate' => $map['certificate']['updatedAt'] ?? '',
                      'guide'       => $map['guide']['updatedAt'] ?? '',
                  ],
              ],
          ]);
      }
  
      public function save()
      {
          $type = strtolower(trim((string) $this->request->post('type', '')));
          $imageUrl = trim((string) $this->request->post('image_url', ''));
          if (!in_array($type, self::TYPES, true)) {
              return json(['code' => 400, 'msg' => '素材类型错误', 'data' => []]);
          }
          if ($imageUrl === '') {
              return json(['code' => 400, 'msg' => '图片地址不能为空', 'data' => []]);
          }
  
          $now = date('Y-m-d H:i:s');
          try {
              $row = Db::name('download_asset')->where('type', $type)->find();
              if ($row) {
                  Db::name('download_asset')->where('id', (int) $row['id'])->update([
                      'image_url'   => $imageUrl,
                      'update_time' => $now,
                  ]);
              } else {
                  Db::name('download_asset')->insert([
                      'type'        => $type,
                      'image_url'   => $imageUrl,
                      'create_time' => $now,
                      'update_time' => $now,
                  ]);
              }
          } catch (Throwable) {
              return json(['code' => 500, 'msg' => '请先执行 patch_download_asset.sql', 'data' => []]);
          }
  
          return json(['code' => 0, 'msg' => '已保存', 'data' => []]);
      }
  }