UserRechargeController.php 6.9 KB

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