AreaService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\sys;
  12. use app\model\sys\SysArea;
  13. use app\service\admin\sys\ConfigService;
  14. use core\base\BaseApiService;
  15. use core\exception\ApiException;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. /**
  20. * 地区服务层
  21. * Class AreaService
  22. * @package app\service\admin\sys
  23. */
  24. class AreaService extends BaseApiService
  25. {
  26. public static $cache_tag_name = 'area_cache';
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $this->model = new SysArea();
  31. }
  32. /**
  33. * 获取地区信息
  34. * @param int $pid //上级pid
  35. * @return mixed
  36. * @throws DataNotFoundException
  37. * @throws DbException
  38. * @throws ModelNotFoundException
  39. */
  40. public function getListByPid(int $pid = 0)
  41. {
  42. $cache_name = self::$cache_tag_name . '_api_pid_' . $pid;
  43. return cache_remember(
  44. $cache_name,
  45. function() use ($pid) {
  46. return $this->model->where([ [ 'pid', '=', $pid ] ])->field('id, name')->select()->toArray();
  47. },
  48. [ self::$cache_tag_name ]
  49. );
  50. }
  51. /**
  52. * 查询地区树列表
  53. * @param int $level //层级1,2,3
  54. * @return mixed
  55. * @throws DataNotFoundException
  56. * @throws DbException
  57. * @throws ModelNotFoundException
  58. */
  59. public function getAreaTree(int $level = 3)
  60. {
  61. $cache_name = self::$cache_tag_name . '_api_tree_' . $level;
  62. return cache_remember(
  63. $cache_name,
  64. function() use ($level) {
  65. $list = $this->model->where([ [ 'level', '<=', $level ] ])->field('id, pid, name')->select()->toArray();
  66. return list_to_tree($list);
  67. },
  68. [ self::$cache_tag_name ]
  69. );
  70. }
  71. public function getAreaByAreaCode($id)
  72. {
  73. $cache_name = self::$cache_tag_name . '_api_area_' . $id;
  74. return cache_remember(
  75. $cache_name,
  76. function() use ($id) {
  77. $level = [ 1 => 'province', 2 => 'city', 3 => 'district' ];
  78. $tree = [];
  79. $area = $this->model->where([ [ 'id', '=', $id ] ])->field('id,level,pid,name')->findOrEmpty();
  80. if (!$area->isEmpty()) {
  81. $tree[ $level[ $area[ 'level' ] ] ] = $area->toArray();
  82. while ($area[ 'level' ] > 1) {
  83. $area = $this->model->where([ [ 'id', '=', $area[ 'pid' ] ] ])->field('id,level,pid,name')->findOrEmpty();
  84. $tree[ $level[ $area[ 'level' ] ] ] = $area->toArray();
  85. }
  86. }
  87. return $tree;
  88. },
  89. [ self::$cache_tag_name ]
  90. );
  91. }
  92. /**
  93. * 通过经纬度查询地址
  94. * @param $params
  95. * @return array|int
  96. */
  97. public function getAddressByLatlng($params)
  98. {
  99. $url = 'https://apis.map.qq.com/ws/geocoder/v1/';
  100. $map = ( new ConfigService() )->getMap();
  101. $get_data = array (
  102. 'location' => $params[ 'latlng' ],
  103. 'key' => $map[ 'key' ],
  104. 'get_poi' => 0,//是否返回周边POI列表:1.返回;0不返回(默认)
  105. );
  106. $url = $url . '?' . http_build_query($get_data);
  107. $curl = curl_init();
  108. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  109. curl_setopt($curl, CURLOPT_HEADER, 0);
  110. curl_setopt($curl, CURLOPT_URL, $url);
  111. curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  112. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  113. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  114. $res = curl_exec($curl);
  115. $res = json_decode($res, true);
  116. if ($res) {
  117. curl_close($curl);
  118. if ($res[ 'status' ] == 0) {
  119. $return_array = $res[ 'result' ][ 'address_component' ] ?? [];
  120. $address_data = array (
  121. 'province' => $return_array[ 'province' ] ?? '',
  122. 'city' => $return_array[ 'city' ] ?? '',
  123. 'district' => $return_array[ 'district' ] ?? '',
  124. 'address' => $return_array[ 'street_number' ] ?? '',
  125. 'full_address' => $res[ 'result' ][ 'address' ] ?? '',
  126. 'formatted_addresses' => $res[ 'result' ][ 'formatted_addresses' ] ?? []
  127. );
  128. $province = '';
  129. if ($address_data[ 'province' ]) {
  130. $province = str_replace('省', '', $address_data[ 'province' ]);
  131. $province = str_replace('市', '', $province);
  132. }
  133. $city = $address_data[ 'city' ] ?? '';
  134. $district = $address_data[ 'district' ] ?? '';
  135. $province_info = $this->model->where([ [ 'name', 'like', '%' . $province . '%' ], [ 'level', '=', 1 ] ])->field('id,name')->select()->toArray()[ 0 ] ?? [];
  136. $province_id = 0;
  137. $province_name = '';
  138. $city_id = 0;
  139. $city_name = '';
  140. $district_id = 0;
  141. $district_name = '';
  142. if (!empty($province_info)) {
  143. $province_id = $province_info[ 'id' ];
  144. $province_name = $province_info[ 'name' ];
  145. }
  146. if ($province_id > 0) {
  147. $city_info = $this->model->where([ [ 'name', 'like', '%' . $city . '%' ], [ 'level', '=', 2 ], [ 'pid', '=', $province_id ] ])->field('id,name')->select()->toArray()[ 0 ] ?? [];
  148. if (!empty($city_info)) {
  149. $city_id = $city_info[ 'id' ];
  150. $city_name = $city_info[ 'name' ];
  151. }
  152. }
  153. if ($city_id > 0 && $province_id > 0) {
  154. $district_info = $this->model->where([ [ 'name', 'like', '%' . $district . '%' ], [ 'level', '=', 3 ], [ 'pid', '=', $city_id ] ])->field('id,name')->select()->toArray()[ 0 ] ?? [];
  155. if (!empty($district_info)) {
  156. $district_id = $district_info[ 'id' ];
  157. $district_name = $district_info[ 'name' ];
  158. }
  159. }
  160. return [
  161. 'province_id' => $province_id,
  162. 'province' => $province_name,
  163. 'city_id' => $city_id,
  164. 'city' => $city_name,
  165. 'district_id' => $district_id,
  166. 'district' => $district_name,
  167. 'full_address' => $address_data[ 'full_address' ],
  168. 'formatted_addresses' => $address_data[ 'formatted_addresses' ]
  169. ];
  170. } else {
  171. throw new ApiException($res[ 'message' ]);
  172. }
  173. } else {
  174. $error = curl_errno($curl);
  175. curl_close($curl);
  176. throw new ApiException($error);
  177. }
  178. }
  179. }