UserRecharge.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\models\user;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\MiniProgramService;
  5. use crmeb\services\WechatService;
  6. use crmeb\traits\ModelTrait;
  7. /**
  8. * TODO 用户充值
  9. * Class UserRecharge
  10. * @package app\models\user
  11. */
  12. class UserRecharge extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'user_recharge';
  24. use ModelTrait;
  25. protected $insert = ['add_time'];
  26. protected function setAddTimeAttr()
  27. {
  28. return time();
  29. }
  30. /**
  31. * 创建充值订单
  32. * @param $uid
  33. * @param $price
  34. * @param string $recharge_type
  35. * @param int $paid
  36. * @return UserRecharge|bool|\think\Model
  37. */
  38. public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0)
  39. {
  40. $order_id = self::getNewOrderId($uid);
  41. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  42. $add_time = time();
  43. return self::create(compact('order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price'));
  44. }
  45. /**
  46. * 生成充值订单号
  47. * @param int $uid
  48. * @return bool|string
  49. */
  50. public static function getNewOrderId($uid = 0)
  51. {
  52. if (!$uid) return false;
  53. $count = (int)self::where('uid', $uid)->where('add_time', '>=', strtotime(date("Y-m-d")))->where('add_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
  54. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  55. }
  56. /**
  57. * 充值js支付
  58. * @param $orderInfo
  59. * @return array|string
  60. * @throws \Exception
  61. */
  62. public static function jsPay($orderInfo)
  63. {
  64. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  65. }
  66. /**
  67. * 微信H5支付
  68. * @param $orderInfo
  69. * @return mixed
  70. */
  71. public static function wxH5Pay($orderInfo)
  72. {
  73. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  74. }
  75. /**
  76. * weixin app 支付
  77. * @param $orderInfo
  78. * @return array|string
  79. * @throws \Exception
  80. */
  81. public static function wxAppPay($orderInfo)
  82. {
  83. return WechatService::appPay(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  84. }
  85. /**
  86. * 公众号支付
  87. * @param $orderInfo
  88. * @return array|string
  89. * @throws \Exception
  90. */
  91. public static function wxPay($orderInfo)
  92. {
  93. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  94. }
  95. /**
  96. * //TODO用户充值成功后
  97. * @param $orderId
  98. */
  99. public static function rechargeSuccess($orderId)
  100. {
  101. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  102. if (!$order) return false;
  103. $user = User::getUserInfo($order['uid']);
  104. self::beginTrans();
  105. $price = bcadd($order['price'], $order['give_price'], 2);
  106. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  107. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  108. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  109. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  110. $res = $res1 && $res2 && $res3;
  111. self::checkTrans($res);
  112. event('RechargeSuccess', [$order]);
  113. return $res;
  114. }
  115. /**
  116. * 导入佣金到余额
  117. * @param $uid 用户uid
  118. * @param $price 导入金额
  119. * @return bool
  120. * @throws \think\Exception
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @throws \think\exception\DbException
  124. */
  125. public static function importNowMoney($uid, $price)
  126. {
  127. $user = User::getUserInfo($uid);
  128. self::beginTrans();
  129. try {
  130. $broken_time = intval(sys_config('extract_time'));
  131. $search_time = time() - 86400 * $broken_time;
  132. //返佣 +
  133. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  134. ->where('add_time', '>', $search_time)
  135. ->where('pm', 1)
  136. ->sum('number');
  137. //退款退的佣金 -
  138. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  139. ->where('add_time', '>', $search_time)
  140. ->where('pm', 0)
  141. ->sum('number');
  142. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  143. if ($broken_commission < 0)
  144. $broken_commission = 0;
  145. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  146. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  147. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  148. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  149. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  150. $extractInfo = [
  151. 'uid' => $uid,
  152. 'real_name' => $user['nickname'],
  153. 'extract_type' => 'balance',
  154. 'extract_price' => $price,
  155. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  156. 'add_time' => time(),
  157. 'status' => 1
  158. ];
  159. $res4 = UserExtract::create($extractInfo);
  160. $res = $res2 && $res1 && $res3;
  161. self::checkTrans($res);
  162. if ($res) {
  163. event('ImportNowMoney', [$uid, $price]);
  164. }
  165. return $res;
  166. } catch (\Exception $e) {
  167. self::rollbackTrans();
  168. return self::setErrorInfo($e->getMessage());
  169. }
  170. }
  171. }