소스 검색

[智价云] 地区查询更新

tangyuanwang 1 개월 전
부모
커밋
9dd78b4df2
1개의 변경된 파일53개의 추가작업 그리고 31개의 파일을 삭제
  1. 53 31
      app/Models/Manager/Citys.php

+ 53 - 31
app/Models/Manager/Citys.php

@@ -1,4 +1,6 @@
-<?php namespace App\Models\Manager;
+<?php
+
+namespace App\Models\Manager;
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
@@ -27,11 +29,11 @@ class Citys extends Model
     {
         // 时间
         $data['insert_time']                = time();
-        $data['update_time']				= time();
+        $data['update_time']                = time();
         // 写入数据表
-        $id						            = $this->query()->insertGetId($data);
+        $id                                    = $this->query()->insertGetId($data);
         // 失败返回0
-        if( !$id )                          return 0;
+        if (!$id)                          return 0;
         // 返回结果
         return                              $id;
     }
@@ -40,14 +42,14 @@ class Citys extends Model
      * 添加数据
      * 
      */
-    public function edit($id,$data)
+    public function edit($id, $data)
     {
         // 更新时间
         $data['update_time']                = time();
         // 写入数据表
-        $result						        = $this->query()->where([['id','=',$id]])->update($data);
+        $result                                = $this->query()->where([['id', '=', $id]])->update($data);
         // 失败返回0
-        if( !$result )                      return 0;
+        if (!$result)                      return 0;
         // 返回结果
         return                              $id;
     }
@@ -64,9 +66,9 @@ class Citys extends Model
         // 结果数据
         $list                  = $force ? [] : cache('admin:city:list');
         // 不存在数据
-        if ( !$list ) {
+        if (!$list) {
             // 从数据库获取数据
-            $data              = $this->query()->where([['status','=',0],['level','<',3]])->get(['id','name','pid']);
+            $data              = $this->query()->where([['status', '=', 0], ['level', '<', 3]])->get(['id', 'name', 'pid']);
             // 是否有数据
             $data              = $data ? $data->toArray() : [];
             // 循环处理数据
@@ -78,9 +80,9 @@ class Citys extends Model
             }
             $list = $this->getTree($list);
             // 存起来
-            cache(['admin:city:list'=>$list]);
+            cache(['admin:city:list' => $list]);
         }
-        
+
         // 返回结果
         return                  $list;
     }
@@ -95,18 +97,42 @@ class Citys extends Model
         // 结果数据
         $list                  = $force ? [] : cache('admin:city_id:list');
         // 不存在数据
-        if ( !$list ) {
+        if (!$list) {
+            // 从数据库获取数据
+            $data              = $this->query()->where([['status', '=', 0], ['level', '<=', 2]])->get(['id', 'name', 'pid'])->keyBy('name')->toarray();
+            // 是否有数据
+            $list              = $data ? $data : [];
+            // 存起来
+            cache(['admin:city_id:list' => $list]);
+        }
+        // 返回结果
+        return                  $list;
+    }
+
+    /**
+     * 获取省份ID列表
+     * @param   Bool    $force  是否强制更新
+     * 
+     */
+    public function get_province_id_list($force = false)
+    {
+        // 结果数据
+        $list                  = $force ? [] : cache('admin:province_id:list');
+        // 不存在数据
+        if (!$list) {
             // 从数据库获取数据
-            $data              = $this->query()->where([['status','=',0],['level','<',3]])->get(['id','name','pid'])->keyBy('name')->toarray();
+            $data              = $this->query()->where([['status', '=', 0], ['level', '=', 1]])->get(['id', 'name', 'pid'])->keyBy('name')->toarray();
             // 是否有数据
-            $list              = $data ? $data: [];
+            $list              = $data ? $data : [];
             // 存起来
-            cache(['admin:city_id:list'=>$list]);
+            cache(['admin:province_id:list' => $list]);
         }
         // 返回结果
         return                  $list;
     }
 
+
+
     /**
      * 获取某个
      * 
@@ -114,15 +140,14 @@ class Citys extends Model
      * @param   string     $field   指定字段
      * 
      */
-    public function getOne($id,$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);
-    
+        return                  empty($field) ? $one : (isset($one[$field]) ? $one[$field] : null);
     }
 
 
@@ -159,20 +184,22 @@ class Citys extends Model
      * @param   string         $name 城市名
      * 
      */
-    public function getIdByName($name){
+    public function getIdByName($name)
+    {
         // 返回结果
-        return                  (int) $this->query()->where([['name','=',$name]])->value('id');
+        return                  (int) $this->query()->where([['name', '=', $name]])->value('id');
     }
 
     /**
      * 获取城市列表
      * 
      */
-    public function getCityList(){
+    public function getCityList()
+    {
         // 城市json
-        $cityList               = @file_get_contents(resource_path('./js/').'city.json');
+        $cityList               = @file_get_contents(resource_path('./js/') . 'city.json');
         // 解码
-        $cityList               = (array) json_decode($cityList,true);
+        $cityList               = (array) json_decode($cityList, true);
         // 返回结果
         return                  $cityList;
     }
@@ -187,7 +214,8 @@ class Citys extends Model
 
      */
 
-    public function getTree($array) {
+    public function getTree($array)
+    {
 
         //遍历数组,按照id作为键名重新组建新的数组
 
@@ -196,7 +224,6 @@ class Citys extends Model
         foreach ($array as $v) {
 
             $new_array[$v['id']] = $v;
-
         }
 
         //遍历新的数组,对每一个值的pid进行判断,判断新数组中是否存在键名为pid的值,如果存在,当前值为存在的pid的子节点,不存在,当前值为一级节点,添加到返回的数据中作为一级节点。这里使用引用传值是因为直接传值是不会影响到函数外边的变量值,我们这里要给一级节点添加子节点(sons),所以需要用到引用传值。
@@ -208,17 +235,12 @@ class Citys extends Model
             if (isset($new_array[$vv['pid']])) {
 
                 $new_array[$vv['pid']]['sons'][] = &$new_array[$kk];
-
             } else {
 
                 $return_tree[] = &$new_array[$kk];
-
             }
-
         }
 
         return $return_tree;
-
     }
-
-}
+}