UserRecharge.php 7.6 KB

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