Blame view

稻城亚丁小程序/backend/app/controller/admin/MapSpotAdmin.php 2.27 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
  <?php
  declare(strict_types=1);
  
  namespace app\controller\admin;
  
  use app\BaseController;
  use think\facade\Db;
  
  class MapSpotAdmin extends BaseController
  {
      public function list()
      {
          $rows = Db::name('map_spot')
              ->order('sort', 'asc')
              ->order('id', 'asc')
              ->select()
              ->toArray();
  
          return json(['code' => 0, 'msg' => 'ok', 'data' => ['list' => $rows]]);
      }
  
      public function save()
      {
          $id          = (int) $this->request->post('id', 0);
          $name        = trim((string) $this->request->post('name', ''));
          $lat         = (float) $this->request->post('lat', 0);
          $lng         = (float) $this->request->post('lng', 0);
          $description = trim((string) $this->request->post('description', ''));
          $thumbnail   = trim((string) $this->request->post('thumbnail', ''));
          $photosCount = (int) $this->request->post('photos_count', 0);
          $pinLeftPct  = (float) $this->request->post('pin_left_pct', 50);
          $pinTopPct   = (float) $this->request->post('pin_top_pct', 50);
          $sort        = (int) $this->request->post('sort', 0);
          $status      = (int) $this->request->post('status', 1);
  
          if ($name === '') {
              return json(['code' => 400, 'msg' => '名称必填', 'data' => []]);
          }
  
          $now = date('Y-m-d H:i:s');
          $row = [
              'name'          => $name,
              'lat'           => $lat,
              'lng'           => $lng,
              'description'   => $description,
              'thumbnail'     => $thumbnail,
              'photos_count'  => $photosCount,
              'pin_left_pct'  => $pinLeftPct,
              'pin_top_pct'   => $pinTopPct,
              'sort'          => $sort,
              'status'        => $status === 0 ? 0 : 1,
              'update_time'   => $now,
          ];
  
          if ($id <= 0) {
              $row['create_time'] = $now;
              Db::name('map_spot')->insert($row);
          } else {
              Db::name('map_spot')->where('id', $id)->update($row);
          }
  
          return json(['code' => 0, 'msg' => '已保存', 'data' => []]);
      }
  
      public function delete(int $id)
      {
          Db::name('map_spot')->where('id', $id)->delete();
          return json(['code' => 0, 'msg' => '已删除', 'data' => []]);
      }
  }