Citys.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php namespace App\Models\Manager;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 城市模型
  6. *
  7. */
  8. class Citys extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'city';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 失败返回0
  30. if( !$id ) return 0;
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id,$data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where([['id','=',$id]])->update($data);
  44. // 失败返回0
  45. if( !$result ) return 0;
  46. // 返回结果
  47. return $id;
  48. }
  49. /**
  50. * 获取列表
  51. * @param Bool $force 是否强制更新
  52. *
  53. */
  54. public function getList($force = false)
  55. {
  56. // 结果数据
  57. $list = $force ? [] : cache('admin:city:list');
  58. // 不存在数据
  59. if ( !$list ) {
  60. // 从数据库获取数据
  61. $data = $this->query()->where([['status','=',0],['level','<',3]])->get(['id','name','pid']);
  62. // 是否有数据
  63. $data = $data ? $data->toArray() : [];
  64. // 循环处理数据
  65. $list = [];
  66. // 进行更新
  67. foreach ($data as $value) {
  68. // 重组数据
  69. $list[$value['id']] = $value;
  70. }
  71. $list = $this->getTree($list);
  72. // 存起来
  73. cache(['admin:city:list'=>$list]);
  74. }
  75. // 返回结果
  76. return $list;
  77. }
  78. /**
  79. * 获取某个
  80. *
  81. * @param int $id ID
  82. * @param string $field 指定字段
  83. *
  84. */
  85. public function getOne($id,$field='')
  86. {
  87. // 获取列表数据
  88. $list = $this->getList();
  89. // 获取数据
  90. $one = isset($list[$id]) ? $list[$id] : [];
  91. // 返回值
  92. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  93. }
  94. /**
  95. * 获取某个地区名称
  96. * @author 唐远望
  97. * @verson 1.0
  98. * @date 2025-12-23
  99. *
  100. */
  101. public function get_city_name($id, $force = false)
  102. {
  103. $list = $force ? [] : cache('admin:city:source_list_name');
  104. // 不存在数据
  105. if (!$list) {
  106. // 获取列表数据
  107. $list_data = $this->query()->where([['status', '=', 0], ['level', '<', 3]])->get(['id', 'name', 'pid'])->toArray();
  108. if (!empty($list_data)) {
  109. foreach ($list_data as $key => $value) {
  110. $list[$value['id']] = $value['name'];
  111. }
  112. }
  113. cache(['admin:city:source_list_name' => $list]);
  114. }
  115. // 获取数据
  116. $one = isset($list[$id]) ? $list[$id] : [];
  117. // 返回值
  118. return $one;
  119. }
  120. /**
  121. * 获取城市对应的ID
  122. *
  123. * @param string $name 城市名
  124. *
  125. */
  126. public function getIdByName($name){
  127. // 返回结果
  128. return (int) $this->query()->where([['name','=',$name]])->value('id');
  129. }
  130. /**
  131. * 获取城市列表
  132. *
  133. */
  134. public function getCityList(){
  135. // 城市json
  136. $cityList = @file_get_contents(resource_path('./js/').'city.json');
  137. // 解码
  138. $cityList = (array) json_decode($cityList,true);
  139. // 返回结果
  140. return $cityList;
  141. }
  142. /**
  143. * 无限极分类
  144. * @param type $array
  145. * @return type $new_array
  146. */
  147. public function getTree($array) {
  148. //遍历数组,按照id作为键名重新组建新的数组
  149. $new_array = [];
  150. foreach ($array as $v) {
  151. $new_array[$v['id']] = $v;
  152. }
  153. //遍历新的数组,对每一个值的pid进行判断,判断新数组中是否存在键名为pid的值,如果存在,当前值为存在的pid的子节点,不存在,当前值为一级节点,添加到返回的数据中作为一级节点。这里使用引用传值是因为直接传值是不会影响到函数外边的变量值,我们这里要给一级节点添加子节点(sons),所以需要用到引用传值。
  154. $return_tree = [];
  155. foreach ($new_array as $kk => $vv) {
  156. if (isset($new_array[$vv['pid']])) {
  157. $new_array[$vv['pid']]['sons'][] = &$new_array[$kk];
  158. } else {
  159. $return_tree[] = &$new_array[$kk];
  160. }
  161. }
  162. return $return_tree;
  163. }
  164. }