user = is_numeric($user) ? User::get($user) : $user; } // 提现规则 $config = shop_config('shop.withdraw'); $config['min_amount'] = BcMath::isZero($config['min_amount']) ? $config['min_amount'] : BcMath::format($config['min_amount'], 2); $config['max_amount'] = BcMath::isZero($config['max_amount']) ? $config['max_amount'] : BcMath::format($config['max_amount'], 2); $config['charge_rate_format'] = round(floatval($config['charge_rate']), 1); // 1 位小数 $config['charge_rate'] = BcMath::div($config['charge_rate'], '100', 3); $config['num_unit'] = $config['num_unit'] == 'day' ? '天' : '月'; $this->config = $config; } /** * 获取用户提现记录列表 * @param array $params 查询参数 * @return array 包含列表数据和统计信息 */ public function getWithdrawList($params = []) { // 构建查询条件数组 $arrWhere = ['user_id' => $this->user->id]; // 状态筛选 if (isset($params['status']) && $params['status'] !== '') { $arrWhere['status'] = (int)$params['status']; } // 年月筛选 (支持YYYY-MM格式) $timeWhere = []; if (isset($params['year_month']) && !empty($params['year_month'])) { $yearMonth = trim($params['year_month']); // 解析年月字符串 (YYYY-MM) if (preg_match('/^(\d{4})-(\d{2})$/', $yearMonth, $matches)) { $year = (int)$matches[1]; $month = (int)$matches[2]; // 按年月筛选 $startTime = mktime(0, 0, 0, $month, 1, $year); $endTime = mktime(23, 59, 59, $month + 1, 0, $year); $timeWhere = [ 'createtime' => ['between', [$startTime, $endTime]] ]; } } // 合并查询条件 $finalWhere = array_merge($arrWhere, $timeWhere); // 计算筛选条件下的总金额(使用BC数学函数) $totalAmount = '0.00'; $totalChargeAmount = '0.00'; $totalActualAmount = '0.00'; $totalCount = 0; // 执行统计查询 $statsRecords = WithdrawModel::where($finalWhere)->field(['amount', 'charge_fee'])->select(); foreach ($statsRecords as $record) { $totalAmount = BcMath::add($totalAmount, $record->amount, 2); $totalChargeAmount = BcMath::add($totalChargeAmount, $record->charge_fee, 2); $actualAmount = BcMath::sub($record->amount, $record->charge_fee, 2); $totalActualAmount = BcMath::add($totalActualAmount, $actualAmount, 2); $totalCount++; } // 分页参数 $page_size = isset($params['page_size']) ? (int)$params['page_size'] : 10; // 查询提现记录(使用相同的查询条件) $withdraws = WithdrawModel::where($finalWhere) ->order('id desc') ->paginate($page_size) ->each(function ($withdraw) { // 隐藏敏感信息 $withdraw->hidden(['withdraw_info']); }); // 组装返回数据 $result = [ 'list' => $withdraws, 'summary' => [ 'total_count' => $totalCount, 'total_amount' => $totalAmount, 'total_charge_amount' => $totalChargeAmount, 'total_actual_amount' => $totalActualAmount, ], 'filter_info' => [ 'year_month' => isset($params['year_month']) ? $params['year_month'] : null, 'status' => isset($params['status']) ? (int)$params['status'] : null, ] ]; return $result; } /** * 获取提现状态列表 * @return array */ public function getStatusList() { return [ -3 => '撤销提现', -2 => '提现失败', -1 => '已拒绝', 0 => '待审核', 1 => '处理中', 2 => '已处理' ]; } // 发起提现申请 public function apply($params) { $type = $params['type']; $money = $params['amount'] ?? 0; // money 改为 amount $money = (string)$money; // 手续费 $charge = BcMath::mul($money, $this->config['charge_rate'], 2); // 检查提现规则 $this->checkApply($type, $money, $charge); // 获取账号信息 $withdrawInfo = $this->getAccountInfo($type, $params); // 添加提现记录 $withdraw = new WithdrawModel(); $withdraw->user_id = $this->user->id; $withdraw->amount = $money; $withdraw->charge_fee = $charge; $withdraw->charge_rate = $this->config['charge_rate']; $withdraw->withdraw_sn = get_sn($this->user->id, 'W'); $withdraw->withdraw_type = $type; $withdraw->withdraw_info = $withdrawInfo; $withdraw->status = 0; $withdraw->platform = request()->header('platform'); $withdraw->save(); // 佣金钱包变动 WalletService::change($this->user, 'commission', '-' . BcMath::add($charge, $money, 2), 'withdraw', [ 'withdraw_id' => $withdraw->id, 'amount' => $withdraw->amount, 'charge_fee' => $withdraw->charge_fee, 'charge_rate' => $withdraw->charge_rate, ]); $this->handleLog($withdraw, '用户发起提现申请', $this->user); return $withdraw; } // 继续提现 public function retry($params) { $withdraw = WithdrawModel::where('user_id', $this->user->id) ->where('withdraw_sn', $params['withdraw_sn']) ->where('status', 1) ->where('withdraw_type', 'wechat') ->find(); if (!$withdraw) { throw new BusinessException('提现记录不存在'); } if (request()->header('platform') !== $withdraw->platform) { throw new BusinessException('请在发起该次提现的平台操作'); } $this->handleLog($withdraw, '用户重新发起提现申请'); // 实时检查提现单状态 return $this->checkWechatTransferResult($withdraw); } // 取消提现(适用于微信商家转账) public function cancel($params) { $withdraw = WithdrawModel::where('user_id', $this->user->id) ->where('withdraw_sn', $params['withdraw_sn']) ->where('status', 1) ->where('withdraw_type', 'wechat') ->find(); if (!$withdraw) { throw new BusinessException('提现记录不存在'); } if (request()->header('platform') !== $withdraw->platform) { throw new BusinessException('请在发起该次提现的平台操作'); } // 实时检查提现单状态 $withdraw = $this->checkWechatTransferResult($withdraw); if ($withdraw->wechat_transfer_state !== 'WAIT_USER_CONFIRM') { throw new BusinessException('该提现单暂无法发起取消操作,请联系客服'); } // 提交微信撤销单 $payService = new PayService($withdraw->withdraw_type, $withdraw->platform); try { $result = $payService->cancelTransferOrder([ 'withdraw_sn' => $withdraw->withdraw_sn, ]); } catch (\Exception $e) { \think\Log::error('撤销提现失败: ' . ': 行号: ' . $e->getLine() . ': 文件: ' . $e->getFile() . ': 错误信息: ' . $e->getMessage()); $this->handleLog($withdraw, '撤销微信商家转账失败: ' . $e->getMessage()); throw new BusinessException($e->getMessage()); } $withdraw->wechat_transfer_state = $result['state']; $withdraw->save(); $this->handleLog($withdraw, '申请撤销微信商家转账成功,报文信息:' . json_encode($result, JSON_UNESCAPED_UNICODE)); return $withdraw; } // 新版本微信商家转账 // https://pay.weixin.qq.com/doc/v3/merchant/4012711988 public function handleWechatTransfer($withdraw) { $payService = new PayService($withdraw->withdraw_type, $withdraw->platform); $payload = [ '_action' => 'mch_transfer', // 新版转账 'out_bill_no' => $withdraw->withdraw_sn, 'transfer_scene_id' => '1005', 'openid' => $withdraw->withdraw_info['微信ID'] ?? '', 'user_name' => $withdraw->withdraw_info['真实姓名'] ?? '', // 明文传参即可,sdk 会自动加密 'transfer_amount' => $withdraw->amount, 'transfer_remark' => "用户[" . ($withdraw->withdraw_info['微信用户'] ?? '') . "]提现", 'transfer_scene_report_infos' => [ ['info_type' => '岗位类型', 'info_content' => '推广专员'], ['info_type' => '报酬说明', 'info_content' => '推广佣金报酬'], ], ]; try { list($response, $transferData) = $payService->transfer($payload); $withdraw->status = 1; // 提现处理中 $withdraw->wechat_transfer_state = $response['state']; $withdraw->payment_json = json_encode($response, JSON_UNESCAPED_UNICODE); $withdraw->save(); $this->handleLog($withdraw, '用户发起微信商家转账收款,报文信息:' . json_encode($response, JSON_UNESCAPED_UNICODE), $this->user); return $transferData; } catch (\Exception $e) { \think\Log::error('提现失败: ' . ': 行号: ' . $e->getLine() . ': 文件: ' . $e->getFile() . ': 错误信息: ' . $e->getMessage()); // $withdraw->wechat_transfer_state = 'NOT_FOUND'; $withdraw->save(); $this->handleFail($withdraw, $e->getMessage()); throw new BusinessException($e->getMessage()); } // 发起提现失败,自动处理退还佣金 } // 支付宝提现 public function handleAlipayWithdraw($withdraw) { Db::startTrans(); try { $withdraw = $this->handleAgree($withdraw, $this->user); $withdraw = $this->handleWithdraw($withdraw, $this->user); Db::commit(); } catch (BusinessException $e) { Db::commit(); // 不回滚,记录错误日志 throw new BusinessException($e->getMessage()); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); throw new BusinessException($message); } catch (\Exception $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } // 同意操作 public function handleAgree($withdraw, $oper = null) { if ($withdraw->status != 0) { throw new BusinessException('请勿重复操作'); } $withdraw->status = 1; $withdraw->save(); return $this->handleLog($withdraw, '同意提现申请', $oper); } // 处理打款 public function handleWithdraw($withdraw, $oper = null) { $withDrawStatus = false; if ($withdraw->status != 1) { throw new BusinessException('请勿重复操作'); } if ($withdraw->withdraw_type !== 'bank') { $withDrawStatus = $this->handleTransfer($withdraw); } else { $withDrawStatus = true; } if ($withDrawStatus) { $withdraw->status = 2; $withdraw->paid_fee = $withdraw->amount; $withdraw->save(); return $this->handleLog($withdraw, '已打款', $oper); } return $withdraw; } // 拒绝操作 public function handleRefuse($withdraw, $refuse_msg) { if ($withdraw->status != 0 && $withdraw->status != 1) { throw new BusinessException('请勿重复操作'); } $withdraw->status = -1; $withdraw->save(); // 退回用户佣金 WalletService::change($this->user, 'commission', BcMath::add($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [ 'withdraw_id' => $withdraw->id, 'amount' => $withdraw->amount, 'charge_fee' => $withdraw->charge_fee, 'charge_rate' => $withdraw->charge_rate, ]); return $this->handleLog($withdraw, '拒绝:' . $refuse_msg); } // 失败 public function handleFail($withdraw, $refuse_msg, $status = -2) { if ($withdraw->status != 0 && $withdraw->status != 1) { throw new BusinessException('请勿重复操作'); } $withdraw->status = $status; $withdraw->save(); // 退回用户佣金 WalletService::change($this->user, 'commission', BcMath::add($withdraw->charge_fee, $withdraw->amount, 2), 'withdraw_error', [ 'withdraw_id' => $withdraw->id, 'amount' => $withdraw->amount, 'charge_fee' => $withdraw->charge_fee, 'charge_rate' => $withdraw->charge_rate, ]); return $this->handleLog($withdraw, '提现失败,已退还提现佣金:' . $refuse_msg); } // 商家转账回调 public function handleWechatTransferNotify($action, $withdrawSn, $originData) { $withdraw = WithdrawModel::where('withdraw_sn', $withdrawSn)->find(); if (!$withdraw) { throw new BusinessException('提现记录不存在'); } if ($withdraw->status !== 1) { throw new BusinessException('提现记录状态不正确'); } $this->user = User::get($withdraw->user_id); if (!$this->user) { throw new BusinessException('用户不存在'); } $withdraw->wechat_transfer_state = $action; switch ($action) { case 'SUCCESS': $withdraw->status = 2; $this->handleLog($withdraw, '微信商家转账成功,报文信息: ' . json_encode($originData, JSON_UNESCAPED_UNICODE), $this->user); $withdraw->save(); break; case 'FAILED': $this->handleFail($withdraw, '微信商家转账失败,报文信息: ' . json_encode($originData, JSON_UNESCAPED_UNICODE), -2); break; case 'CANCELLED': // 撤销完成 $this->handleFail($withdraw, '微信商家转账撤销成功,报文信息: ' . json_encode($originData, JSON_UNESCAPED_UNICODE), -3); break; default: $this->handleLog($withdraw, '回调结果不是终态,报文信息: ' . json_encode($originData, JSON_UNESCAPED_UNICODE), $this->user); } return true; } // 检查微信商家转账单结果 public function checkWechatTransferResult($withdraw) { if ($withdraw->createtime < strtotime('-30 day')) { throw new BusinessException('提现单据已过期,请联系客服解决'); } $payService = new PayService($withdraw->withdraw_type, $withdraw->platform); $result = $payService->queryTransferOrder([ 'withdraw_sn' => $withdraw->withdraw_sn, ]); $withdraw->wechat_transfer_state = $result['state']; $withdraw->save(); $this->handleLog($withdraw, '查询微信商家转账单,报文信息:' . json_encode($result, JSON_UNESCAPED_UNICODE)); return $withdraw; } // 企业付款提现 private function handleTransfer($withdraw) { $type = $withdraw->withdraw_type; $platform = $withdraw->platform; $payService = new PayService($type, $platform); if ($type == 'alipay') { $payload = [ 'out_biz_no' => $withdraw->withdraw_sn, 'trans_amount' => $withdraw->amount, 'product_code' => 'TRANS_ACCOUNT_NO_PWD', 'biz_scene' => 'DIRECT_TRANSFER', // 'order_title' => '余额提现到', 'remark' => '用户提现', 'payee_info' => [ 'identity' => $withdraw->withdraw_info['支付宝账户'] ?? '', 'identity_type' => 'ALIPAY_LOGON_ID', 'name' => $withdraw->withdraw_info['真实姓名'] ?? '', ] ]; } try { list($code, $response) = $payService->transfer($payload); if ($code === 1) { $withdraw->payment_json = json_encode($response, JSON_UNESCAPED_UNICODE); $withdraw->save(); return true; } throw new BusinessException(json_encode($response, JSON_UNESCAPED_UNICODE)); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); throw new BusinessException($message); } catch (\Exception $e) { \think\Log::error('提现失败:' . ' 行号:' . $e->getLine() . '文件:' . $e->getFile() . '错误信息:' . $e->getMessage()); $this->handleLog($withdraw, '提现失败:' . $e->getMessage()); throw new BusinessException($e->getMessage()); // 弹出错误信息 } return false; } // 添加日志 private function handleLog($withdraw, $oper_info, $oper = null) { $oper = Operator::get($oper); WithdrawLogModel::insert([ 'withdraw_id' => $withdraw->id, 'content' => $oper_info, 'oper_type' => $oper['type'], 'oper_id' => $oper['id'], 'createtime' => time() ]); return $withdraw; } // 提现条件检查 private function checkApply($type, $money, $charge) { if (!in_array($type, $this->config['methods'])) { throw new BusinessException('暂不支持该提现方式'); } // 使用BC数学函数检查金额是否大于0 if (!BcMath::isPositive($money)) { throw new BusinessException('请输入正确的提现金额'); } // 检查最小提现金额 if (BcMath::isPositive($this->config['min_amount']) && BcMath::comp($money, $this->config['min_amount']) < 0) { throw new BusinessException('提现金额不能少于 ' . $this->config['min_amount'] . '元'); } // 检查最大提现金额 if (BcMath::isPositive($this->config['max_amount']) && BcMath::comp($money, $this->config['max_amount']) > 0) { throw new BusinessException('提现金额不能大于 ' . $this->config['max_amount'] . '元'); } // 检查用户佣金余额是否足够 $totalAmount = BcMath::add($charge, $money, 2); if (BcMath::comp($this->user->commission, $totalAmount) < 0) { throw new BusinessException('可提现佣金不足'); } // 检查最大提现次数 if (isset($this->config['max_num']) && $this->config['max_num'] > 0) { $start_time = $this->config['num_unit'] == 'day' ? strtotime(date('Y-m-d', time())) : strtotime(date('Y-m', time())); $num = WithdrawModel::where('user_id', $this->user->id)->where('createtime', '>=', $start_time)->count(); if ($num >= $this->config['max_num']) { throw new BusinessException('每' . ($this->config['num_unit'] == 'day' ? '日' : '月') . '提现次数不能大于 ' . $this->config['max_num'] . '次'); } } } // 获取提现账户信息 private function getAccountInfo($type, $params) { // 根据账户ID查询账户信息 $account = \app\common\model\user\Account::where([ 'id' => $params['account_id'], 'user_id' => $this->user->id, 'type' => $type ])->find(); if (!$account) { throw new BusinessException('账户信息不存在'); } $platform = request()->header('platform'); switch ($type) { case 'wechat': if ($platform == 'App') { $platform = 'openPlatform'; } elseif (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) { $platform = lcfirst(str_replace('Wechat', '', $platform)); } $thirdOauth = ThirdOauth::where('provider', 'wechat')->where('platform', $platform)->where('user_id', $this->user->id)->find(); if (!$thirdOauth) { throw new BusinessException('请先绑定微信账号', -1); } $withdrawInfo = [ '真实姓名' => $account->account_name, '微信用户' => $thirdOauth['nickname'] ?: $this->user['nickname'], '微信ID' => $thirdOauth['openid'], ]; break; case 'alipay': $withdrawInfo = [ '真实姓名' => $account->account_name, '支付宝账户' => $account->account_no ]; break; case 'bank': $withdrawInfo = [ '真实姓名' => $account->account_name, '开户行' => $account->account_header, '银行卡号' => $account->account_no ]; break; } if (!isset($withdrawInfo)) { throw new BusinessException('您的提现信息有误'); } return $withdrawInfo; } }