UserRechargeController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * @return mixed
  108. */
  109. public function index()
  110. {
  111. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  112. $data['recharge_quota'] = $rechargeQuota;
  113. $recharge_attention = sys_config('recharge_attention');
  114. $recharge_attention = explode("\n", $recharge_attention);
  115. $data['recharge_attention'] = $recharge_attention;
  116. return app('json')->successful($data);
  117. }
  118. }