浏览代码

feat(balpay): 新增余额转账功能

- 添加转账API路由和TransferController控制器
- 实现转账功能的后端逻辑,包括支付密码验证
- 添加防抖功能到前端用户ID输入事件
- 集成转账API调用和错误处理机制
- 实现转账确认对话框和成功后的数据刷新
- 添加相关API函数和页面组件更新
runphp 1 月之前
父节点
当前提交
5c1f308b43
共有 3 个文件被更改,包括 57 次插入0 次删除
  1. 8 0
      route/api.php
  2. 34 0
      src/Controller/TransferController.php
  3. 15 0
      src/Entity/ExtensionBalpayLogEntity.php

+ 8 - 0
route/api.php

@@ -0,0 +1,8 @@
+<?php
+declare(strict_types=1);
+
+
+use SixShop\Balpay\Controller\TransferController;
+use think\facade\Route;
+
+Route::post('transfer', [TransferController::class, 'save'])->middleware(['auth']);

+ 34 - 0
src/Controller/TransferController.php

@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Balpay\Controller;
+
+use app\model\User;
+use SixShop\Balpay\Entity\ExtensionBalpayLogEntity;
+use SixShop\Core\Request;
+use think\Response;
+use function SixShop\Core\success_response;
+use function SixShop\Core\throw_logic_exception;
+
+class TransferController
+{
+    public function save(Request $request, ExtensionBalpayLogEntity $extensionBalpayLogEntity): Response
+    {
+        $params = $request->post([
+            'user_id/d',
+            'amount/f',
+            'pay_password/s',
+        ]);
+        if (empty($params['pay_password'])) {
+            throw_logic_exception('请输入支付密码', status: 'balpay.pay_password_empty');
+        }
+        $payPassword = User::where('id', $request->userID)->value('pay_password');
+        if (empty($payPassword)) {
+            throw_logic_exception('请先设置支付密码', status: 'balpay.pay_password_not_set');
+        }
+        if (!password_verify($params['pay_password'], $payPassword)) {
+            throw_logic_exception('支付密码错误', status: 'balpay.pay_password_error');
+        }
+        $extensionBalpayLogEntity->transfer((int)$request->userID, $params['user_id'], $params['amount']);
+        return success_response(msg: '转账成功');
+    }
+}

+ 15 - 0
src/Entity/ExtensionBalpayLogEntity.php

@@ -51,4 +51,19 @@ class ExtensionBalpayLogEntity extends BaseEntity
             ->order('id', 'desc')
             ->paginate($pageAndLimit);
     }
+
+    /**
+     * 转账
+     *
+     * @param int $fromUserID 转账方用户ID
+     * @param int $toUserID 转账方用户ID
+     * @param float $amount 转账金额
+     */
+    public function transfer(int $fromUserID, int $toUserID, float $amount): void
+    {
+        $this->transaction(function () use ($fromUserID, $toUserID, $amount) {
+            $this->change($fromUserID, $amount, BalpayLogTypeEnum::CONSUMTION, sprintf('余额转出(UID:%d)', $toUserID));
+            $this->change($toUserID, $amount, BalpayLogTypeEnum::RECHARGE, sprintf('接收来自UID:%d的转账', $fromUserID));
+        });
+    }
 }