UserRecharge.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/6/2
  7. *
  8. *
  9. */
  10. namespace app\controller\api\user;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\system\groupData\GroupDataRepository;
  13. use app\common\repositories\user\UserRechargeRepository;
  14. use app\common\repositories\user\UserRepository;
  15. use app\common\repositories\wechat\WechatUserRepository;
  16. use crmeb\services\WechatService;
  17. use think\App;
  18. class UserRecharge extends BaseController
  19. {
  20. protected $repository;
  21. public function __construct(App $app, UserRechargeRepository $repository)
  22. {
  23. parent::__construct($app);
  24. $this->repository = $repository;
  25. }
  26. public function brokerage(UserRepository $userRepository)
  27. {
  28. $brokerage = (float)$this->request->param('brokerage');
  29. if ($brokerage <= 0)
  30. return app('json')->fail('请输入正确的充值金额!');
  31. $user = $this->request->userInfo();
  32. if ($user->brokerage_price < $brokerage)
  33. return app('json')->fail('剩余可用佣金不足' . $brokerage);
  34. $config = systemConfig(['recharge_switch', 'balance_func_status']);
  35. if (!$config['recharge_switch'] || !$config['balance_func_status'])
  36. return app('json')->fail('余额充值功能已关闭');
  37. $userRepository->switchBrokerage($user, $brokerage);
  38. return app('json')->success('转换成功');
  39. }
  40. public function recharge(GroupDataRepository $groupDataRepository)
  41. {
  42. [$type, $price, $rechargeId] = $this->request->params(['type', 'price', 'recharge_id'], true);
  43. if (!in_array($type, ['wechat', 'routine', 'h5']))
  44. return app('json')->fail('请选择正确的支付方式!');
  45. $wechatUserId = $this->request->userInfo()['wechat_user_id'];
  46. if (!$wechatUserId && in_array($type, ['wechat', 'routine']))
  47. return app('json')->fail('请关联微信' . ($type == 'wechat' ? '公众号' : '小程序') . '!');
  48. $config = systemConfig(['store_user_min_recharge', 'recharge_switch', 'balance_func_status']);
  49. if (!$config['recharge_switch'] || !$config['balance_func_status'])
  50. return app('json')->fail('余额充值功能已关闭');
  51. if ($rechargeId) {
  52. if (!intval($rechargeId))
  53. return app('json')->fail('请选择充值金额!');
  54. $rule = $groupDataRepository->merGet(intval($rechargeId), 0);
  55. if (!$rule || !isset($rule['price']) || !isset($rule['give']))
  56. return app('json')->fail('您选择的充值方式已下架!');
  57. $give = floatval($rule['give']);
  58. $price = floatval($rule['price']);
  59. } else {
  60. $price = floatval($price);
  61. if ($price <= 0)
  62. return app('json')->fail('请输入充值金额!');
  63. if ($price < $config['store_user_min_recharge'])
  64. return app('json')->fail('最底充值' . floatval($config['store_user_min_recharge']));
  65. $give = 0;
  66. }
  67. $recharge = $this->repository->create($this->request->uid(), $price, $give, $type);
  68. $userRepository = app()->make(WechatUserRepository::class);
  69. if ($type == 'wechat') {
  70. $openId = $userRepository->idByOpenId($wechatUserId);
  71. if (!$openId)
  72. return app('json')->fail('请关联微信公众号!');
  73. $data = $this->repository->wxPay($openId, $recharge);
  74. } else if ($type == 'h5') {
  75. $data = $this->repository->wxH5Pay($recharge);
  76. } else {
  77. $openId = $userRepository->idByRoutineId($wechatUserId);
  78. if (!$openId)
  79. return app('json')->fail('请关联微信小程序!');
  80. $data = $this->repository->jsPay($openId, $recharge);
  81. }
  82. return app('json')->success(compact('type', 'data'));
  83. }
  84. }