OperatingcityController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Geocoder;
  5. use app\model\Config;
  6. use app\model\Operatingcity;
  7. use app\model\HousingEstate;
  8. use app\model\Tuanzhang;
  9. use app\model\Address;
  10. use think\facade\Db;
  11. class OperatingcityController extends Base
  12. {
  13. public function getcity()
  14. {
  15. $postdata = input('post.');
  16. $Configtuanzhang = Config::getconfig('tuanzhang');
  17. $configoperatingcity = Config::getconfig('operatingcity');
  18. $Configdata = Config::getconfig();
  19. $is_nulldate = 0;
  20. /*
  21. if ($this->userInfo['city_info_expire_time']) {
  22. //10分钟内不重新获取
  23. if ($this->userInfo['city_info_expire_time'] + 600 < time() || empty($this->userInfo['cityinfo']['cityName'])) {
  24. $this->userInfo['cityinfo'] = [];
  25. $this->userInfo['city_info_expire_time'] = time();
  26. }
  27. } else {
  28. $this->userInfo['cityinfo'] = [];
  29. $this->userInfo['city_info_expire_time'] = time();
  30. }*/
  31. if (!empty($postdata['he_id'])) {
  32. $this->userInfo['cityinfo']['he_id'] = $postdata['he_id'];
  33. }
  34. if (!empty($postdata['tz_id'])) {
  35. $this->userInfo['cityinfo']['tz_id'] = $postdata['tz_id'];
  36. $Tuanzhang = Tuanzhang::find($postdata['tz_id']);
  37. if (!empty($Tuanzhang)) {
  38. $Tuanzhang = $Tuanzhang->toArray();
  39. $this->userInfo['cityinfo']['tz_id'] = $Tuanzhang['id'];
  40. $this->userInfo['cityinfo']['tuanzhangName'] = $Tuanzhang['title'];
  41. $this->userInfo['cityinfo']['community_title'] = $Tuanzhang['community_title'];
  42. $this->userInfo['cityinfo']['tuanzhangtouxiang'] = toimg($Tuanzhang['touxiang']);
  43. }
  44. }
  45. if (!empty($postdata['city_id'])) {
  46. $Operatingcity = Operatingcity::find($postdata['city_id']);
  47. if (!empty($Operatingcity)) {
  48. $Operatingcity = $Operatingcity->toArray();
  49. unset($this->userInfo['cityinfo']['he_id']);
  50. unset($this->userInfo['cityinfo']['tuanzhangName']);
  51. unset($this->userInfo['cityinfo']['community_title']);
  52. unset($this->userInfo['cityinfo']['tuanzhangtouxiang']);
  53. unset($this->userInfo['cityinfo']['tz_id']);
  54. $this->userInfo['cityinfo']['ocid'] = $Operatingcity['id'];
  55. $this->userInfo['cityinfo']['province_name'] = $Operatingcity['province_name'];
  56. $this->userInfo['cityinfo']['city_name'] = $Operatingcity['city_name'];
  57. $this->userInfo['cityinfo']['district_name'] = $Operatingcity['district_name'];
  58. $this->userInfo['cityinfo']['cityName'] = $this->userInfo['cityinfo']['city_name'];
  59. $this->userInfo['cityinfo']['latitude'] = $Operatingcity['latitude'];
  60. $this->userInfo['cityinfo']['longitude'] = $Operatingcity['longitude'];
  61. }
  62. } elseif (!empty($postdata['he_id']) && $Configtuanzhang['tuanzhang_open'] == 1) {
  63. $HousingEstate = HousingEstate::find($postdata['he_id']);
  64. if (!empty($HousingEstate)) {
  65. $HousingEstate = $HousingEstate->toArray();
  66. $this->userInfo['cityinfo']['he_id'] = $postdata['he_id'];
  67. $this->userInfo['cityinfo']['housingName'] = $HousingEstate['title'];
  68. $this->userInfo['cityinfo']['province_name'] = $HousingEstate['province_name'];
  69. $this->userInfo['cityinfo']['city_name'] = $HousingEstate['city_name'];
  70. $this->userInfo['cityinfo']['district_name'] = $HousingEstate['district_name'];
  71. $this->userInfo['cityinfo']['latitude'] = $HousingEstate['latitude'];
  72. $this->userInfo['cityinfo']['longitude'] = $HousingEstate['longitude'];
  73. $Tuanzhang = Tuanzhang::find($HousingEstate['tzid']);
  74. if (!empty($Tuanzhang)) {
  75. $Tuanzhang = $Tuanzhang->toArray();
  76. $this->userInfo['cityinfo']['tz_id'] = $Tuanzhang['id'];
  77. $this->userInfo['cityinfo']['tuanzhangName'] = $Tuanzhang['title'];
  78. $this->userInfo['cityinfo']['community_title'] = $Tuanzhang['community_title'];
  79. $this->userInfo['cityinfo']['tuanzhangtouxiang'] = toimg($Tuanzhang['touxiang']);
  80. }
  81. }
  82. } elseif (!empty($postdata['latitude']) && !empty($postdata['longitude']) && empty($this->userInfo['cityinfo']['ocid'])) {
  83. $this->userInfo['cityinfo']['latitude'] = $postdata['latitude'];
  84. $this->userInfo['cityinfo']['longitude'] = $postdata['longitude'];
  85. if (!empty($postdata['address'])) {
  86. $resarea = Address::Address2area($postdata['address']);
  87. }
  88. if ($resarea['province_name'] && $resarea['city_name'] && $resarea['district_name']) {
  89. $this->userInfo['cityinfo']['province_name'] = $resarea['province_name'];
  90. $this->userInfo['cityinfo']['city_name'] = $resarea['city_name'];
  91. $this->userInfo['cityinfo']['district_name'] = $resarea['district_name'];
  92. } else {
  93. $data = Geocoder::reverse_geocoding($postdata['latitude'], $postdata['longitude']);
  94. $this->userInfo['cityinfo']['province_name'] = $data['addressComponent']['province'];
  95. $this->userInfo['cityinfo']['city_name'] = $data['addressComponent']['city'];;
  96. $this->userInfo['cityinfo']['district_name'] = $data['addressComponent']['district'];
  97. }
  98. }
  99. $this->userInfo['cityinfo']['cityName'] = $this->userInfo['cityinfo']['city_name'];
  100. if ($Configdata['locationscope'] == 3 && $this->userInfo['cityinfo']['district_name']) {
  101. $this->userInfo['cityinfo']['cityName'] = $this->userInfo['cityinfo']['district_name'];
  102. }
  103. if ($Configdata['locationscope'] == 1 && $this->userInfo['cityinfo']['province_name']) {
  104. $this->userInfo['cityinfo']['cityName'] = $this->userInfo['cityinfo']['province_name'];
  105. }
  106. if ($Configdata['locationscope'] == 3 && $this->userInfo['cityinfo']['district_name']) {
  107. $Operatingcity = Operatingcity::where('status',1)->where('areatype', $Configdata['locationscope'])->where('district_name', $this->userInfo['cityinfo']['district_name'])->find();
  108. if (empty($Operatingcity)) {
  109. $Operatingcity = Operatingcity::where('status',1)->whereNotNull('areatype')->where('district_name', $this->userInfo['cityinfo']['district_name'])->find();
  110. }
  111. if (!empty($Operatingcity)) {
  112. $Operatingcity = $Operatingcity->toArray();
  113. $this->userInfo['cityinfo']['ocid'] = $Operatingcity['id'];
  114. }
  115. } elseif ($Configdata['locationscope'] == 1 && $this->userInfo['cityinfo']['province_name']) {
  116. $Operatingcity = Operatingcity::where('status',1)->where('areatype', $Configdata['locationscope'])->where('province_name', $this->userInfo['cityinfo']['province_name'])->find();
  117. if (!empty($Operatingcity)) {
  118. $Operatingcity = $Operatingcity->toArray();
  119. $this->userInfo['cityinfo']['ocid'] = $Operatingcity['id'];
  120. }
  121. } else {
  122. if ($this->userInfo['cityinfo']['city_name']) {
  123. $Operatingcity = Operatingcity::where('status',1)->where('areatype', $Configdata['locationscope'])->where('city_name', $this->userInfo['cityinfo']['city_name'])->find();
  124. if (!empty($Operatingcity)) {
  125. $Operatingcity = $Operatingcity->toArray();
  126. $this->userInfo['cityinfo']['ocid'] = $Operatingcity['id'];
  127. }
  128. }
  129. }
  130. $this->setAppToken($this->userInfo, $this->getAppToken());
  131. if ($postdata['is_index'] == 1 && $Configdata['is_close_getposition'] == 1) {
  132. $is_close_getposition = 1;
  133. } else {
  134. $is_close_getposition = 0;
  135. }
  136. if ($this->userInfo['cityinfo']['cityName'] == null) {
  137. unset($this->userInfo['cityinfo']['cityName']);
  138. }
  139. if (empty($this->userInfo['cityinfo']['cityName'])) {
  140. unset($this->userInfo['cityinfo']['cityName']);
  141. }
  142. if (!empty($this->userInfo['cityinfo'])) {
  143. $this->userInfo['cityinfo']['locationscope'] = $Configdata['locationscope'];
  144. $is_nulldate = 1;
  145. }
  146. $this->userInfo['cityinfo']['operatingcityis_limit'] = $configoperatingcity['is_limit'];
  147. return $this->json([
  148. 'data' => $this->userInfo['cityinfo'],
  149. 'is_nulldate' => $is_nulldate,
  150. 'is_close_getposition' => $is_close_getposition
  151. ]);
  152. }
  153. public function list()
  154. {
  155. $keyword = input('post.keyword', '', 'serach_in');
  156. $config = Config::getconfig();
  157. if (empty($config['locationscope'])) {
  158. $config['locationscope'] = 3;
  159. }
  160. $latitude = input('post.latitude', '', 'serach_in'); //纬度信息
  161. $longitude = input('post.longitude', '', 'serach_in'); //经度信息
  162. if (!empty($longitude) && !empty($latitude)) {
  163. $sql = "select * from (select id,weid,sort,title,areatype,latitude,longitude,province_name,city_name,district_name,area_name,house_number,status, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from " . (new Operatingcity)->getTable() . " order by distance asc ) as a where status=1 ";
  164. } else {
  165. $sql = "select * from " . (new Operatingcity)->getTable() . " where status=1 ";
  166. }
  167. $sql .= " and `weid` = " . weid();
  168. $sql .= " and `areatype` = " . $config['locationscope'];
  169. if (trim($keyword)) {
  170. $sql .= " and `title` LIKE '%" . $keyword . "%'";
  171. }
  172. //var_dump($sql);
  173. $data = Db::query($sql);
  174. foreach ($data as &$vo) {
  175. $vo['image'] = toimg($vo['image']);
  176. $vo['distance'] = round(($vo['distance']) / 1000, 1);
  177. }
  178. $res['data'] = $data;
  179. $res['sql'] = $sql;
  180. return $this->json(['data' => $res]);
  181. }
  182. }