UserRecharge.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\user\UserExtractRepository;
  14. use app\common\repositories\system\groupData\GroupDataRepository;
  15. use app\common\repositories\user\UserRechargeRepository;
  16. use app\common\repositories\user\UserRepository;
  17. use app\common\repositories\wechat\WechatUserRepository;
  18. use crmeb\services\WechatService;
  19. use think\App;
  20. class UserRecharge extends BaseController
  21. {
  22. protected $repository;
  23. public function __construct(App $app, UserRechargeRepository $repository)
  24. {
  25. parent::__construct($app);
  26. $this->repository = $repository;
  27. }
  28. /**
  29. * 佣金转入余额
  30. * @param UserRepository $userRepository
  31. * @return \think\response\Json
  32. * @author wuhaotian
  33. * @email 442384644@qq.com
  34. * @date 2024/7/10
  35. */
  36. public function brokerage(UserExtractRepository $userExtractRepository)
  37. {
  38. $brokerage = (float)$this->request->param('brokerage');
  39. if ($brokerage <= 0)
  40. return app('json')->fail('请输入正确的充值金额!');
  41. $user = $this->request->userInfo();
  42. //if ($user->brokerage_price < $brokerage)
  43. // return app('json')->fail('剩余可用佣金不足' . $brokerage);
  44. //$config = systemConfig(['recharge_switch', 'balance_func_status']);
  45. //if (!$config['recharge_switch'] || !$config['balance_func_status'])
  46. // return app('json')->fail('余额充值功能已关闭');
  47. //$userRepository->switchBrokerage($user, $brokerage);
  48. $data = [
  49. 'extract_type' => $userExtractRepository::EXTRACT_TYPE_YUE,
  50. 'extract_price'=> $brokerage,
  51. ];
  52. $userExtractRepository->create($user,$data);
  53. return app('json')->success('转换成功');
  54. }
  55. /**
  56. * 用户充值
  57. * @param GroupDataRepository $groupDataRepository
  58. * @return \think\response\Json
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @author wuhaotian
  63. * @email 442384644@qq.com
  64. * @date 2024/7/10
  65. */
  66. public function recharge(GroupDataRepository $groupDataRepository)
  67. {
  68. [$type, $price, $rechargeId, $return_url] = $this->request->params(['type', 'price', 'recharge_id', 'return_url'], true);
  69. if (!in_array($type, ['weixin', 'routine', 'h5', 'alipay', 'alipayQr', 'weixinQr']))
  70. return app('json')->fail('请选择正确的支付方式!');
  71. if($price > 1000000){
  72. return app('json')->fail('充值金额超出最大限制');
  73. }
  74. $app = $this->request->isApp();
  75. $user = $this->request->userInfo();
  76. $wechatUserId = $user['wechat_user_id'];
  77. if (!$wechatUserId && in_array($type, [$this->repository::TYPE_ROUTINE, $this->repository::TYPE_WECHAT]) && !$app)
  78. return app('json')->fail('请关联微信' . ($type == 'weixin' ? '公众号' : '小程序') . '!');
  79. $config = systemConfig(['store_user_min_recharge', 'recharge_switch', 'balance_func_status']);
  80. if (!$config['recharge_switch'] || !$config['balance_func_status'])
  81. return app('json')->fail('余额充值功能已关闭');
  82. if ($rechargeId) {
  83. if (!intval($rechargeId))
  84. return app('json')->fail('请选择充值金额!');
  85. $rule = $groupDataRepository->merGet(intval($rechargeId), 0);
  86. if (!$rule || !isset($rule['price']) || !isset($rule['give']))
  87. return app('json')->fail('您选择的充值方式已下架!');
  88. $give = floatval($rule['give']);
  89. $price = floatval($rule['price']);
  90. if ($price <= 0)
  91. return app('json')->fail('请选择正确的充值金额!');
  92. } else {
  93. $price = floatval($price);
  94. if ($price <= 0)
  95. return app('json')->fail('请输入正确的充值金额!');
  96. if ($price < $config['store_user_min_recharge'])
  97. return app('json')->fail('最低充值' . floatval($config['store_user_min_recharge']));
  98. $give = 0;
  99. }
  100. $recharge = $this->repository->create($this->request->uid(), $price, $give, $type);
  101. return app('json')->success($this->repository->pay($type, $user, $recharge, $return_url, $app));
  102. }
  103. }