UserRechargeController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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([['price', 0], ['rechar_id', 0], ['type', 0]], $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. * 公众号充值
  62. *
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function wechat(Request $request)
  67. {
  68. list($price, $recharId, $from, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], $request, true);
  69. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  70. $storeMinRecharge = sys_config('store_user_min_recharge');
  71. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  72. switch ((int)$type) {
  73. case 0: //支付充值余额
  74. $paid_price = 0;
  75. if ($recharId) {
  76. $data = SystemGroupData::getDateValue($recharId);
  77. if ($data === false) {
  78. return app('json')->fail('您选择的充值方式已下架!');
  79. } else {
  80. $paid_price = $data['give_money'] ?? 0;
  81. }
  82. }
  83. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
  84. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  85. try {
  86. if ($from == 'weixinh5') {
  87. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  88. } else {
  89. $recharge = UserRecharge::wxPay($rechargeOrder);
  90. }
  91. } catch (\Exception $e) {
  92. return app('json')->fail($e->getMessage());
  93. }
  94. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  95. break;
  96. case 1: //佣金转入余额
  97. if (UserRecharge::importNowMoney($request->uid(), $price))
  98. return app('json')->successful('转入余额成功');
  99. else
  100. return app('json')->fail(UserRecharge::getErrorInfo());
  101. break;
  102. default:
  103. return app('json')->fail('缺少参数');
  104. break;
  105. }
  106. }
  107. /**
  108. * 充值额度选择
  109. * @return mixed
  110. */
  111. public function index()
  112. {
  113. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  114. $data['recharge_quota'] = $rechargeQuota;
  115. $recharge_attention = sys_config('recharge_attention');
  116. $recharge_attention = explode("\n", $recharge_attention);
  117. $data['recharge_attention'] = $recharge_attention;
  118. return app('json')->successful($data);
  119. }
  120. public function trade_money(Request $request)
  121. {
  122. $uid = $request->post('to_uid', 0);
  123. $num = $request->post('num', 0);
  124. // $type = $request->post('type', 0);
  125. $real_get =$num;
  126. if (!$uid || !$trader = User::where('uid|phone',$uid)->find()) return app('json')->fail('接收用户不存在');
  127. $user = User::get($request->uid());
  128. $bill_type_ex = 'trade';
  129. // switch ($type)
  130. // {
  131. // case 0:
  132. $field = "now_money";$bill_type = "now_money"; $title = '余额';
  133. // if ($num < 1) return app('json')->fail('至少100余额才可以转账');
  134. if ($user['now_money'] < $real_get) return app('json')->fail('余额不足');
  135. // break;
  136. // case 1:
  137. // $field = "equity"; $bill_type = "equity";$title="权益分";
  138. // if ($num < 1) return app('json')->fail('至少100权益分才可以转账');
  139. // if ($user['equity'] < $real_get) return app('json')->fail('权益分不足');
  140. // }
  141. User::beginTrans();
  142. try {
  143. UserBill::expend('赠送'.$title, $request->uid(), $field, $bill_type_ex, $num, 0, $user[$field] - $num, '转账给' . $trader['phone'] .'扣除' . $num );
  144. UserBill::income('赠送'.$title, $trader['uid'], $field, $bill_type, $real_get, 0, $trader[$field] + (float)$real_get, '转账自' . $user['phone'] . '获得' . $num );
  145. User::bcDec($request->uid(), $field, $real_get, 'uid');
  146. User::bcInc($trader['uid'], $field, $num, 'uid');
  147. User::commitTrans();
  148. return app('json')->success('转账成功');
  149. } catch (\Exception $e) {
  150. User::rollbackTrans();
  151. return app('json')->fail($e->getMessage());
  152. }
  153. }
  154. }