area.model.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * 地区模型
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. class areaModel extends Model {
  8. public function __construct() {
  9. parent::__construct('area');
  10. }
  11. /**
  12. * 获取地址列表
  13. *
  14. * @return mixed
  15. */
  16. public function getAreaList($condition = [], $fields = '*', $group = '') {
  17. return $this->where($condition)->field($fields)->limit(false)->group($group)->select();
  18. }
  19. /**
  20. * 获取地址详情
  21. *
  22. * @return mixed
  23. */
  24. public function getAreaInfo($condition = [], $fileds = '*') {
  25. return $this->where($condition)->field($fileds)->find();
  26. }
  27. /**
  28. * 获取一级地址(省级)名称数组
  29. *
  30. * @return array 键为id 值为名称字符串
  31. */
  32. public function getTopLevelAreas() {
  33. // 对象属性中有数据则返回
  34. if ($this->cachedTopLevelAreas !== null)
  35. return $this->cachedTopLevelAreas;
  36. // 缓存中有数据则返回
  37. $arr = [];
  38. if ($arr = rkcache('area_toplevelareas')) {
  39. $this->cachedTopLevelAreas = $arr;
  40. return $arr;
  41. }
  42. $data = $this->getCache();
  43. foreach ($data['children'][0] as $i) {
  44. $arr[$i] = $data['name'][$i];
  45. }
  46. wkcache('area_toplevelareas', $arr);
  47. $this->cachedTopLevelAreas = $arr;
  48. return $arr;
  49. }
  50. /**
  51. * 获取获取市级id对应省级id的数组
  52. *
  53. * @return array 键为市级id 值为省级id
  54. */
  55. public function getCityProvince() {
  56. // 对象属性中有数据则返回
  57. if ($this->cachedCityProvince !== null)
  58. return $this->cachedCityProvince;
  59. // 缓存中有数据则返回
  60. $arr = [];
  61. if ($arr = rkcache('area_cityprovince')) {
  62. $this->cachedCityProvince = $arr;
  63. return $arr;
  64. }
  65. // 生成数据
  66. $data = $this->getCache();
  67. foreach ($data['parent'] as $k => $v) {
  68. if ($v && $data['parent'][$v] == 0) {
  69. $arr[$k] = $v;
  70. }
  71. }
  72. wkcache('area_cityprovince', $arr);
  73. $this->cachedCityProvince = $arr;
  74. return $arr;
  75. }
  76. /**
  77. * 获取地区缓存
  78. *
  79. * @return array
  80. */
  81. public function getAreas() {
  82. return $this->getCache();
  83. }
  84. /**
  85. * 获取全部地区名称数组
  86. *
  87. * @return array 键为id 值为名称字符串
  88. */
  89. public function getAreaNames() {
  90. $data = $this->getCache();
  91. return $data['name'];
  92. }
  93. /**
  94. * 获取用于前端js使用的全部地址数组
  95. *
  96. * @return array
  97. */
  98. public function getAreaArrayForJson() {
  99. $data = $this->getCache();
  100. $arr = [];
  101. foreach ($data['children'] as $k => $v) {
  102. foreach ($v as $vv) {
  103. $arr[$k][] = [$vv, $data['name'][$vv]];
  104. }
  105. }
  106. return $arr;
  107. }
  108. /**
  109. * 获取地区数组 格式如下
  110. * array(
  111. * 'name' => array(
  112. * '地区id' => '地区名称',
  113. * // ..
  114. * ),
  115. * 'parent' => array(
  116. * '子地区id' => '父地区id',
  117. * // ..
  118. * ),
  119. * 'children' => array(
  120. * '父地区id' => array(
  121. * '子地区id 1',
  122. * '子地区id 2',
  123. * // ..
  124. * ),
  125. * // ..
  126. * ),
  127. * 'region' => array(array(
  128. * '华北区' => array(
  129. * '省级id 1',
  130. * '省级id 2',
  131. * // ..
  132. * ),
  133. * // ..
  134. * ),
  135. * )
  136. *
  137. * @return array
  138. */
  139. protected function getCache() {
  140. // 对象属性中有数据则返回
  141. if ($this->cachedData !== null)
  142. return $this->cachedData;
  143. // 缓存中有数据则返回
  144. if ($data = rkcache('area')) {
  145. $this->cachedData = $data;
  146. return $data;
  147. }
  148. // 查库
  149. $data = array();
  150. $area_all_array = $this->limit(false)->select();
  151. foreach ((array) $area_all_array as $a) {
  152. $data['name'][$a['area_id']] = $a['area_name'];
  153. $data['parent'][$a['area_id']] = $a['area_parent_id'];
  154. $data['children'][$a['area_parent_id']][] = $a['area_id'];
  155. if ($a['area_deep'] == 1 && $a['area_region'])
  156. $data['region'][$a['area_region']][] = $a['area_id'];
  157. }
  158. wkcache('area', $data);
  159. $this->cachedData = $data;
  160. return $data;
  161. }
  162. /**
  163. * 格式化地址, 返回格式化字符串, 省\t市\t区
  164. * @return string
  165. */
  166. public function formatAddress($address) {
  167. $topLevelAreas = $this->getTopLevelAreas();
  168. $cityProvince = $this->getCityProvince();
  169. $areaNames = $this->getAreaNames();
  170. // 过滤空格等各种不可见字符
  171. $address = trim($address);// 首先去掉头尾空格
  172. $address = preg_replace('/\s(?=\s)/', '', $address);// 接着去掉两个空格以上的
  173. $address = preg_replace('/[\n\r\t]/', '', $address);// 最后将非空格替换为没空格
  174. // 省
  175. foreach ($topLevelAreas as $key => $topValue) {
  176. $topLevelLen = strlen($topValue);
  177. // 匹配不到省, 跳过
  178. $ret = strncmp($address, $topValue, $topLevelLen);
  179. if ($ret != 0) continue;
  180. $topLevelLen = mb_strlen($topValue);
  181. // 市
  182. foreach ($cityProvince as $cityValue => $parent) {
  183. if ($parent != $key) continue;
  184. $cityValue = $areaNames[$cityValue];
  185. $cityLen = mb_strlen($cityValue);
  186. $tmp = mb_substr($address, $topLevelLen, $cityLen);
  187. // 匹配不到省, 跳过
  188. if (strcmp($tmp, $cityValue) != 0) continue;
  189. // 地区
  190. $tmp = mb_substr($address, $topLevelLen+$cityLen, NULL);
  191. return "{$topValue}\t{$cityValue}\t{$tmp}";
  192. }
  193. }
  194. return null;
  195. }
  196. protected $cachedData; // 地区表缓存
  197. protected $cachedTopLevelAreas; // 省名字缓存
  198. protected $cachedCityProvince; // 市id缓存
  199. }