UserRechargeController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\system\SystemGroupData;
  4. use app\models\user\UserRecharge;
  5. use app\Request;
  6. use crmeb\services\GroupDataService;
  7. use crmeb\services\SystemConfigService;
  8. use crmeb\services\UtilService;
  9. /**
  10. * 充值类
  11. * Class UserRechargeController
  12. * @package app\api\controller\user
  13. */
  14. class UserRechargeController
  15. {
  16. /**
  17. * 小程序充值
  18. *
  19. * @param Request $request
  20. * @return mixed
  21. */
  22. public function routine(Request $request)
  23. {
  24. list($price, $recharId, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0]], $request, true);
  25. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  26. $storeMinRecharge = sys_config('store_user_min_recharge');
  27. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  28. switch ((int)$type) {
  29. case 0: //支付充值余额
  30. $paid_price = 0;
  31. if ($recharId) {
  32. $data = SystemGroupData::getDateValue($recharId);
  33. if ($data === false) {
  34. return app('json')->fail('您选择的充值方式已下架!');
  35. } else {
  36. $paid_price = $data['give_money'] ?? 0;
  37. }
  38. }
  39. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  40. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  41. try {
  42. return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
  43. } catch (\Exception $e) {
  44. return app('json')->fail($e->getMessage());
  45. }
  46. break;
  47. case 1: //佣金转入余额
  48. if (UserRecharge::importNowMoney($request->uid(), $price))
  49. return app('json')->successful('转入余额成功');
  50. else
  51. return app('json')->fail(UserRecharge::getErrorInfo());
  52. break;
  53. default:
  54. return app('json')->fail('缺少参数');
  55. break;
  56. }
  57. }
  58. /**
  59. * 公众号充值
  60. *
  61. * @param Request $request
  62. * @return mixed
  63. */
  64. public function wechat(Request $request)
  65. {
  66. list($price, $recharId, $from, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], $request, true);
  67. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  68. $storeMinRecharge = sys_config('store_user_min_recharge');
  69. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  70. switch ((int)$type) {
  71. case 0: //支付充值余额
  72. $paid_price = 0;
  73. if ($recharId) {
  74. $data = SystemGroupData::getDateValue($recharId);
  75. if ($data === false) {
  76. return app('json')->fail('您选择的充值方式已下架!');
  77. } else {
  78. $paid_price = $data['give_money'] ?? 0;
  79. }
  80. }
  81. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
  82. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  83. try {
  84. if ($from == 'weixinh5') {
  85. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  86. } else {
  87. $recharge = UserRecharge::wxPay($rechargeOrder);
  88. }
  89. } catch (\Exception $e) {
  90. return app('json')->fail($e->getMessage());
  91. }
  92. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  93. break;
  94. case 1: //佣金转入余额
  95. if (UserRecharge::importNowMoney($request->uid(), $price))
  96. return app('json')->successful('转入余额成功');
  97. else
  98. return app('json')->fail(UserRecharge::getErrorInfo());
  99. break;
  100. default:
  101. return app('json')->fail('缺少参数');
  102. break;
  103. }
  104. }
  105. /**
  106. * 支付宝充值
  107. *
  108. * @param Request $request
  109. * @return mixed
  110. */
  111. public function ali(Request $request)
  112. {
  113. list($price, $recharId, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0]], $request, true);
  114. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  115. $storeMinRecharge = sys_config('store_user_min_recharge');
  116. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  117. switch ((int)$type) {
  118. case 0: //支付充值余额
  119. $paid_price = 0;
  120. if ($recharId) {
  121. $data = SystemGroupData::getDateValue($recharId);
  122. if ($data === false) {
  123. return app('json')->fail('您选择的充值方式已下架!');
  124. } else {
  125. $paid_price = $data['give_money'] ?? 0;
  126. }
  127. }
  128. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'ali', $paid_price);
  129. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  130. try {
  131. return app('json')->successful(UserRecharge::aliPay($rechargeOrder));
  132. } catch (\Exception $e) {
  133. return app('json')->fail($e->getMessage());
  134. }
  135. break;
  136. case 1: //佣金转入余额
  137. if (UserRecharge::importNowMoney($request->uid(), $price))
  138. return app('json')->successful('转入余额成功');
  139. else
  140. return app('json')->fail(UserRecharge::getErrorInfo());
  141. break;
  142. default:
  143. return app('json')->fail('缺少参数');
  144. break;
  145. }
  146. }
  147. /**
  148. * 充值额度选择
  149. * @return mixed
  150. */
  151. public function index()
  152. {
  153. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  154. $data['recharge_quota'] = $rechargeQuota;
  155. $recharge_attention = sys_config('recharge_attention');
  156. $recharge_attention = explode("\n", $recharge_attention);
  157. $data['recharge_attention'] = $recharge_attention;
  158. return app('json')->successful($data);
  159. }
  160. }