|
|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|