Citys.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * 获取城市ID列表
  80. * @param Bool $force 是否强制更新
  81. *
  82. */
  83. public function get_city_id_list($force = false)
  84. {
  85. // 结果数据
  86. $list = $force ? [] : cache('admin:city_id:list');
  87. // 不存在数据
  88. if ( !$list ) {
  89. // 从数据库获取数据
  90. $data = $this->query()->where([['status','=',0],['level','<',3]])->get(['id','name','pid'])->keyBy('name')->toarray();
  91. // 是否有数据
  92. $list = $data ? $data: [];
  93. // 存起来
  94. cache(['admin:city_id:list'=>$list]);
  95. }
  96. // 返回结果
  97. return $list;
  98. }
  99. /**
  100. * 获取某个
  101. *
  102. * @param int $id ID
  103. * @param string $field 指定字段
  104. *
  105. */
  106. public function getOne($id,$field='')
  107. {
  108. // 获取列表数据
  109. $list = $this->getList();
  110. // 获取数据
  111. $one = isset($list[$id]) ? $list[$id] : [];
  112. // 返回值
  113. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  114. }
  115. /**
  116. * 获取某个地区名称
  117. * @author 唐远望
  118. * @verson 1.0
  119. * @date 2025-12-23
  120. *
  121. */
  122. public function get_city_name($id, $force = false)
  123. {
  124. $list = $force ? [] : cache('admin:city:source_list_name');
  125. // 不存在数据
  126. if (!$list) {
  127. // 获取列表数据
  128. $list_data = $this->query()->where([['status', '=', 0], ['level', '<', 3]])->get(['id', 'name', 'pid'])->toArray();
  129. if (!empty($list_data)) {
  130. foreach ($list_data as $key => $value) {
  131. $list[$value['id']] = $value['name'];
  132. }
  133. }
  134. cache(['admin:city:source_list_name' => $list]);
  135. }
  136. // 获取数据
  137. $one = isset($list[$id]) ? $list[$id] : [];
  138. // 返回值
  139. return $one;
  140. }
  141. /**
  142. * 获取城市对应的ID
  143. *
  144. * @param string $name 城市名
  145. *
  146. */
  147. public function getIdByName($name){
  148. // 返回结果
  149. return (int) $this->query()->where([['name','=',$name]])->value('id');
  150. }
  151. /**
  152. * 获取城市列表
  153. *
  154. */
  155. public function getCityList(){
  156. // 城市json
  157. $cityList = @file_get_contents(resource_path('./js/').'city.json');
  158. // 解码
  159. $cityList = (array) json_decode($cityList,true);
  160. // 返回结果
  161. return $cityList;
  162. }
  163. /**
  164. * 无限极分类
  165. * @param type $array
  166. * @return type $new_array
  167. */
  168. public function getTree($array) {
  169. //遍历数组,按照id作为键名重新组建新的数组
  170. $new_array = [];
  171. foreach ($array as $v) {
  172. $new_array[$v['id']] = $v;
  173. }
  174. //遍历新的数组,对每一个值的pid进行判断,判断新数组中是否存在键名为pid的值,如果存在,当前值为存在的pid的子节点,不存在,当前值为一级节点,添加到返回的数据中作为一级节点。这里使用引用传值是因为直接传值是不会影响到函数外边的变量值,我们这里要给一级节点添加子节点(sons),所以需要用到引用传值。
  175. $return_tree = [];
  176. foreach ($new_array as $kk => $vv) {
  177. if (isset($new_array[$vv['pid']])) {
  178. $new_array[$vv['pid']]['sons'][] = &$new_array[$kk];
  179. } else {
  180. $return_tree[] = &$new_array[$kk];
  181. }
  182. }
  183. return $return_tree;
  184. }
  185. }