AreaService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\sys;
  12. use app\model\sys\SysArea;
  13. use core\base\BaseAdminService;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. /**
  18. * 地区服务层
  19. * Class AreaService
  20. * @package app\service\admin\sys
  21. */
  22. class AreaService extends BaseAdminService
  23. {
  24. public static $cache_tag_name = 'area_cache';
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->model = new SysArea();
  29. }
  30. /**
  31. * 获取地区信息
  32. * @param int $pid //上级pid
  33. * @return mixed
  34. * @throws DataNotFoundException
  35. * @throws DbException
  36. * @throws ModelNotFoundException
  37. */
  38. public function getListByPid(int $pid = 0)
  39. {
  40. $cache_name = self::$cache_tag_name.'_pid_'.$pid;
  41. return cache_remember(
  42. $cache_name,
  43. function() use($pid) {
  44. return $this->model->where([['pid', '=', $pid]])->field('id, pid, name, shortname, longitude, latitude, level, sort, status')->select()->toArray();
  45. },
  46. [self::$cache_tag_name]
  47. );
  48. }
  49. /**
  50. * 查询地区树列表
  51. * @param int $level //层级1,2,3
  52. * @return mixed
  53. * @throws DataNotFoundException
  54. * @throws DbException
  55. * @throws ModelNotFoundException
  56. */
  57. public function getAreaTree(int $level = 3)
  58. {
  59. $cache_name = self::$cache_tag_name.'_tree_'.$level;
  60. return cache_remember(
  61. $cache_name,
  62. function() use($level) {
  63. $list = $this->model->where([['level', '<=', $level]])->field('id, pid, name, shortname, longitude, latitude, level, sort, status')->select()->toArray();
  64. return list_to_tree($list);
  65. },
  66. [self::$cache_tag_name]
  67. );
  68. }
  69. public function getAreaByAreaCode($id) {
  70. $cache_name = self::$cache_tag_name.'_area_'. $id;
  71. return cache_remember(
  72. $cache_name,
  73. function() use($id) {
  74. $level = [1 => 'province', 2 => 'city', 3 => 'district'];
  75. $tree = [];
  76. $area = $this->model->where([ ['id', '=', $id] ])->field('id,level,pid,name')->findOrEmpty();
  77. if (!$area->isEmpty()) {
  78. $tree[ $level[ $area['level'] ] ] = $area->toArray();
  79. while ($area['level'] > 1) {
  80. $area = $this->model->where([ ['id', '=', $area['pid'] ] ])->field('id,level,pid,name')->findOrEmpty();
  81. $tree[ $level[ $area['level'] ] ] = $area->toArray();
  82. }
  83. }
  84. return $tree;
  85. },
  86. [self::$cache_tag_name]
  87. );
  88. }
  89. /**
  90. * @param string $address
  91. * @return int|mixed
  92. * 地址解析
  93. */
  94. public function getAddress(string $address){
  95. $map = (new ConfigService())->getMap();
  96. $url = "https://apis.map.qq.com/ws/geocoder/v1/?address=".$address."&key=".$map['key'];
  97. $curl = curl_init();
  98. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  99. curl_setopt($curl, CURLOPT_HEADER, 0);
  100. curl_setopt($curl, CURLOPT_URL, $url);
  101. curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  102. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  104. $res = curl_exec($curl);
  105. $res = json_decode($res, true);
  106. if($res){
  107. curl_close($curl);
  108. return $res;
  109. }else {
  110. $error = curl_errno($curl);
  111. curl_close($curl);
  112. return $error;
  113. }
  114. }
  115. /**
  116. * @param string $location
  117. * @return int|mixed
  118. * 逆地址解析
  119. */
  120. public function getAddressInfo(string $location){
  121. $map = (new ConfigService())->getMap();
  122. $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=".$location."&key=".$map['key'];
  123. $curl = curl_init();
  124. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  125. curl_setopt($curl, CURLOPT_HEADER, 0);
  126. curl_setopt($curl, CURLOPT_URL, $url);
  127. curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  128. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  129. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  130. $res = curl_exec($curl);
  131. $res = json_decode($res, true);
  132. if($res){
  133. curl_close($curl);
  134. return $res;
  135. }else {
  136. $error = curl_errno($curl);
  137. curl_close($curl);
  138. return $error;
  139. }
  140. }
  141. public function getAreaId($name, $level){
  142. $field = 'id';
  143. $info = $this->model->field($field)->where([['name', 'like', '%' . $name . '%' ], ['level', '=', $level]])->findOrEmpty()->toArray();
  144. return $info['id'];
  145. }
  146. /**
  147. * 获取地址名称
  148. */
  149. public function getAreaName($id){
  150. $info = $this->model->field("name")->where([['id', '=', $id ]])->findOrEmpty()->toArray();
  151. return $info['name'];
  152. }
  153. }