Citys.php 6.9 KB

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