ParkingController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/19
  6. * Time: 2:23 PM
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Handlers\ConvertHandler;
  10. use App\Http\Requests\AreaIdRequest;
  11. use App\Http\Requests\NearBySiteRequest;
  12. use App\Models\AreaSetting;
  13. use App\Models\Parking;
  14. use App\Repositories\AreaSettingRepository;
  15. use App\Repositories\ParkingRepository;
  16. use App\Transformers\ParkingTransformer;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. /**
  20. * 停车点模块
  21. * Class ParkingController
  22. * @package App\Http\Controllers\V1
  23. */
  24. class ParkingController extends BaseController
  25. {
  26. /**
  27. * 有区域id 获取区域停车点
  28. * @param AreaIdRequest $request
  29. * @param ParkingRepository $parkingRepository
  30. * @return
  31. * User: Mead
  32. */
  33. public function stopSite(AreaIdRequest $request, ParkingRepository $parkingRepository, AreaSettingRepository $areaSettingRepository)
  34. {
  35. try {
  36. $area_id = $request->get('area_id', false);
  37. if ($areaSettingRepository->byAreaIdGetIsWholeAreaHuanche($area_id) === AreaSetting::WHOLE_AREA_HUANCHE_NO) {
  38. $parking = $parkingRepository->byAreaIdGetAllStopSites($area_id);
  39. } else {
  40. $parking = $parkingRepository->byAreaIdGetBanStopSites($area_id);
  41. }
  42. $points = $data = [];
  43. foreach ($parking as $model) {
  44. if ($model->type == Parking::TYPE_NO_STOP_BIKE) {
  45. //禁停区
  46. $iconPath = 'http://resource.weilaibike.com/ban-stop.png';
  47. $strokeColor = '#FF0000';
  48. $fillColor = '#FF000030';
  49. $color = '#FF0000';
  50. } else if ($model->type == Parking::TYPE_STOP_BIKE) {
  51. //停车区
  52. $iconPath = 'http://resource.weilaibike.com/stop1.png';
  53. $strokeColor = '#FF00FF';
  54. $fillColor = '#FF00FF66';
  55. $color = '#FF00FF';
  56. } else {
  57. //区域
  58. $iconPath = 'http://resource.weilaibike.com/stop1.png';
  59. $strokeColor = '#FF00FF';
  60. $fillColor = '#FF00FF66';
  61. $color = '#0000FF';
  62. }
  63. $data[] = [
  64. 'name' => $model->name,
  65. // 'centre' =>
  66. 'points' => $model->parking_fence,
  67. 'radius' => $model->parking_radius,
  68. 'type' => $model->type,
  69. 'type_text' => Parking::$typeMaps[$model->type],
  70. 'strokeWidth' => 4,
  71. 'strokeColor' => $strokeColor,
  72. 'fillColor' => $fillColor,
  73. 'color' => $color,
  74. 'width' => 4
  75. ];
  76. $points[] = array_merge([
  77. 'iconPath' => $iconPath,
  78. 'name' => $model->name,
  79. 'width' => 24,
  80. 'height' => 28,
  81. 'zIndex' => $model->id,
  82. 'id' => $model->id,
  83. 'type' => $model->type,
  84. ], $model->parking_centre);
  85. }
  86. $runt = [
  87. 'centres' => $points,
  88. 'polygons' => $data
  89. ];
  90. $json = json_decode(json_encode($runt), true); //转成数组
  91. return response()->json()->setData($json);
  92. } catch (\Exception $exception) {
  93. return $this->exception($exception);
  94. }
  95. }
  96. /**
  97. * 有区域id 获取区域禁停区
  98. * @param AreaIdRequest $request
  99. * @param ParkingRepository $parkingRepository
  100. * @return
  101. * User: Mead
  102. */
  103. public function banStopSite(AreaIdRequest $request, ParkingRepository $parkingRepository)
  104. {
  105. try {
  106. $area_id = $request->get('area_id', false);
  107. $parking = $parkingRepository->byAreaIdGetBanStopSites($area_id);
  108. $data = [];
  109. $points = [];
  110. foreach ($parking as $model) {
  111. if ($model->type == Parking::TYPE_NO_STOP_BIKE) {
  112. //禁停区
  113. $iconPath = 'http://resource.weilaibike.com/ban-stop.png';
  114. $strokeColor = '#FF0000';
  115. $fillColor = '#FF000030';
  116. $color = '#FF0000';
  117. } else if ($model->type == Parking::TYPE_STOP_BIKE) {
  118. //停车区
  119. $iconPath = 'http://resource.weilaibike.com/stop1.png';
  120. $strokeColor = '#FF00FF';
  121. $fillColor = '#FF00FF66';
  122. $color = '#FF00FF';
  123. } else {
  124. //区域
  125. $iconPath = 'http://resource.weilaibike.com/stop1.png';
  126. $strokeColor = '#FF00FF';
  127. $fillColor = '#FF00FF66';
  128. $color = '#0000FF';
  129. }
  130. $data[] = [
  131. 'name' => $model->name,
  132. // 'centre' =>
  133. 'points' => $model->parking_fence,
  134. 'radius' => $model->parking_radius,
  135. 'type' => $model->type,
  136. 'type_text' => Parking::$typeMaps[$model->type],
  137. 'strokeWidth' => 4,
  138. 'strokeColor' => $strokeColor,
  139. 'fillColor' => $fillColor,
  140. 'color' => $color,
  141. 'width' => 4
  142. ];
  143. $points[] = array_merge([
  144. 'iconPath' => $iconPath,
  145. 'name' => $model->name,
  146. 'width' => 24,
  147. 'height' => 28,
  148. 'zIndex' => $model->id,
  149. 'id' => $model->id,
  150. 'type' => $model->type,
  151. ], $model->parking_centre);
  152. }
  153. $runt = [
  154. 'centres' => $points,
  155. 'polygons' => $data
  156. ];
  157. $json = json_decode(json_encode($runt), true); //转成数组
  158. return response()->json()->setData($json);
  159. } catch (\Exception $exception) {
  160. return $this->exception($exception);
  161. }
  162. }
  163. /**
  164. * 获取附近停车区
  165. * @param NearBySiteRequest $request
  166. * @param Parking $parking
  167. * User: Mead
  168. */
  169. public function nearbySite(NearBySiteRequest $request, Parking $parking, AreaSettingRepository $areaSettingRepository)
  170. {
  171. try {
  172. $lng = $request->get('lng');
  173. $lat = $request->get('lat');
  174. $location_lat = $request->get('location_lat');
  175. $location_lng = $request->get('location_lng');
  176. $area_id = $request->get('area_id');
  177. if ($areaSettingRepository->byAreaIdGetIsWholeAreaHuanche($area_id) === AreaSetting::WHOLE_AREA_HUANCHE_OK) {
  178. return $this->errorNoValidation('该运营区域内都可以停车(除禁停区内)!');
  179. }
  180. $options = ['SORT' => 'ASC', 'WITHDIST' => true, 'WITHCOORD' => true];
  181. $redis = Redis::connection();
  182. $stop_bike_stations = $redis->georadius(Parking::REDIS_STOP_BIKE_SITE_TAG . "_{$area_id}", $lng, $lat, 1, 'km', $options);
  183. $ids = array_column($stop_bike_stations, 0);
  184. $stop_bike_names = $parking->whereIn('id', $ids)->get(['id', 'name', 'description', 'parking_centre as centre', 'parking_fence as fence', 'parking_radius as radius', 'type'])->mapWithKeys(function ($item) {
  185. $item['centre'] = js2php($item['centre']);
  186. $item['fence'] = js2php($item['fence']);
  187. return [$item['id'] => $item];
  188. })->toArray();
  189. $points = [];
  190. $polygons = [];
  191. foreach ($stop_bike_stations as $bike_station) {
  192. $item = $stop_bike_names[$bike_station[0]];
  193. $item['location_distance'] = format_mileage(bcdiv((new ConvertHandler())->distance($location_lat, $location_lng, $bike_station[2][1], $bike_station[2][0]), 1000, 2));
  194. $item['distance'] = format_mileage($bike_station[1]);
  195. $iconPath = 'http://resource.weilaibike.com/stop1.png';
  196. $strokeColor = '#FF00FF';
  197. $fillColor = '#FF00FF50';
  198. $color = '#FF00FF';
  199. $polygons[] = [
  200. 'points' => $item['fence'],
  201. 'strokeWidth' => 4,
  202. 'strokeColor' => $strokeColor,
  203. 'color' => $color,
  204. 'width' => 4,
  205. 'fillColor' => $fillColor,
  206. 'zIndex' => 1
  207. ];
  208. $points[] = array_merge([
  209. 'iconPath' => $iconPath,
  210. 'width' => 24,
  211. 'height' => 28,
  212. 'zIndex' => 1111,
  213. 'id' => $item['id'],
  214. 'type' => $item['type'],
  215. 'description' => $item['description'],
  216. 'distance' => $item['distance'],
  217. 'location_distance' => $item['location_distance'],
  218. 'name' => $item['name'],
  219. ], $item['centre']);
  220. $points[0]['iconPath'] = 'http://resource.weilaibike.com/orange-stop.png';
  221. }
  222. $runt = [
  223. 'points' => $points,
  224. 'polygons' => $polygons
  225. ];
  226. $json = json_decode(json_encode($runt), true); //转成数组
  227. return response()->json()->setData($json);
  228. // return $this->response->array([
  229. // 'points' => $points,
  230. // 'polygons' => $polygons
  231. //
  232. // ]);
  233. } catch (\Exception $exception) {
  234. return $this->exception($exception);
  235. }
  236. }
  237. }