PageController.php 8.6 KB

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