PageController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\Http\Requests\ApplyAddParkingRequest;
  10. use App\Http\Requests\HomePageRequest;
  11. use App\Models\ApplyParking;
  12. use App\Models\Area;
  13. use App\Models\Bike;
  14. use App\Models\CardRiding;
  15. use App\Models\Coupon;
  16. use App\Models\DepositOrder;
  17. use App\Models\InviteNewUsersConfig;
  18. use App\Models\InviteNewUsersGiveGiftLog;
  19. use App\Models\RechargeConfiguration;
  20. use App\Models\RefundLog;
  21. use App\Repositories\AreaRepository;
  22. use App\Repositories\CardRidingRepository;
  23. use App\Repositories\CouponRepository;
  24. use App\Repositories\InviteNewUsersConfigRepository;
  25. use App\Repositories\OrderRepository;
  26. use App\Repositories\RechargeConfigurationRepository;
  27. use App\Transformers\AreaTransformer;
  28. use App\Transformers\OrderRideStatusTransformer;
  29. use App\Transformers\RechargeConfigurationTransformer;
  30. use Dingo\Api\Http\Request;
  31. use Illuminate\Support\Facades\Log;
  32. use Illuminate\Support\Facades\Redis;
  33. class PageController extends BaseController
  34. {
  35. /**
  36. * 首页
  37. * User: Mead
  38. */
  39. public function home(HomePageRequest $request, AreaRepository $areaRepository)
  40. {
  41. $lat = (string)$request->get('lat');
  42. $lng = (string)$request->get('lng');
  43. $bike_no = (string)$request->get('bike_no', false);
  44. try {
  45. $area_id = 0;
  46. if ($bike_no) {
  47. $area_id = Bike::byBikeNoGetAreaId($bike_no);
  48. }
  49. $user = $this->user;
  50. if (!$area_id) {
  51. $options = ['SORT' => 'ASC', 'COUNT' => 1];
  52. $redis = Redis::connection();
  53. $area_ids = $redis->georadius(Area::REDIS_AREAS_LOCATION_TAG, $lng, $lat, 30, 'km', $options);
  54. if (count($area_ids) === 1) {
  55. $area_id = $area_ids[0];
  56. }
  57. if (!$area_id) {
  58. if ($user) {
  59. $area_id = $user->register_area_id;
  60. }
  61. }
  62. }
  63. if (!$area_id) {
  64. return $this->errorNoValidation('该附近暂无运营区域');
  65. }
  66. $area = $areaRepository->byIdGetModel($area_id);
  67. $area->load('setting');
  68. if ($user) {
  69. if (!$user->register_area_id) {
  70. $user->register_area_id = $area->id;
  71. $user->register_area = $area->name;
  72. $user->save();
  73. }
  74. }
  75. return $this->response->item($area, AreaTransformer::class);
  76. // // 查找最近区域
  77. // $options = ['SORT' => 'ASC', 'COUNT' => 1];
  78. // $redis = Redis::connection();
  79. // $area_ids = $redis->georadius(Area::REDIS_AREAS_LOCATION_TAG, $lng, $lat, 30, 'km', $options);
  80. // if (count($area_ids) === 1) {
  81. // $area = $areaRepository->byIdGetModel($area_ids[0]);
  82. // if (!$area) {
  83. // return $this->errorNoValidation('该附近暂无运营区域');
  84. // }
  85. // $area->load('setting');
  86. // return $this->response->item($area, AreaTransformer::class);
  87. // } else {
  88. // return $this->errorNoValidation('该附近暂无运营区域');
  89. // }
  90. } catch (\Exception $exception) {
  91. return $this->errorException($exception->getMessage());
  92. }
  93. }
  94. /**
  95. * 骑行订单
  96. * User: Mead
  97. */
  98. public function rideOrder(Request $request, OrderRepository $orderRepository)
  99. {
  100. $no = $request->get('no');
  101. try {
  102. $order = $orderRepository->byNo($no);
  103. if (!$order) {
  104. return $this->errorNoValidation('订单找不到');
  105. }
  106. return $this->response->item($order, OrderRideStatusTransformer::class);
  107. } catch (\Exception $exception) {
  108. return $this->errorException($exception->getMessage());
  109. }
  110. }
  111. /**
  112. * 充值页面
  113. * @param RechargeConfigurationRepository $configurationRepository
  114. */
  115. public function rechargePage(Request $request, RechargeConfigurationRepository $configurationRepository)
  116. {
  117. try {
  118. $area_id = $request->get('area_id', $this->user->register_area_id);
  119. $setting = $configurationRepository->byAreaIdGetActiveConfig($area_id);
  120. if (count($setting) === 0) {
  121. return $this->response->array(RechargeConfiguration::DEFAULT_CONFIG);
  122. }
  123. return $this->response->collection($setting->sortBy('recharge_money'), RechargeConfigurationTransformer::class);
  124. } catch (\Exception $e) {
  125. return $this->errorNoValidation($e->getMessage());
  126. }
  127. }
  128. /**
  129. * 邀请新用户活动列表 inviteNewusersConfigs
  130. *
  131. * @param InviteNewUsersConfigRepository $inviteNewUsersConfigRepository
  132. * @return \Dingo\Api\Http\Response|void
  133. * @author Fx
  134. *
  135. */
  136. public function inviteNewusersConfigs(InviteNewUsersConfigRepository $inviteNewUsersConfigRepository, CouponRepository $couponRepository, CardRidingRepository $cardRidingRepository)
  137. {
  138. try {
  139. $area_id = $this->user->register_area_id;
  140. $inviteConfigs = $inviteNewUsersConfigRepository->getInviteNewUsersConfigByAreaId($area_id);
  141. if (empty($inviteConfigs)) return $this->response->array([]);
  142. $dynamic_item = $inviteConfigs->dynamic_item;
  143. if (count($dynamic_item) == 0) return $this->response->array([]);
  144. foreach ($dynamic_item as &$v) {
  145. if ($v['give_type'] == InviteNewUsersGiveGiftLog::GIFT_TYPE_BALANCE) {
  146. $v['gift_name'] = '赠送余额';
  147. $v['gift_id'] = $v['balance']; // id就是钱
  148. $v['gift_num'] = $v['balance']; // 数量就是钱
  149. } elseif ($v['give_type'] == InviteNewUsersGiveGiftLog::GIFT_TYPE_COUPON) {
  150. $coupon = $couponRepository->byIdGetModel($v['coupon_id']);
  151. if (empty($coupon)) {
  152. unset($v);
  153. } else {
  154. $v['gift_name'] = $coupon->title;
  155. $v['gift_id'] = $v['coupon_id'];
  156. $v['gift_num'] = $v['coupon_num'];
  157. $v['gift_detail'] = $coupon;
  158. }
  159. } elseif ($v['give_type'] == InviteNewUsersGiveGiftLog::GIFT_TYPE_CARD) {
  160. $card = $cardRidingRepository->byIdGetModel($v['card_id']);
  161. if (empty($card)) {
  162. unset($v);
  163. } else {
  164. $v['gift_name'] = $card->name;
  165. $v['gift_id'] = $v['card_id'];
  166. $v['gift_num'] = 1; // 骑行卡 默认一张
  167. $v['gift_detail'] = $card;
  168. }
  169. } else {
  170. unset($v);
  171. }
  172. }
  173. if (count($dynamic_item) == 0) return $this->response->array([]);
  174. $inviteConfigs->dynamic_item = $dynamic_item;
  175. $inviteConfigs->condition = InviteNewUsersConfig::$conditionMaps[$inviteConfigs->condition];
  176. return $this->response->array($inviteConfigs->toArray());
  177. } catch (\Exception $e) {
  178. return $this->errorNoValidation($e->getMessage());
  179. }
  180. }
  181. /**
  182. * 申请还车点
  183. * @param ApplyAddParkingRequest $request
  184. * @return \Dingo\Api\Http\Response|void
  185. * Author: Mead
  186. */
  187. public function applyAddParking(ApplyAddParkingRequest $request)
  188. {
  189. try {
  190. $data = [
  191. 'location_name' => $request->get('location_name'),
  192. 'longitude' => $request->get('longitude'),
  193. 'latitude' => $request->get('latitude'),
  194. 'area_id' => $request->get('area_id'),
  195. 'user_id' => $this->user->id,
  196. ];
  197. ApplyParking::query()->create($data);
  198. return $this->response->array([
  199. 'status' => 'OK'
  200. ]);
  201. } catch (\Exception $e) {
  202. return $this->errorNoValidation($e->getMessage());
  203. }
  204. }
  205. /**
  206. * 退款状态
  207. * @return \Dingo\Api\Http\Response|void
  208. * Author: Mead
  209. */
  210. public function userDepositStatus()
  211. {
  212. try {
  213. $order = DepositOrder::where('user_id', $this->user->id)->where('pay_status', DepositOrder::PAY_STATUS_OK)->where('is_refund', DepositOrder::REFUND_NO)->orderBy('id', 'desc')->first();
  214. if (!$order) {
  215. return $this->response()->array([
  216. 'type' => 1,
  217. 'msg' => '未缴纳',
  218. ]);
  219. }
  220. $refundLog = RefundLog::where('deposit_id', $order->id)->first();
  221. if (!$refundLog) {
  222. return $this->response()->array([
  223. 'type' => 4,
  224. 'msg' => '申请退押金',
  225. 'money' => $order->pay_money
  226. ]);
  227. }
  228. $pay_status = (int)$refundLog->pay_status;
  229. if ($pay_status === RefundLog::PAY_STATUS_WAIT) {
  230. return $this->response()->array([
  231. 'type' => 3,
  232. 'msg' => '退款排队中',
  233. ]);
  234. }
  235. if ($pay_status === RefundLog::PAY_STATUS_NO) {
  236. return $this->response()->array([
  237. 'type' => 0,
  238. 'msg' => '退款中',
  239. ]);
  240. }
  241. if ($pay_status === RefundLog::PAY_STATUS_ERROR) {
  242. return $this->response()->array([
  243. 'type' => 2,
  244. 'msg' => $refundLog->result,
  245. ]);
  246. }
  247. if ($pay_status === RefundLog::PAY_STATUS_OK) {
  248. return $this->response()->array([
  249. 'type' => 1,
  250. 'msg' => '已退款',
  251. ]);
  252. }
  253. } catch (\Exception $e) {
  254. return $this->errorNoValidation($e->getMessage());
  255. }
  256. }
  257. }