UserRecharge.php 4.4 KB

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