| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\api\controller\invitation;
- use think\facade\Db;
- use app\common\controller\Frontend;
- use app\common\service\CommissionService;
- class Withdrawal extends Frontend
- {
- protected $commissionService;
- public function initialize(): void
- {
- parent::initialize();
- $this->commissionService = new CommissionService();
- }
- /**
- * 申请提现
- */
- public function apply(): void
- {
- if (!$this->request->isPost()) {
- $this->error('非法请求');
- }
- $userId = $this->auth->id;
- $amount = $this->request->post('amount', 0);
- $withdrawalMethod = $this->request->post('withdrawal_method', 'wechat');
- $accountInfo = $this->request->post('account_info', '');
- $remark = $this->request->post('remark', '');
- if ($amount <= 0) {
- $this->error('提现金额必须大于0');
- }
- if (!in_array($withdrawalMethod, ['wechat', 'alipay', 'bank'])) {
- $this->error('提现方式错误');
- }
- try {
- // 检查用户佣金是否足够
- $commission = Db::name('ba_user_commission')
- ->where('user_id', $userId)
- ->find();
- if (!$commission || $commission['current_commission'] < $amount) {
- $this->error('佣金余额不足');
- }
- // 生成提现单号
- $withdrawalNo = 'TX' . date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
- // 创建提现记录
- $data = [
- 'withdrawal_no' => $withdrawalNo,
- 'user_id' => $userId,
- 'amount' => $amount,
- 'withdrawal_method' => $withdrawalMethod,
- 'account_info' => $accountInfo,
- 'current_balance' => $commission['current_commission'],
- 'balance_after' => $commission['current_commission'] - $amount,
- 'status' => 0, // 待审核
- 'remark' => $remark,
- 'create_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $result = Db::name('ba_withdrawal_record')->insert($data);
- if ($result) {
- $this->success('提现申请提交成功,等待审核', [
- 'withdrawal_no' => $withdrawalNo
- ]);
- } else {
- $this->error('提现申请失败');
- }
- } catch (\Throwable $e) {
- $this->error('申请失败:' . $e->getMessage());
- }
- }
- /**
- * 获取提现记录
- */
- public function records(): void
- {
- $userId = $this->auth->id;
- $page = $this->request->get('page', 1);
- $pageSize = $this->request->get('pageSize', 10);
- try {
- $list = Db::name('ba_withdrawal_record')
- ->where('user_id', $userId)
- ->order('create_time', 'desc')
- ->paginate([
- 'page' => $page,
- 'list_rows' => $pageSize
- ]);
- $this->success('', [
- 'list' => $list->items(),
- 'total' => $list->total(),
- 'page' => $list->currentPage(),
- 'page_size' => $list->listRows()
- ]);
- } catch (\Throwable $e) {
- $this->error('获取提现记录失败');
- }
- }
- /**
- * 审核提现(管理员用)
- */
- public function audit(): void
- {
- if (!$this->auth->isLogin() || !$this->auth->isAdmin()) {
- $this->error('权限不足');
- }
- $withdrawalId = $this->request->post('withdrawal_id', 0);
- $status = $this->request->post('status', 0);
- $auditRemark = $this->request->post('audit_remark', '');
- if ($withdrawalId <= 0) {
- $this->error('参数错误');
- }
- if (!in_array($status, [1, 2])) { // 1=通过,2=拒绝
- $this->error('状态错误');
- }
- try {
- if ($status == 1) {
- // 审核通过,处理提现
- $result = $this->commissionService->processWithdrawal($withdrawalId);
- } else {
- // 审核拒绝
- $result = Db::name('ba_withdrawal_record')
- ->where('id', $withdrawalId)
- ->update([
- 'status' => 2, // 审核拒绝
- 'audit_time' => date('Y-m-d H:i:s'),
- 'audit_remark' => $auditRemark,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- $result = $result !== false;
- }
- if ($result) {
- $this->success('审核操作成功');
- } else {
- $this->error('审核操作失败');
- }
- } catch (\Throwable $e) {
- $this->error('审核失败:' . $e->getMessage());
- }
- }
- }
|