OperatingcityController.php 10 KB

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