Address.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use extend\api\HttpClient;
  12. use think\facade\Cache;
  13. use app\model\BaseModel;
  14. use think\facade\Db;
  15. /**
  16. * 地区表
  17. */
  18. class Address extends BaseModel
  19. {
  20. /**
  21. * 获取地区列表
  22. * @param unknown $condition
  23. * @param string $field
  24. * @param string $order
  25. * @param string $limit
  26. * @return multitype:string mixed
  27. */
  28. public function getAreaList($condition = [], $field = '*', $order = '', $limit = null)
  29. {
  30. $data = json_encode([$condition, $field, $order, $limit]);
  31. $cache = Cache::get("area_getAreaList_" . $data);
  32. if (!empty($cache)) {
  33. return $this->success($cache);
  34. }
  35. $area_list = model("area")->getList($condition, $field, $order, $limit);
  36. Cache::tag("area")->set("area_getAreaList_" . $data, $area_list);
  37. return $this->success($area_list);
  38. }
  39. /**
  40. * 获取地区详情
  41. */
  42. public function getAreaInfo($circle)
  43. {
  44. $cache = Cache::get("area_getAreaInfo_" . $circle);
  45. if (!empty($cache)) {
  46. return $this->success($cache);
  47. }
  48. $info = model("area")->getInfo([['id', '=', $circle]]);
  49. Cache::tag("area")->set("area_getAreaInfo_" . $circle, $info);
  50. return $this->success($info);
  51. }
  52. /**
  53. * 获取地区数量
  54. * @param $condition
  55. * @return array
  56. */
  57. public function getAreaCount($condition){
  58. $count = model("area")->getCount($condition);
  59. return $this->success($count);
  60. }
  61. /**
  62. * 获取省市子项
  63. */
  64. public function getAreas($circle = 0)
  65. {
  66. $cache = Cache::get("area_getAreas_" . $circle);
  67. if (!empty($cache)) {
  68. return $this->success($cache);
  69. }
  70. $list = model("area")->getList([['pid', '=', $circle]]);
  71. Cache::tag("area")->set("area_getAreas_" . $circle, $list);
  72. return $this->success($list);
  73. }
  74. /**
  75. * 获取整理后的地址
  76. */
  77. public function getAddressTree($level = 4)
  78. {
  79. $condition = [['level', '<=', $level]];
  80. $json_condition = json_encode($condition);
  81. $cache = Cache::get("area_getAddressTree" . $json_condition);
  82. if (!empty($cache)) {
  83. return $this->success($cache);
  84. }
  85. $area_list = $this->getAreaList($condition, "id, pid, name, level", "id asc")['data'];
  86. //组装数据
  87. $refer_list = [];
  88. foreach ($area_list as $key => $val) {
  89. $refer_list[$val['level']][$val['pid']]['child_list'][$val['id']] = $area_list[$key];
  90. if (isset($refer_list[$val['level']][$val['pid']]['child_num'])) {
  91. $refer_list[$val['level']][$val['pid']]['child_num'] += 1;
  92. } else {
  93. $refer_list[$val['level']][$val['pid']]['child_num'] = 1;
  94. }
  95. }
  96. Cache::tag("area")->set("area_getAddressTree" . $json_condition, $refer_list);
  97. return $this->success($refer_list);
  98. }
  99. /**
  100. * 获取地址树结构
  101. * @param $level
  102. * @return array
  103. */
  104. public function getAddressTreeList($level){
  105. $condition = [['level', '<=', $level]];
  106. $json_condition = json_encode($condition);
  107. $cache = Cache::get("area_getAddressTreeList" . $json_condition);
  108. if (!empty($cache)) {
  109. return $this->success($cache);
  110. }
  111. $area_list = $this->getAreaList($condition, "id, pid, name", "id asc")['data'];
  112. $tree = $this->toTree($area_list);
  113. Cache::tag("area")->set("area_getAddressTreeList" . $json_condition, $tree);
  114. return $this->success($tree);
  115. }
  116. /**
  117. * 列表转树结构
  118. * @param $array
  119. * @param int $pid
  120. * @return array
  121. */
  122. public function toTree($array, $pid = 0){
  123. $tree = array();
  124. foreach ($array as $key => $value) {
  125. if ($value['pid'] == $pid) {
  126. $value['children'] = $this->toTree($array, $value['id']);
  127. $tree[] = $value;
  128. }
  129. }
  130. return $tree;
  131. }
  132. /**
  133. * 获取地址
  134. * @param array $condition
  135. * @param string $field
  136. * @return multitype:number unknown
  137. */
  138. public function getAreasInfo(array $condition, string $field = '*')
  139. {
  140. $info = model("area")->getInfo($condition, $field);
  141. if ($info) return $this->success($info);
  142. return $this->error();
  143. }
  144. /**
  145. * 通过地址查询
  146. */
  147. public function getAddressByLatlng($post_data)
  148. {
  149. $post_url = 'https://apis.map.qq.com/ws/geocoder/v1/';
  150. $config_model = new \app\model\web\Config();
  151. $config_result = $config_model->getMapConfig()['data'] ?? [];
  152. $config = $config_result['value'] ?? [];
  153. $tencent_map_key = $config['tencent_map_key'] ?? '';
  154. $post_data = array(
  155. 'location' => $post_data['latlng'],
  156. 'key' => $tencent_map_key,
  157. 'get_poi' => 0,//是否返回周边POI列表:1.返回;0不返回(默认)
  158. );
  159. $httpClient = new HttpClient();
  160. $res = $httpClient->post($post_url, $post_data);
  161. $res = json_decode($res, true);
  162. if($res['status'] == 0){
  163. $return_array = $res['result']['address_component'] ?? [];
  164. $return_data = array(
  165. 'province' => $return_array['province'] ?? '',
  166. 'city' => $return_array['city'] ?? '',
  167. 'district' => $return_array['district'] ?? '',
  168. 'address' => $return_array['street_number'] ?? '',
  169. 'full_address' => $res['result']['address'] ?? ''
  170. );
  171. return $this->success($return_data);
  172. }else{
  173. return $this->error([], $res['message']);
  174. }
  175. }
  176. /**
  177. * 通过地址查询
  178. */
  179. public function getAddressByName($address)
  180. {
  181. $post_url = 'https://apis.map.qq.com/ws/geocoder/v1/';
  182. $config_model = new \app\model\web\Config();
  183. $config_result = $config_model->getMapConfig()['data'] ?? [];
  184. $config = $config_result['value'] ?? [];
  185. $tencent_map_key = $config['tencent_map_key'] ?? '';
  186. $post_data = array(
  187. 'address' => $address,
  188. 'key' => $tencent_map_key,
  189. );
  190. $httpClient = new HttpClient();
  191. $res = $httpClient->post($post_url, $post_data);
  192. $res = json_decode($res, true);
  193. if($res['status'] == 0){
  194. $return_array = $res['result']['location'] ?? [];
  195. $return_data = array(
  196. 'longitude' => $return_array['lng'] ?? '',
  197. 'latitude' => $return_array['lat'] ?? '',
  198. );
  199. return $this->success($return_data);
  200. }else{
  201. return $this->error([], $res['message']);
  202. }
  203. }
  204. /**
  205. * 编辑地区
  206. * @param $data
  207. * @return array
  208. */
  209. public function saveArea($data){
  210. $count = model('area')->getCount([ ['id', '=', $data['id'] ] ]);
  211. if ($count) {
  212. unset($data['id']);
  213. $res = model('area')->update($data, [ ['id', '=', $data['id'] ] ]);
  214. } else {
  215. $res = model('area')->add($data);
  216. }
  217. if ($res) {
  218. Cache::clear("area");
  219. return $this->success($res);
  220. }
  221. return $this->error();
  222. }
  223. /**
  224. * 删除地区
  225. * @param $condition
  226. * @return array
  227. */
  228. public function deleteArae($id, $level){
  229. switch ((int)$level) {
  230. case 1:
  231. $child = model('area')->getColumn([ ['pid', '=', $id] ], 'id');
  232. if (empty($child)) {
  233. $condition = [ ['id', '=', $id], ['level', '=', $level] ];
  234. } else {
  235. $child = implode(',', $child);
  236. $condition = [ ['', 'exp', Db::raw("(id = $id AND level = $level) OR (id in ($child) AND level = 2) OR (pid in ($child) AND level = 3)") ]];
  237. }
  238. break;
  239. case 2:
  240. $condition = [ ['', 'exp', Db::raw("(id = $id AND level = 2) OR (pid = $id AND level = 3)") ]];
  241. break;
  242. case 3:
  243. $condition = [ ['id', '=', $id], ['level', '=', $level] ];
  244. break;
  245. }
  246. $res = model('area')->delete($condition);
  247. if ($res) {
  248. Cache::clear("area");
  249. return $this->success($res);
  250. }
  251. return $this->error();
  252. }
  253. }