IndexController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\AreaFilter;
  4. use App\Filters\BikeFilter;
  5. use App\Handlers\BikeStatusInfoSyncHandler;
  6. use App\Http\Resources\App\BikeResource;
  7. use App\Models\Area;
  8. use App\Models\Bike;
  9. use App\Models\Parking;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Log;
  12. class IndexController extends AppBaseController
  13. {
  14. /**
  15. * index 首页 获取区域
  16. *
  17. * @return \Illuminate\Http\JsonResponse
  18. * @author Fx
  19. *
  20. */
  21. public function index()
  22. {
  23. $area = Area::query()->whereIn('id', self::$areaIds)->pluck('name', 'id');
  24. $wx_area = Area::query()->whereIn('id', self::$areaIds)->get()->toArray();
  25. $wx_data = [];
  26. if (!empty($wx_area)) {
  27. foreach ($wx_area as $v) {
  28. $wx_data[] = ['areaID' => $v['id'], 'text' => $v['name']];
  29. }
  30. }
  31. $bikeStates = Bike::$bikeStatesMaps;
  32. $wx_bikeStates = [];
  33. foreach ($bikeStates as $k => $v) {
  34. $wx_bikeStates[] = ['areaID' => $k, 'text' => $v];
  35. }
  36. $data = [
  37. 'area' => $area,
  38. 'wx_area' => $wx_data,
  39. 'bike_states' => $bikeStates,
  40. 'wx_bikeStates' => $wx_bikeStates,
  41. ];
  42. return $this->ok($data);
  43. }
  44. /**
  45. * getBikes 首页搜索车辆
  46. *
  47. * @param BikeFilter $filter
  48. * @return \Illuminate\Http\JsonResponse
  49. * @author Fx
  50. *
  51. */
  52. public function getBikes(BikeFilter $filter)
  53. {
  54. $bikes = Bike::query()
  55. ->whereIn('put_area_id', self::$areaIds)
  56. ->whereNotNull('box_no')
  57. ->filter($filter)
  58. ->orderByDesc('id')
  59. ->get();
  60. $worker_bikes = app()->redis->hkeys(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG);
  61. $newBikes = [];
  62. foreach ($bikes as $key => $bike) {
  63. $location = $bike->last_location ? [json_decode($bike->last_location)->lng, json_decode($bike->last_location)->lat] : [113.801689, 34.815298];
  64. $newBike = [
  65. // 'bike_id' => $bike->id,
  66. 'id' => "B" . $bike->bike_no,
  67. 'iconPath' => '',
  68. 'typer' => '',
  69. 'width' => 28,
  70. 'height' => 28,
  71. 'zIndex' => 1111,
  72. 'latitude' => $location[1],
  73. 'longitude' => $location[0],
  74. ];
  75. if (is_array($worker_bikes) && in_array($bike->bike_no, $worker_bikes)) {
  76. $newBike['iconPath'] = self::$url . '/bike_yunwei/work.png';
  77. $newBike['typer'] = 'bike_worker_riding';
  78. } elseif (!(bool)$bike->is_link) {
  79. // 离线
  80. $newBike['iconPath'] = self::$url . '/bike_yunwei/outLi.png';
  81. $newBike['typer'] = 'lixian';
  82. } elseif ((bool)$bike->is_trouble) {
  83. // 故障上线
  84. if ((bool)$bike->put_status) {
  85. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/repair_xbm.png';
  86. $newBike['typer'] = 'bike_trouble';
  87. } else {
  88. // 故障下线
  89. $newBike['iconPath'] = self::$url . '/trobole.png';
  90. $newBike['typer'] = 'trouble';
  91. }
  92. } elseif ((bool)$bike->is_riding) {
  93. //骑行中
  94. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/riding_xbm.png';
  95. $newBike['typer'] = 'bike_riding';
  96. } elseif (!(bool)$bike->is_low_battery_power) {
  97. // 低电量
  98. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/lowPower_xbm.png';
  99. $newBike['typer'] = 'bike_low_power';
  100. } elseif (!(bool)$bike->put_status) {
  101. // 下线 未投放
  102. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/outline_xbm.png';
  103. $newBike['typer'] = 'bike_off_line';
  104. } elseif (!(bool)$bike->is_in_parking) {
  105. // 不在停车区
  106. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/noInPark_xbm.png';
  107. $newBike['typer'] = 'bike_not_in_parking';
  108. } else {
  109. // 未骑行
  110. $newBike['iconPath'] = self::$url . '/xiaobanma_yunwei/noUser_xbm.png';
  111. $newBike['typer'] = 'bike_not_riding';
  112. }
  113. $newBikes[] = $newBike;
  114. }
  115. return $this->ok($newBikes);
  116. }
  117. /**
  118. * searchBikes 首页搜索
  119. *
  120. * @param BikeFilter $filter
  121. * @return \Illuminate\Http\JsonResponse
  122. * @author Fx
  123. *
  124. */
  125. public function searchBikes(BikeFilter $filter)
  126. {
  127. $bike = Bike::query()
  128. ->whereIn('put_area_id', self::$areaIds)
  129. ->filter($filter)
  130. ->orderByDesc('id')
  131. ->get();
  132. return $this->ok(BikeResource::collection($bike));
  133. }
  134. /**
  135. * getAreas 还车点 骑行区
  136. *
  137. * @param Request $request
  138. * @return \Illuminate\Http\JsonResponse
  139. * @author Fx
  140. *
  141. */
  142. public function getAreas(Request $request, AreaFilter $areaFilter)
  143. {
  144. $areaIds = self::$areaIds;
  145. // 骑行区
  146. $ridding_area = Area::query()->filter($areaFilter)->whereIn('id', $areaIds);
  147. $is_with_parking = $request->get('is_with_parking', 1);
  148. if ($is_with_parking) {
  149. // 停车点
  150. $parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_STOP_BIKE)->where('status', Parking::STATUS_OK); //2为停车区
  151. //禁停区
  152. $no_parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_NO_STOP_BIKE)->where('status', Parking::STATUS_OK); //1为禁停
  153. }
  154. $area_id = $request->get('put_area_id') ?? '';
  155. if (!empty($area_id)) {
  156. $ridding_area = $ridding_area->where('id', $area_id);
  157. if ($is_with_parking) {
  158. $parking_area = $parking_area->where('area_id', $area_id);
  159. $no_parking_area = $no_parking_area->where('area_id', $area_id);
  160. }
  161. }
  162. $ridding_area = $ridding_area->get();
  163. $ridding_areas = [];
  164. foreach ($ridding_area as $v) {
  165. $area = [
  166. 'points' => json_decode($v->wx_area_fence),
  167. 'strokeWidth' => 4,
  168. 'strokeColor' => '#0000FF',
  169. 'fillColor' => '#0000FF10',
  170. 'zIndex' => 999,
  171. 'id' => 'A' . $v->id
  172. ];
  173. $ridding_areas[] = $area;
  174. }
  175. if (!$is_with_parking) {
  176. $data = [
  177. 'ridding_area' => $ridding_areas,
  178. ];
  179. return $this->ok($data);
  180. }
  181. $parking_area = $parking_area->orderByDesc('id')->get();
  182. $no_parking_area = $no_parking_area->orderByDesc('id')->get();
  183. $parink_areas = [];
  184. $parink_points = [];
  185. foreach ($parking_area as $key => $v1) {
  186. $parink_area = [
  187. 'points' => json_decode($v1->wx_parking_fence),
  188. 'strokeWidth' => 4,
  189. 'strokeColor' => '#ff00ff',
  190. 'fillColor' => '#ff00ff22',
  191. 'zIndex' => $key,
  192. 'id' => 'P' . $v1->id
  193. ];
  194. $center = json_decode($v1->wx_parking_centre, true);
  195. $point = [
  196. 'latitude' => $center['latitude'],
  197. 'longitude' => $center['longitude'],
  198. 'zIndex' => $key,
  199. 'id' => 'p' . $v1->id,
  200. 'width' => 24,
  201. 'height' => 28,
  202. 'iconPath' => self::$url . '/yunwei/parkShow.png',
  203. ];
  204. $parink_areas[] = $parink_area;
  205. $parink_points[] = $point;
  206. }
  207. $no_parink_areas = [];
  208. $no_parink_points = [];
  209. foreach ($no_parking_area as $key => $v2) {
  210. $parink_area = [
  211. 'points' => json_decode($v2->wx_parking_fence),
  212. 'strokeWidth' => 4,
  213. 'strokeColor' => '#ff0000',
  214. 'fillColor' => '#ff000022',
  215. 'zIndex' => $key,
  216. 'id' => 'N' . $v2->id
  217. ];
  218. $center = json_decode($v2->wx_parking_centre, true);
  219. $point = [
  220. 'latitude' => $center['latitude'],
  221. 'longitude' => $center['longitude'],
  222. 'zIndex' => $key,
  223. 'id' => 'n' . $v2->id,
  224. 'width' => 24,
  225. 'height' => 28,
  226. 'iconPath' => self::$url . '/yunwei/forbid.png',
  227. ];
  228. $no_parink_areas[] = $parink_area;
  229. $no_parink_points[] = $point;
  230. }
  231. $data = [
  232. 'ridding_area' => $ridding_areas,
  233. 'parking_area' => $parink_areas,
  234. 'parking_points' => $parink_points,
  235. 'no_parking_area' => $no_parink_areas,
  236. 'no_parking_points' => $no_parink_points,
  237. ];
  238. return $this->ok($data);
  239. }
  240. }