UserRecharge.php 7.2 KB

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