123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- <?php
- namespace App\Services;
- use App\Constants\StepStatus;
- use App\Models\Address;
- use App\Models\BalanceLog;
- use App\Models\Config;
- use App\Models\RoomUser;
- use App\Models\User;
- use App\Models\Wallet;
- use App\Models\Withdraw;
- use Illuminate\Support\Facades\Cache;
- use LaravelLang\Publisher\Console\Add;
- use Telegram\Bot\Api;
- use Telegram\Bot\Exceptions\TelegramSDKException;
- class WithdrawService
- {
- private $serviceCharge;
- public static function model(): string
- {
- return Withdraw::class;
- }
- public static function enum(): string
- {
- return '';
- }
- public static function getWhere(array $search = []): array
- {
- $where = [];
- if (isset($search['id']) && !empty($search['id'])) {
- $where[] = ['id', '=', $search['id']];
- }
- if (isset($search['member_id']) && !empty($search['member_id'])) {
- $where[] = ['member_id', '=', $search['member_id']];
- }
- if (isset($search['status']) && $search['status'] != '') {
- $where[] = ['status', '=', $search['status']];
- }
- return $where;
- }
- public static function findOne(array $search): ?Withdraw
- {
- return self::model()::where(self::getWhere($search))->first();
- }
- public static function findAll(array $search = [])
- {
- return self::model()::where(self::getWhere($search))->get();
- }
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit']) ? $search['limit'] : 10;
- $paginator = self::model()::where(self::getWhere($search))
- ->orderBy('created_at', 'desc')->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
- public function __construct()
- {
- $serviceCharge = Config::where('field', 'service_charge')->first()->val;
- $serviceCharge = floatval($serviceCharge);
- $this->serviceCharge = $serviceCharge;
- }
- public static function notify($data)
- {
- try {
- $telegram = new Api(config('services.telegram.token'));
- $telegram->sendMessage($data);
- } catch (TelegramSDKException $e) {
- return false;
- }
- return true;
- }
- //提现账单
- public function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
- {
- $list = Withdraw::where('member_id', $chatId)
- ->orderBy('id', 'desc')
- ->forPage($page, $limit)
- ->get();
- $count = Withdraw::where('member_id', $chatId)
- ->whereIn('status', [0, 1])
- ->count();
- $status = ['等待放行', '已到账', "失败"];
- $text = "👤 {$firstName}({$chatId}) 钱包提现记录\n\n";
- foreach ($list as $item) {
- $amount = floatval($item->amount);
- $balance = floatval($item->after_balance);
- $text .= "-------------------------------------\n";
- $text .= "➖ {$amount} USDT\n余额:{$balance} \n";
- $text .= "类别:提现\n";
- $text .= "状态:{$status[$item->status]}\n";
- if ($item->remark) {
- $text .= "说明:{$item->remark}\n";
- }
- $text .= "日期:{$item->created_at}\n";
- }
- if ($page > 1) {
- $keyboard[] = [
- ['text' => "👆上一页", 'callback_data' => "withdrawBillNextPage@@" . ($page - 1)]
- ];
- }
- $allPage = ceil($count / $limit);
- if ($allPage > $page) {
- if ($page > 1) {
- $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)];
- } else {
- $keyboard[] = [
- ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)]
- ];
- }
- }
- $keyboard[] = [
- ['text' => "🔙返回", 'callback_data' => "withdraw@@home"]
- ];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //删除地址
- public static function delAddress($chatId, $id, $messageId)
- {
- Address::where('id', $id)
- ->where('member_id', $chatId)->delete();
- return WithdrawService::getAddress($chatId, $messageId);
- }
- //地址详情
- public static function addressDetails($chatId, $messageId, $id)
- {
- $text = "*TRC20 地址管理*\n\n";
- $address = Address::where('id', $id)
- ->where('member_id', $chatId)->first();
- $text .= "地址:{$address->address}\n";
- $text .= "别名:{$address->alias}";
- $keyboard = [
- [['text' => '❌删除该地址', 'callback_data' => "withdrawAddress@@del{$id}"]],
- [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@address']]
- ];
- return [
- 'chat_id' => $chatId,
- 'parse_mode' => 'MarkdownV2',
- 'text' => $text,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
- 'message_id' => $messageId
- ];
- }
- //输入别名
- public static function inputAlias($chatId, $alias, $messageId)
- {
- if (mb_strlen($alias) > 20) {
- return [
- 'chat_id' => $chatId,
- 'text' => "别名不能超过20个字,请重新输入",
- 'reply_to_message_id' => $messageId,
- ];
- }
- $address = Cache::get("{$chatId}_address");
- $a = new Address();
- $a->address = $address;
- $a->alias = $alias;
- $a->member_id = $chatId;
- $a->save();
- Cache::delete("{$chatId}_address");
- Cache::delete(get_step_key($chatId));
- return self::getAddress($chatId, $messageId);
- }
- //用户输入TRC20地址
- public static function inputAddress($chatId, $address, $messageId)
- {
- if (!preg_match('/^T[a-zA-Z1-9]{33}$/', $address)) {
- return [
- 'chat_id' => $chatId,
- 'text' => "地址输入不正确,请重新输入TRC20地址",
- 'reply_to_message_id' => $messageId,
- ];
- }
- Cache::put("{$chatId}_address", $address);
- Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_ALIAS);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入地址别名,便于区分多个地址",
- ];
- }
- //添加地址
- public static function addAddress($chatId, $messageId)
- {
- $text = "请输入新的TRC20地址\n";
- $keyboard = [[
- ['text' => '❌取消', 'callback_data' => "message@@close"],
- ]];
- Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_TRC20);
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //地址管理
- public static function getAddress($chatId, $messageId)
- {
- $text = "🏠 地址管理\n";
- $text .= "--------------------------\n";
- $list = Address::where('member_id', $chatId)
- ->get();
- $keyboard = [];
- foreach ($list as $item) {
- $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@detail{$item->id}"]];
- }
- if (count($list) < 5) {
- $keyboard[] = [['text' => "➕ 添加地址", 'callback_data' => "withdrawAddress@@add"]];
- }
- $keyboard[] = [['text' => "↩️返回", 'callback_data' => "withdraw@@home"]];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //设置提现地址
- public function setAddress($chatId, $address)
- {
- $address = trim($address);
- if (!$address) return [['chat_id' => $chatId, 'text' => "提现地址不能为空"]];
- $user = User::where('member_id', $chatId)->first();
- $user->usdt = $address;
- $user->save();
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- $text = "✅ 提现地址已设置\n\n";
- $text .= "请发送提现金额\n";
- $text .= "💰 当前可用USDT余额:{$temp}\n";
- $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
- $text .= "------------------------------------\n";
- $text .= "例如您要提现100 USDT\n";
- $text .= "那么请发送:【提现】100\n";
- return [['chat_id' => $chatId, 'text' => $text]];
- }
- public static function done($chatId, $messageId, $firstName)
- {
- $serviceCharge = (new WithdrawService())->serviceCharge;
- $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
- $address = Cache::get("{$chatId}_WITHDRAW_ADDRESS", '');
- if (!$amount || !$address) return WithdrawService::index($chatId, $firstName, $messageId);
- $real = bcsub($amount, $serviceCharge, 10);
- $real = floatval($real);
- if ($amount <= $serviceCharge) {
- return [
- 'chat_id' => $chatId,
- 'text' => "⚠️提现不能少于{$serviceCharge} USDT,请重试",
- 'message_id' => $messageId
- ];
- }
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- if ($amount > $temp) {
- return [
- 'chat_id' => $chatId,
- 'text' => "⚠️可用余额不足,请重试",
- 'message_id' => $messageId
- ];
- }
- $ru = RoomUser::where('member_id', $chatId)
- ->whereIn('status', [0, 1, 2, 3])
- ->first();
- if ($ru) {
- $text = "游戏未结算\n";
- $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => [[
- ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
- ]]])
- ];
- }
- $wallet = Wallet::where('member_id', $chatId)->first();
- $bl = new BalanceLog();
- $bl->member_id = $chatId;
- $bl->amount = $amount * -1;
- $bl->before_balance = $wallet->available_balance;
- $wallet->available_balance = bcsub($wallet->available_balance, $amount, 10);
- $wallet->save();
- $bl->after_balance = $wallet->available_balance;
- $bl->change_type = '提现';
- $withdraw = new Withdraw();
- $withdraw->member_id = $chatId;
- $withdraw->amount = $amount;
- $withdraw->service_charge = $serviceCharge;
- $withdraw->to_account = $real;
- $withdraw->address = $address;
- $withdraw->status = 0;
- $withdraw->after_balance = $wallet->available_balance;
- $withdraw->save();
- $bl->related_id = $withdraw->id;
- $bl->save();
- $temp = floatval($wallet->available_balance);
- $text = "✅ 提现申请已提交!\n\n";
- $text .= "钱包余额:{$temp} USDT\n";
- $text .= "提现金额:{$amount} USDT\n";
- $text .= "实际到账:{$real} USDT\n";
- $text .= "手续费:{$serviceCharge} USDT\n\n";
- $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
- Cache::delete(get_step_key($chatId));
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- ];
- }
- public static function chooseAddress($chatId, $firstName, $messageId, $id)
- {
- $serviceCharge = (new WithdrawService())->serviceCharge;
- $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
- if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
- $amount = floatval($amount);
- $real = bcsub($amount, $serviceCharge, 10);
- $real = floatval($real);
- $address = Address::where('id', $id)
- ->where('member_id', $chatId)->first();
- $text = "请确认\n\n";
- $text .= "手续费:{$serviceCharge} USDT\n";
- $text .= "提现金额:{$amount} USDT\n";
- $text .= "实际到账:{$real} USDT\n";
- $text .= "提现地址:{$address->address}\n";
- $keyboard = [
- [
- ['text' => '✅ 确认', 'callback_data' => "withdrawAddress@@done"],
- ],
- [
- ['text' => '❌取消', 'callback_data' => "message@@close"]
- ]
- ];
- Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //用户输入提现金额
- public function inputAmount($chatId, $amount, $messageId)
- {
- if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
- return [[
- 'chat_id' => $chatId,
- 'text' => "金额输入不正确,请发送提现数字",
- 'reply_to_message_id' => $messageId
- ]];
- }
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- if ($amount <= $this->serviceCharge) {
- return [[
- 'chat_id' => $chatId,
- 'text' => "⚠️提现不能少于{$this->serviceCharge} USDT,请重试",
- 'reply_to_message_id' => $messageId
- ]];
- }
- if ($amount > $temp) {
- return [[
- 'chat_id' => $chatId,
- 'text' => "⚠️可用余额不足,请重试",
- 'reply_to_message_id' => $messageId
- ]];
- }
- $ru = RoomUser::where('member_id', $chatId)
- ->whereIn('status', [0, 1, 2, 3])
- ->first();
- if ($ru) {
- $text = "*游戏未结算*\n";
- $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
- return [[
- 'chat_id' => $chatId,
- 'text' => $text,
- 'parse_mode' => 'MarkdownV2',
- 'reply_markup' => json_encode(['inline_keyboard' => [[
- ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
- ]]])
- ]];
- }
- $list = Address::where('member_id', $chatId)->get();
- $keyboard = [];
- foreach ($list as $item) {
- $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
- }
- $keyboard[] = [
- ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
- ['text' => '❌取消', 'callback_data' => "message@@close"]
- ];
- $text = "请直接选择下面的地址\n";
- $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
- Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
- Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
- return [[
- 'chat_id' => $chatId,
- 'text' => $text,
- 'reply_to_message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ]];
- }
- //申请提现
- public function apply($chatId, $messageId)
- {
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- $text = "请发送提现金额\n";
- $text .= "💰 当前可用USDT余额:{$temp}\n";
- $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
- // $keyboard = [[
- // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
- // ]];
- Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //提现管理
- public static function index($chatId, $firstName, $messageId = null)
- {
- $wallet = Wallet::where('member_id', $chatId)->first();
- $text = "👤 {$firstName}({$chatId})\n\n";
- $text .= "💰钱包余额\n";
- $temp = floatval($wallet->available_balance);
- $text .= "USDT:{$temp}\n";
- $text .= "--------------------------\n";
- $serviceAccount = Config::where('field', 'service_account')->first()->val;
- $keyboard = [
- [
- ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
- ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
- ],
- [
- ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
- ['text' => '👩 客服帮助', 'url' => "https://t.me/{$serviceAccount}"]
- ],
- [
- ['text' => '❌取消', 'callback_data' => "message@@close"],
- ]
- ];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- }
|