UserRecharge.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public static function addRecharge($uid,$price,$recharge_type = 'weixin',$paid = 0)
  36. {
  37. $order_id = self::getNewOrderId($uid);
  38. $add_time = time();
  39. return self::create(compact('order_id','uid','price','recharge_type','paid','add_time'));
  40. }
  41. public static function getNewOrderId($uid = 0)
  42. {
  43. if(!$uid) return false;
  44. $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();
  45. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  46. }
  47. public static function jsPay($orderInfo)
  48. {
  49. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']),$orderInfo['order_id'],$orderInfo['price'],'user_recharge','用户充值');
  50. }
  51. /**
  52. * 微信H5支付
  53. * @param $orderInfo
  54. * @return mixed
  55. */
  56. public static function wxH5Pay($orderInfo)
  57. {
  58. return WechatService::paymentPrepare(null,$orderInfo['order_id'],$orderInfo['price'],'user_recharge','用户充值', '', 'MWEB');
  59. }
  60. /**
  61. * 公众号支付
  62. * @param $orderInfo
  63. * @return array|string
  64. * @throws \Exception
  65. */
  66. public static function wxPay($orderInfo)
  67. {
  68. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'),$orderInfo['order_id'],$orderInfo['price'],'user_recharge','用户充值');
  69. }
  70. /**
  71. * //TODO用户充值成功后
  72. * @param $orderId
  73. */
  74. public static function rechargeSuccess($orderId)
  75. {
  76. $order = self::where('order_id',$orderId)->where('paid',0)->find();
  77. if(!$order) return false;
  78. $user = User::getUserInfo($order['uid']);
  79. self::beginTrans();
  80. $res1 = self::where('order_id',$order['order_id'])->update(['paid'=>1,'pay_time'=>time()]);
  81. $res2 = UserBill::income('用户余额充值',$order['uid'],'now_money','recharge',$order['price'],$order['id'],$user['now_money'],'成功充值余额'.floatval($order['price']).'元');
  82. $res3 = User::edit(['now_money'=>bcadd($user['now_money'],$order['price'],2)],$order['uid'],'uid');
  83. $res = $res1 && $res2 && $res3;
  84. self::checkTrans($res);
  85. event('RechargeSuccess', [$order]);
  86. return $res;
  87. }
  88. /*
  89. * 导入佣金到余额
  90. * @param int uid 用户uid
  91. * @param string $price 导入金额
  92. * */
  93. public static function importNowMoney($uid,$price){
  94. $user = User::getUserInfo($uid);
  95. self::beginTrans();
  96. try{
  97. if($price > $user['brokerage_price']) return self::setErrorInfo('转入金额不能大于当前佣金!');
  98. $res1 = User::bcInc($uid,'now_money',$price,'uid');
  99. $res3 = User::bcDec($uid,'brokerage_price',$price,'uid');
  100. $res2 = UserBill::expend('用户佣金转入余额',$uid,'now_money','recharge',$price,0,$user['now_money'],'成功转入余额'.floatval($price).'元');
  101. $res = $res2 && $res1 && $res3;
  102. self::checkTrans($res);
  103. if($res){
  104. event('ImportNowMoney', [$uid, $price]);
  105. }
  106. return $res;
  107. }catch (\Exception $e){
  108. self::rollbackTrans();
  109. return self::setErrorInfo($e->getMessage());
  110. }
  111. }
  112. }