浏览代码

[智价云] 城市接口补充2

tangyuanwang 1 周之前
父节点
当前提交
3f7faabbcc
共有 2 个文件被更改,包括 173 次插入0 次删除
  1. 45 0
      app/Http/Requests/Manager/Citys.php
  2. 128 0
      app/Models/Manager/Citys.php

+ 45 - 0
app/Http/Requests/Manager/Citys.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace  App\Http\Requests\Manager;
+
+use App\Http\Requests\BaseRequest;
+
+/**
+ * 城市管理
+ * @author  唐远望
+ * @version 1.0
+ * @date 2025-12-04
+ *
+ */
+class Citys extends BaseRequest
+{
+
+    /**
+     *
+     * @return  array
+     */
+    public  function  rules()
+    {
+        return [
+            'page'  =>  'required',
+            'limit'  =>  'required',
+        ];
+    }
+
+    // 场景列表
+    protected   $scenes         = [
+        'list'                  => [''],
+    ];
+
+    /**
+     *
+     * @return  array
+     */
+    public  function  messages()
+    {
+        return [
+            'page.required'  =>  '请输入页码',
+            'limit.required'  =>  '请输入每页显示数量',
+        ];
+    }
+}

+ 128 - 0
app/Models/Manager/Citys.php

@@ -0,0 +1,128 @@
+<?php namespace App\Models\Manager;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * 城市模型
+ * 
+ */
+class Citys extends Model
+{
+    use HasFactory;
+
+    // 与模型关联的表名
+    protected $table = 'city';
+    // 是否主动维护时间戳
+    public $timestamps = false;
+    // 定义时间戳字段名
+    // const CREATED_AT = 'insert_time';
+    // const UPDATED_AT = 'update_time';
+
+    /**
+     * 添加数据
+     * 
+     */
+    public function add($data)
+    {
+        // 时间
+        $data['insert_time']                = time();
+        $data['update_time']				= time();
+        // 写入数据表
+        $id						            = $this->query()->insertGetId($data);
+        // 失败返回0
+        if( !$id )                          return 0;
+        // 返回结果
+        return                              $id;
+    }
+
+    /**
+     * 添加数据
+     * 
+     */
+    public function edit($id,$data)
+    {
+        // 更新时间
+        $data['update_time']                = time();
+        // 写入数据表
+        $result						        = $this->query()->where([['id','=',$id]])->update($data);
+        // 失败返回0
+        if( !$result )                      return 0;
+        // 返回结果
+        return                              $id;
+    }
+
+
+
+    /**
+     * 获取列表
+     * @param   Bool    $force  是否强制更新
+     * 
+     */
+    public function getList($force = false)
+    {
+        // 结果数据
+        $list                  = $force ? [] : cache('admin:city:list');
+        // 不存在数据
+        if ( !$list ) {
+            // 从数据库获取数据
+            $data              = $this->query()->where([['status','=',0]])->get(['id','name','pid']);
+            // 是否有数据
+            $data              = $data ? $data->toArray() : [];
+            // 循环处理数据
+            $list              = [];
+            // 进行更新
+            foreach ($data as $value) {
+                // 重组数据
+                $list[$value['id']] = $value;
+            }
+            // 存起来
+            cache(['admin:city:list'=>$list]);
+        }
+        // 返回结果
+        return                  $list;
+    }
+
+    /**
+     * 获取某个
+     * 
+     * @param   int         $id     ID
+     * @param   string     $field   指定字段
+     * 
+     */
+    public function getOne($id,$field='')
+    {
+        // 获取列表数据
+        $list                   = $this->getList();
+        // 获取数据
+        $one                    = isset($list[$id]) ? $list[$id] : [];
+        // 返回值
+        return                  empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
+    
+    }
+
+    /**
+     * 获取城市对应的ID
+     * 
+     * @param   string         $name 城市名
+     * 
+     */
+    public function getIdByName($name){
+        // 返回结果
+        return                  (int) $this->query()->where([['name','=',$name]])->value('id');
+    }
+
+    /**
+     * 获取城市列表
+     * 
+     */
+    public function getCityList(){
+        // 城市json
+        $cityList               = @file_get_contents(resource_path('./js/').'city.json');
+        // 解码
+        $cityList               = (array) json_decode($cityList,true);
+        // 返回结果
+        return                  $cityList;
+    }
+
+}