UserRechargeController.php 6.8 KB

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