UserRechargeRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\UserRechargeDao;
  13. use app\common\model\user\User;
  14. use app\common\model\user\UserRecharge;
  15. use app\common\repositories\BaseRepository;
  16. use crmeb\jobs\SendSmsJob;
  17. use crmeb\services\PayService;
  18. use think\facade\Db;
  19. use think\facade\Queue;
  20. /**
  21. * Class UserRechargeRepository
  22. * @package app\common\repositories\user
  23. * @author xaboy
  24. * @day 2020/6/2
  25. * @mixin UserRechargeDao
  26. */
  27. class UserRechargeRepository extends BaseRepository
  28. {
  29. const TYPE_WECHAT = 'weixin';
  30. const TYPE_ROUTINE = 'routine';
  31. /**
  32. * UserRechargeRepository constructor.
  33. * @param UserRechargeDao $dao
  34. */
  35. public function __construct(UserRechargeDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. public function create($uid, float $price, float $givePrice, string $type)
  40. {
  41. return $this->dao->create([
  42. 'uid' => $uid,
  43. 'price' => $price,
  44. 'give_price' => $givePrice,
  45. 'recharge_type' => $type,
  46. 'paid' => 0,
  47. 'order_id' => $this->dao->createOrderId($uid)
  48. ]);
  49. }
  50. public function getList($where, $page, $limit)
  51. {
  52. $query = $this->dao->searchJoinQuery($where)->order('a.pay_time DESC,a.create_time DESC');
  53. $count = $query->count();
  54. $list = $query->page($page, $limit)->select();
  55. return compact('count', 'list');
  56. }
  57. public function priceByGive($price)
  58. {
  59. $quota = systemGroupData('user_recharge_quota');
  60. $give = 0;
  61. foreach ($quota as $item) {
  62. $min = floatval($item['price']);
  63. $_give = floatval($item['give']);
  64. if ($price > $min) $give = $_give;
  65. }
  66. return $give;
  67. }
  68. /**
  69. * @param string $type
  70. * @param User $user
  71. * @param UserRecharge $recharge
  72. * @param string $return_url
  73. * @return mixed
  74. * @author xaboy
  75. * @day 2020/10/22
  76. */
  77. public function pay(string $type, User $user, UserRecharge $recharge, $return_url = '', $isApp = false)
  78. {
  79. if (in_array($type, ['weixin', 'alipay'], true) && $isApp) {
  80. $type .= 'App';
  81. }
  82. event('user.recharge.before', compact('user', 'recharge', 'type', 'isApp'));
  83. $service = new PayService($type, $recharge->getPayParams($type === 'alipay' ? $return_url : ''));
  84. $config = $service->pay($user);
  85. return $config + ['recharge_id' => $recharge['recharge_id'], 'type' => $type];
  86. }
  87. /**
  88. * //TODO 余额充值成功
  89. *
  90. * @param $orderId
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @author xaboy
  95. * @day 2020/6/19
  96. */
  97. public function paySuccess($orderId)
  98. {
  99. $recharge = $this->dao->getWhere(['order_id' => $orderId]);
  100. if ($recharge->paid == 1) return;
  101. $recharge->paid = 1;
  102. $recharge->pay_time = date('Y-m-d H:i:s');
  103. Db::transaction(function () use ($recharge) {
  104. $price = bcadd($recharge->price, $recharge->give_price, 2);
  105. $mark = '成功充值余额' . floatval($recharge->price) . '元' . ($recharge->give_price > 0 ? ',赠送' . $recharge->give_price . '元' : '');
  106. app()->make(UserBillRepository::class)->incBill($recharge->user->uid, 'now_money', 'recharge', [
  107. 'link_id' => $recharge->recharge_id,
  108. 'status' => 1,
  109. 'title' => '余额充值',
  110. 'number' => $price,
  111. 'mark' => $mark,
  112. 'balance' => $recharge->user->now_money
  113. ]);
  114. $recharge->user->now_money = bcadd($recharge->user->now_money, $price, 2);
  115. $recharge->user->save();
  116. $recharge->save();
  117. });
  118. Queue::push(SendSmsJob::class,['tempId' => 'USER_BALANCE_CHANGE', 'id' =>$orderId]);
  119. event('user.recharge',compact('recharge'));
  120. }
  121. }