Data.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Area\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. public function getProvince()
  7. {
  8. return Dever::db('province', 'area')->select(array('status' => 1));
  9. }
  10. public function getCity($province_id)
  11. {
  12. if ($province_id) {
  13. $where['province_id'] = $province_id;
  14. }
  15. $where['status'] = 1;
  16. return Dever::db('city', 'area')->select($where);
  17. }
  18. public function getCounty($city_id)
  19. {
  20. if ($city_id) {
  21. $where['city_id'] = $city_id;
  22. }
  23. $where['status'] = 1;
  24. return Dever::db('county', 'area')->select($where);
  25. }
  26. public function getTown($county_id)
  27. {
  28. if ($county_id) {
  29. $where['county_id'] = $county_id;
  30. }
  31. $where['status'] = 1;
  32. return Dever::db('town', 'area')->select($where);
  33. }
  34. public function getVillage($town_id)
  35. {
  36. if ($town_id) {
  37. $where['town_id'] = $town_id;
  38. }
  39. $where['status'] = 1;
  40. return Dever::db('village', 'area')->select($where);
  41. }
  42. # 获取城市并根据首字母排序的
  43. public function getCityToFirst()
  44. {
  45. $result = array();
  46. $data = $this->getCity(false);
  47. if (Dever::import('pinyin')) {
  48. $result = Dever::sortPinyinFirst($data, 'pinyin_first');
  49. }
  50. return $result;
  51. }
  52. /**
  53. * 获取详细信息
  54. *
  55. * @return mixed
  56. */
  57. public function getInfo($area, $col = 'id')
  58. {
  59. if ($area) {
  60. $area = explode(',', $area);
  61. $result = array();
  62. foreach ($area as $k => $v) {
  63. if ($k == 0) {
  64. $result[$k] = $this->getName('province', $v, true, $col);
  65. } elseif ($k == 1) {
  66. $result[$k] = $this->getName('city', $v, true, $col);
  67. if ($col == 'id' && isset($result[1]['name']) && $result[0]['name'] == $result[1]['name']) {
  68. unset($result[1]);
  69. }
  70. } elseif ($k == 2) {
  71. $result[$k] = $this->getName('county', $v, true, $col);
  72. } elseif ($k == 3) {
  73. $result[$k] = $this->getName('town', $v, true, $col);
  74. } elseif ($k == 4) {
  75. $result[$k] = $this->getName('village', $v, true, $col);
  76. }
  77. }
  78. return $result;
  79. }
  80. return array();
  81. }
  82. /**
  83. * 根据地区id转成名称
  84. *
  85. * @return mixed
  86. */
  87. public function string($area, $im = ',', $name = '不限', $unset = true, $check = false)
  88. {
  89. if ($area) {
  90. if (is_string($area)) {
  91. $area = explode(',', $area);
  92. }
  93. $result = array();
  94. foreach ($area as $k => $v) {
  95. if ($k == 0) {
  96. $result[$k] = $this->getName('province', $v, false, 'id', $name);
  97. } elseif ($k == 1) {
  98. $result[$k] = $this->getName('city', $v, false, 'id', $name);
  99. if (isset($result[0]) && $result[0] == $result[1] && $unset) {
  100. unset($result[1]);
  101. }
  102. } elseif ($k == 2) {
  103. $parent = $area[0] . ',' . $area[1];
  104. $result[$k] = $this->getName('county', $v, false, 'id', $name, $check, $parent);
  105. } elseif ($k == 3) {
  106. $parent = $area[0] . ',' . $area[1] . ',' . $area[2];
  107. $result[$k] = $this->getName('town', $v, false, 'id', $name, $check, $parent);
  108. } elseif ($k == 4) {
  109. $result[$k] = $this->getName('village', $v, false, 'id', $name);
  110. } else {
  111. $result[$k] = '';
  112. }
  113. if (isset($result[$k]) && !$result[$k]) {
  114. unset($result[$k]);
  115. }
  116. }
  117. return implode($im, $result);
  118. }
  119. return '';
  120. }
  121. private function getName($table, $value, $state = false, $col = 'id', $name = '不限', $check = false, $area = array())
  122. {
  123. if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
  124. $where[$col] = $value;
  125. $data = Dever::db($table, 'area')->find($where);
  126. if ($state) {
  127. return $data;
  128. }
  129. if ($data) {
  130. $name = $data['name'];
  131. if ($check && $area && $data['area'] != $area) {
  132. $name = '<font style="color:red">'.$name.'(错误)</font>';
  133. }
  134. }
  135. }
  136. return $name;
  137. }
  138. public function pinyin($data)
  139. {
  140. if (Dever::project('pinyin') && $data['name']) {
  141. $data['pinyin'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
  142. $data['pinyin_first'] = Dever::load('convert', 'pinyin')->getPinyinFirst($data['name']);
  143. }
  144. return $data;
  145. }
  146. }