UserRechargeController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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([
  25. [['price', 'f'], 0],
  26. [['rechar_id', 'd'], 0],
  27. ['type', 0]
  28. ], $request, true);
  29. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  30. $storeMinRecharge = sys_config('store_user_min_recharge');
  31. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  32. switch ((int)$type) {
  33. case 0: //支付充值余额
  34. $paid_price = 0;
  35. if ($recharId) {
  36. $data = SystemGroupData::getDateValue($recharId);
  37. if ($data === false) {
  38. return app('json')->fail('您选择的充值方式已下架!');
  39. } else {
  40. $paid_price = $data['give_money'] ?? 0;
  41. }
  42. }
  43. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  44. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  45. try {
  46. return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
  47. } catch (\Exception $e) {
  48. return app('json')->fail($e->getMessage());
  49. }
  50. break;
  51. case 1: //佣金转入余额
  52. if (UserRecharge::importNowMoney($request->uid(), $price))
  53. return app('json')->successful('转入余额成功');
  54. else
  55. return app('json')->fail(UserRecharge::getErrorInfo());
  56. break;
  57. default:
  58. return app('json')->fail('缺少参数');
  59. break;
  60. }
  61. }
  62. /**
  63. * 公众号充值
  64. *
  65. * @param Request $request
  66. * @return mixed
  67. */
  68. public function wechat(Request $request)
  69. {
  70. list($price, $recharId, $from, $type) = UtilService::postMore([
  71. [['price', 'f'], 0],
  72. [['rechar_id', 'd'], 0],
  73. ['from', 'weixin'],
  74. ['type', 0]
  75. ], $request, true);
  76. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  77. $storeMinRecharge = sys_config('store_user_min_recharge');
  78. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  79. switch ((int)$type) {
  80. case 0: //支付充值余额
  81. $paid_price = 0;
  82. if ($recharId) {
  83. $data = SystemGroupData::getDateValue($recharId);
  84. if ($data === false) {
  85. return app('json')->fail('您选择的充值方式已下架!');
  86. } else {
  87. $paid_price = $data['give_money'] ?? 0;
  88. }
  89. }
  90. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
  91. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  92. try {
  93. if ($from == 'weixinh5') {
  94. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  95. } else {
  96. $recharge = UserRecharge::wxPay($rechargeOrder);
  97. }
  98. } catch (\Exception $e) {
  99. return app('json')->fail($e->getMessage());
  100. }
  101. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  102. break;
  103. case 1: //佣金转入余额
  104. if (UserRecharge::importNowMoney($request->uid(), $price))
  105. return app('json')->successful('转入余额成功');
  106. else
  107. return app('json')->fail(UserRecharge::getErrorInfo());
  108. break;
  109. default:
  110. return app('json')->fail('缺少参数');
  111. break;
  112. }
  113. }
  114. /**
  115. * 充值额度选择
  116. * @return mixed
  117. */
  118. public function index()
  119. {
  120. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  121. $data['recharge_quota'] = $rechargeQuota;
  122. $recharge_attention = sys_config('recharge_attention');
  123. $recharge_attention = explode("\n", $recharge_attention);
  124. $data['recharge_attention'] = $recharge_attention;
  125. return app('json')->successful($data);
  126. }
  127. }