MapSpotAdmin.php 2.27 KB
<?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' => []]);
    }
}