UserRecharge.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if ($openid = WechatUser::where('uid', $order['uid'])->value('routine_openid')) {
  113. MiniProgramService::sendSubscribeTemlate($openid, 'HgPU5FHUTwqGNSkJ7IsAP0XkJZV3UUf9fjUWUD3ebZY',
  114. [
  115. 'amount3' => $order['price'],
  116. 'date5' => date('Y-m-d H:i:s'),
  117. ]);
  118. }
  119. event('RechargeSuccess', [$order]);
  120. return $res;
  121. }
  122. public static function sendRechargeAward($order)
  123. {
  124. if (!$order['store_id']) return true;
  125. $store = SystemStore::get($order['store_id']);
  126. if (!$store) return true;
  127. $ratio = $store['recharge_award_ratio'];
  128. if ($ratio <= 0) return true;
  129. $ratio = bcdiv($ratio, 100, 4);
  130. $brokerage = bcmul($order['price'], $ratio, 2);
  131. if ($brokerage <= 0) return true;
  132. $res = SystemStore::where('id', $order['store_id'])->inc('brokerage_price', $brokerage)->update();
  133. return $res && SystemStoreBill::income('充值佣金', $order['store_id'], 'brokerage_price', 'recharge_award', $brokerage, $order['id'], ($store['brokerage_price'] + (float)$brokerage), '用户扫描店铺二维码充值' . $order['price'] . '元返利');
  134. }
  135. /**
  136. * 导入佣金到余额
  137. * @param $uid 用户uid
  138. * @param $price 导入金额
  139. * @return bool
  140. * @throws \think\Exception
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public static function importNowMoney($uid, $price)
  146. {
  147. $user = User::getUserInfo($uid);
  148. self::beginTrans();
  149. try {
  150. $broken_time = intval(sys_config('extract_time'));
  151. $search_time = time() - 86400 * $broken_time;
  152. //返佣 +
  153. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  154. ->where('add_time', '>', $search_time)
  155. ->where('pm', 1)
  156. ->sum('number');
  157. //退款退的佣金 -
  158. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  159. ->where('add_time', '>', $search_time)
  160. ->where('pm', 0)
  161. ->sum('number');
  162. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  163. if ($broken_commission < 0)
  164. $broken_commission = 0;
  165. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  166. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  167. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  168. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  169. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  170. $extractInfo = [
  171. 'uid' => $uid,
  172. 'real_name' => $user['nickname'],
  173. 'extract_type' => 'balance',
  174. 'extract_price' => $price,
  175. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  176. 'add_time' => time(),
  177. 'status' => 1
  178. ];
  179. $res4 = UserExtract::create($extractInfo);
  180. $res = $res2 && $res1 && $res3;
  181. self::checkTrans($res);
  182. if ($res) {
  183. event('ImportNowMoney', [$uid, $price]);
  184. }
  185. return $res;
  186. } catch (\Exception $e) {
  187. self::rollbackTrans();
  188. return self::setErrorInfo($e->getMessage());
  189. }
  190. }
  191. }