WithdrawLogic.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic;
  17. use app\common\enum\AccountLogEnum;
  18. use app\common\enum\UserTerminalEnum;
  19. use app\common\enum\WithdrawEnum;
  20. use app\common\logic\AccountLogLogic;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\User;
  23. use app\common\model\WithdrawApply;
  24. use app\common\service\ConfigService;
  25. use app\common\service\WeChatConfigService;
  26. use think\facade\Db;
  27. /**
  28. * 提现逻辑层
  29. * Class WithdrawLogic
  30. * @package app\shopapi\logic
  31. */
  32. class WithdrawLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 获取提现配置
  36. * @param $userId
  37. * @return array
  38. * @author Tab
  39. * @date 2021/8/6 17:06
  40. */
  41. public static function getConfig($userId,$terminal)
  42. {
  43. $user = User::findOrEmpty($userId)->toArray();
  44. $config = [
  45. 'able_withdraw' => $user['user_earnings'],
  46. 'min_withdraw' => ConfigService::get('config', 'withdraw_min_money', WithdrawEnum::DEFAULT_MIN_MONEY),
  47. 'max_withdraw' => ConfigService::get('config', 'withdraw_max_money', WithdrawEnum::DEFAULT_MAX_MONEY),
  48. 'percentage' => ConfigService::get('config', 'withdraw_service_charge', WithdrawEnum::DEFAULT_PERCENTAGE),
  49. ];
  50. $types = ConfigService::get('config', 'withdraw_way', WithdrawEnum::DEFAULT_TYPE);
  51. foreach($types as $value) {
  52. //h5和头条隐藏微信零钱提现
  53. if(in_array($terminal,[UserTerminalEnum::H5,UserTerminalEnum::TOUTIAO]) && WithdrawEnum::TYPE_WECHAT_CHANGE == $value){
  54. continue;
  55. }
  56. $config['type'][] = [
  57. 'name' => WithdrawEnum::getTypeDesc($value),
  58. 'value' => $value
  59. ];
  60. }
  61. return $config;
  62. }
  63. /**
  64. * @notes 提现申请
  65. * @param $params
  66. * @return bool
  67. * @author Tab
  68. * @date 2021/8/6 17:52
  69. */
  70. public static function apply($params)
  71. {
  72. Db::startTrans();
  73. try {
  74. // 手续费,单位:元
  75. $handlingFee = 0;
  76. if($params['type'] != WithdrawEnum::TYPE_BALANCE) {
  77. // 不是提现至余额,需收手续费
  78. $percentage = ConfigService::get('config', 'withdraw_service_charge', WithdrawEnum::DEFAULT_PERCENTAGE);
  79. $handlingFee = bcadd(($params['money'] * $percentage / 100), 0, 2);
  80. }
  81. $withdrawApply = new WithdrawApply();
  82. $data = [
  83. 'sn' => generate_sn($withdrawApply, 'sn'),
  84. 'batch_no' => generate_sn($withdrawApply, 'batch_no','SJZZ'),
  85. 'user_id' => $params['user_id'],
  86. 'real_name' => $params['real_name'] ?? '',
  87. 'account' => $params['account'] ?? '',
  88. 'type' => $params['type'],
  89. 'money' => $params['money'],
  90. 'left_money' => $params['money'] - $handlingFee,
  91. 'money_qr_code' => $params['money_qr_code'] ?? '',
  92. 'handling_fee' => $handlingFee,
  93. 'apply_remark' => $params['apply_remark'] ?? '',
  94. 'status' => WithdrawEnum::STATUS_WAIT,
  95. 'bank' => $params['bank'] ?? '',
  96. 'subbank' => $params['subbank'] ?? '',
  97. 'terminal' => request()->userInfo['terminal'],
  98. ];
  99. // 新增提现申请记录
  100. $withdrawApply->save($data);
  101. // 扣减用户可提现金额
  102. $user = User::find($params['user_id']);
  103. $user->user_earnings = $user->user_earnings - $params['money'];
  104. $user->save();
  105. // 添加账户流水记录
  106. AccountLogLogic::add($user->id, AccountLogEnum::BW_DEC_WITHDRAWAL, AccountLogEnum::DEC, $params['money'], $withdrawApply->sn, '提现申请');
  107. Db::commit();
  108. return $withdrawApply->id;
  109. } catch (\Exception $e) {
  110. Db::rollback();
  111. self::setError($e->getMessage());
  112. return false;
  113. }
  114. }
  115. /**
  116. * @notes 查看提现申请详情
  117. * @param $params
  118. * @return array
  119. * @author Tab
  120. * @date 2021/8/6 19:06
  121. */
  122. public static function detail($params)
  123. {
  124. $field = 'id,status,status as status_desc,money,sn,create_time,type,type as type_desc,handling_fee,left_money,money_qr_code,apply_remark,audit_remark,transfer_voucher,transfer_remark,account,real_name,bank,subbank,apply_remark,pay_desc,transfer_time,transfer_voucher,transfer_remark,terminal';
  125. $info = WithdrawApply::field($field)
  126. ->append([ 'wechat_change_wait_receive' ])
  127. ->findOrEmpty($params['id'])
  128. ->toArray();
  129. $info['pay_desc'] = json_decode($info['pay_desc'], true) ? : new \stdClass();
  130. $config = WeChatConfigService::getWechatConfigByTerminal($info['terminal']);
  131. $info['pay_config'] = [
  132. 'mch_id' => $config['mch_id'] ?? '',
  133. 'app_id' => $config['app_id'] ?? '',
  134. ];
  135. return $info;
  136. }
  137. // 收款
  138. public static function receive($id, $user_id)
  139. {
  140. $withdraw = WithdrawApply::where(['id' => $id, 'user_id' => $user_id])->find();
  141. if (empty($withdraw['id'])){
  142. return '提现记录不存在';
  143. }
  144. if ($withdraw['status'] != WithdrawEnum::STATUS_ING || $withdraw['type'] != WithdrawEnum::TYPE_WECHAT_CHANGE){
  145. return '当前状态不能收款';
  146. }
  147. $update = [
  148. 'status' => 3,
  149. 'update_time' => time(),
  150. ];
  151. WithdrawApply::update($update, [ [ 'id', '=', $withdraw['id'] ] ]);
  152. return true;
  153. }
  154. }