UserRechargeController.php 7.4 KB

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