From 7abdcd8952fde1f1fb56d2656e971ba387d6df10 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Mon, 24 Jul 2023 16:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=91=E8=B5=B7=E6=8F=90?= =?UTF-8?q?=E7=8E=B0=E3=80=81=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/finance/WithdrawController.php | 38 ++++++++++++ app/api/controller/UserController.php | 18 ++++++ app/api/logic/UserLogic.php | 61 +++++++++++++++++-- app/common/model/user/Withdraw.php | 16 +++++ 4 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 app/adminapi/controller/finance/WithdrawController.php create mode 100644 app/common/model/user/Withdraw.php diff --git a/app/adminapi/controller/finance/WithdrawController.php b/app/adminapi/controller/finance/WithdrawController.php new file mode 100644 index 000000000..d1adb5863 --- /dev/null +++ b/app/adminapi/controller/finance/WithdrawController.php @@ -0,0 +1,38 @@ +getPage(); + $query = Withdraw::where('status', '<>', -1); + $count = $query->count(); + $list = $query->order('id', 'desc')->page($page)->limit($limit)->select()->toArray(); + return $this->success('success', ['count' => $count, 'data' => $list]); + } + + public function update($id) + { + $status = $this->request->param('status'); + if (!in_array($status, [1, 2, 3])) { + return $this->fail('参数错误'); + } + $data = Withdraw::find($id); + if (empty($data)) { + return $this->fail('数据不存在'); + } + $data->status = $status; + $data->udpate_time = time(); + $data->save(); + return $this->success('操作成功', [], 1, 1); + } + +} \ No newline at end of file diff --git a/app/api/controller/UserController.php b/app/api/controller/UserController.php index 53dea7b8e..a42ebff45 100755 --- a/app/api/controller/UserController.php +++ b/app/api/controller/UserController.php @@ -144,4 +144,22 @@ class UserController extends BaseApiController return $this->fail(UserLogic::getError()); } + public function withdraw() + { + $params = $this->request->param(); + $params['user_id'] = $this->userId; + $result = UserLogic::withdraw($params); + if($result) { + return $this->success('提现申请已提交', [], 1, 1); + } + return $this->fail(UserLogic::getError()); + } + + public function withdrawList() + { + [$page, $limit] = $this->getPage(); + [$count, $list] = UserLogic::withdrawList($this->userId, $page, $limit); + return $this->success('success', ['count' => $count, 'data' => $list]); + } + } \ No newline at end of file diff --git a/app/api/logic/UserLogic.php b/app/api/logic/UserLogic.php index 29b803461..87a17e0ee 100755 --- a/app/api/logic/UserLogic.php +++ b/app/api/logic/UserLogic.php @@ -14,17 +14,20 @@ namespace app\api\logic; - use app\common\{enum\notice\NoticeEnum, enum\user\UserTerminalEnum, enum\YesNoEnum, logic\BaseLogic, model\user\User, model\user\UserAuth, + model\user\Withdraw, service\sms\SmsDriver, - service\wechat\WeChatMnpService}; -use app\Request; + service\wechat\WeChatMnpService +}; +use think\db\exception\DataNotFoundException; +use think\exception\ValidateException; use think\facade\Config; +use think\facade\Db; /** * 会员逻辑层 @@ -118,7 +121,7 @@ class UserLogic extends BaseLogic public static function hasWechatAuth(int $userId) { //是否有微信授权登录 - $terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA,UserTerminalEnum::PC]; + $terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA, UserTerminalEnum::PC]; $auth = UserAuth::where(['user_id' => $userId]) ->whereIn('terminal', $terminal) ->findOrEmpty(); @@ -289,4 +292,54 @@ class UserLogic extends BaseLogic } } + public static function withdraw($params) + { + Db::startTrans(); + try { + $user = User::findOrEmpty($params['user_id']); + if ($user->isEmpty()) { + throw new DataNotFoundException('用户不存在'); + } + + // 校验提现金额 + if ($params['amount'] <= 0) { + throw new ValidateException('提现金额错误'); + } + + // 校验提现金额 + if ($params['amount'] > $user['user_money']) { + throw new ValidateException('余额不足'); + } + + // 保存提现记录 + $withdraw = new Withdraw(); + $withdraw->user_id = $user['id']; + $withdraw->admin_id = $user['admin_id']; + $withdraw->order_sn = $withdraw->buildOrderSn(); + $withdraw->amount = $params['amount']; + $withdraw->create_time = time(); + $withdraw->update_time = time(); + $withdraw->save(); + + // 扣除余额 + $user->user_money = $user['user_money'] - $params['amount']; + $user->save(); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + public static function withdrawList($userId, $page, $limit) + { + $query = Withdraw::where('user_id', $userId); + $count = $query->count(); + $list = $query->order('id', 'desc')->page($page)->limit($limit)->select()->toArray(); + return [$count, $list]; + } + } \ No newline at end of file diff --git a/app/common/model/user/Withdraw.php b/app/common/model/user/Withdraw.php new file mode 100644 index 000000000..990c74957 --- /dev/null +++ b/app/common/model/user/Withdraw.php @@ -0,0 +1,16 @@ +