Citys.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * 获取城市对应的ID
  96. *
  97. * @param string $name 城市名
  98. *
  99. */
  100. public function getIdByName($name){
  101. // 返回结果
  102. return (int) $this->query()->where([['name','=',$name]])->value('id');
  103. }
  104. /**
  105. * 获取城市列表
  106. *
  107. */
  108. public function getCityList(){
  109. // 城市json
  110. $cityList = @file_get_contents(resource_path('./js/').'city.json');
  111. // 解码
  112. $cityList = (array) json_decode($cityList,true);
  113. // 返回结果
  114. return $cityList;
  115. }
  116. /**
  117. * 无限极分类
  118. * @param type $array
  119. * @return type $new_array
  120. */
  121. public function getTree($array) {
  122. //遍历数组,按照id作为键名重新组建新的数组
  123. $new_array = [];
  124. foreach ($array as $v) {
  125. $new_array[$v['id']] = $v;
  126. }
  127. //遍历新的数组,对每一个值的pid进行判断,判断新数组中是否存在键名为pid的值,如果存在,当前值为存在的pid的子节点,不存在,当前值为一级节点,添加到返回的数据中作为一级节点。这里使用引用传值是因为直接传值是不会影响到函数外边的变量值,我们这里要给一级节点添加子节点(sons),所以需要用到引用传值。
  128. $return_tree = [];
  129. foreach ($new_array as $kk => $vv) {
  130. if (isset($new_array[$vv['pid']])) {
  131. $new_array[$vv['pid']]['sons'][] = &$new_array[$kk];
  132. } else {
  133. $return_tree[] = &$new_array[$kk];
  134. }
  135. }
  136. return $return_tree;
  137. }
  138. }