UserRecharge.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 int $uid
  53. * @return bool|string
  54. */
  55. public static function getNewOrderId($uid = 0)
  56. {
  57. if (!$uid) return false;
  58. $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();
  59. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  60. }
  61. /**
  62. * 充值js支付
  63. * @param $orderInfo
  64. * @return array|string
  65. * @throws \Exception
  66. */
  67. public static function jsPay($orderInfo)
  68. {
  69. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  70. }
  71. /**
  72. * 微信H5支付
  73. * @param $orderInfo
  74. * @return mixed
  75. */
  76. public static function wxH5Pay($orderInfo)
  77. {
  78. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  79. }
  80. /**
  81. * 公众号支付
  82. * @param $orderInfo
  83. * @return array|string
  84. * @throws \Exception
  85. */
  86. public static function wxPay($orderInfo)
  87. {
  88. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  89. }
  90. /**
  91. * //TODO用户充值成功后
  92. * @param $orderId
  93. */
  94. public static function rechargeSuccess($orderId)
  95. {
  96. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  97. if (!$order) return false;
  98. $user = User::getUserInfo($order['uid']);
  99. self::beginTrans();
  100. $price = bcadd($order['price'], $order['give_price'], 2);
  101. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  102. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  103. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  104. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  105. $res = $res1 && $res2 && $res3;
  106. self::checkTrans($res);
  107. event('RechargeSuccess', [$order]);
  108. return $res;
  109. }
  110. /**
  111. * 导入佣金到余额
  112. * @param $uid 用户uid
  113. * @param $price 导入金额
  114. * @return bool
  115. * @throws \think\Exception
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. */
  120. public static function importNowMoney($uid, $price)
  121. {
  122. $user = User::getUserInfo($uid);
  123. self::beginTrans();
  124. try {
  125. $broken_time = intval(sys_config('extract_time'));
  126. $search_time = time() - 86400 * $broken_time;
  127. //返佣 +
  128. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  129. ->where('add_time', '>', $search_time)
  130. ->where('pm', 1)
  131. ->sum('number');
  132. //退款退的佣金 -
  133. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  134. ->where('add_time', '>', $search_time)
  135. ->where('pm', 0)
  136. ->sum('number');
  137. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  138. if ($broken_commission < 0)
  139. $broken_commission = 0;
  140. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  141. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  142. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  143. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  144. $res2 = UserBill::income('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  145. $res4 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'brokerage_recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  146. $res = $res2 && $res1 && $res3 && $res4;
  147. self::checkTrans($res);
  148. if ($res) {
  149. event('ImportNowMoney', [$uid, $price]);
  150. }
  151. return $res;
  152. } catch (\Exception $e) {
  153. self::rollbackTrans();
  154. return self::setErrorInfo($e->getMessage());
  155. }
  156. }
  157. /**
  158. * 导入佣金到余额
  159. * @param $uid 用户uid
  160. * @param $price 导入金额
  161. * @return bool
  162. * @throws \think\Exception
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. */
  167. public static function Wdc2Money($uid, $price)
  168. {
  169. $user = User::getUserInfo($uid);
  170. $commission = bcmul($user['wdc_all'], bcmul(sys_config('wdc2money', 40), 0.01, 2), 2);
  171. $value = bcsub($user['wdc_all'], $commission);
  172. if ($price + $user['wdc_trade'] > $value) {
  173. $value_sub = bcsub($value, $user['wdc_trade'], 2);
  174. return self::setErrorInfo("可转酒币不足,您的可转酒币余额为$value_sub");
  175. }
  176. self::beginTrans();
  177. try {
  178. $broken_time = intval(sys_config('extract_time'));
  179. $search_time = time() - 86400 * $broken_time;
  180. $commissionCount = $user['wdc'];
  181. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于酒币!');
  182. // $commission = bcmul($price, bcmul(sys_config('wdc2money', 40), 0.01, 2), 2);
  183. // $value = bcsub($price, $commission);
  184. $value = $price;
  185. $res1 = User::bcInc($uid, 'now_money', $value, 'uid');
  186. $res5 = User::bcInc($uid, 'wdc_trade', $price, 'uid');
  187. $res3 = User::bcDec($uid, 'wdc', $price, 'uid');
  188. $res2 = UserBill::income('用户酒币转入余额', $uid, 'now_money', 'recharge', $value, 0, $user['now_money'], '成功转入余额' . floatval($value) . '元' . "手续费" . $commission);
  189. $res4 = UserBill::expend('用户酒币转入余额', $uid, 'now_money', 'wdc', $value, 0, $user['now_money'], '成功转入余额' . floatval($value) . '元' . $commission);
  190. $res = $res2 && $res1 && $res3 && $res4 && $res5;
  191. self::checkTrans($res);
  192. return $res;
  193. } catch (\Exception $e) {
  194. self::rollbackTrans();
  195. return self::setErrorInfo($e->getMessage());
  196. }
  197. }
  198. }