WithdrawService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Constants\StepStatus;
  5. use App\Models\ActivityUser;
  6. use App\Models\Address;
  7. use App\Models\BalanceLog;
  8. use App\Models\Bank;
  9. use App\Models\Config;
  10. use App\Models\RoomUser;
  11. use App\Models\User;
  12. use App\Models\Wallet;
  13. use App\Models\Withdraw;
  14. use Exception;
  15. use Illuminate\Support\Facades\Cache;
  16. use Illuminate\Support\Facades\DB;
  17. use LaravelLang\Publisher\Console\Add;
  18. use Telegram\Bot\Api;
  19. use Telegram\Bot\Exceptions\TelegramSDKException;
  20. class WithdrawService
  21. {
  22. public $serviceCharge;
  23. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  24. {
  25. switch ($data) {
  26. case "withdraw@@address"://地址管理
  27. $res = WithdrawService::getAddress($chatId, $messageId);
  28. $telegram->editMessageText($res);
  29. break;
  30. case "withdrawAddress@@done":
  31. $res = WithdrawService::done($chatId, $messageId, $firstName);
  32. $telegram->editMessageText($res);
  33. break;
  34. case "withdraw@@bill"://点击提现的账单按钮
  35. $res = WithdrawService::bill($chatId, $firstName, $messageId);
  36. $telegram->editMessageText($res);
  37. break;
  38. case "withdrawAddress@@add":
  39. $res = WithdrawService::addAddress($chatId, $messageId);
  40. $telegram->editMessageText($res);
  41. break;
  42. case "withdraw@@apply"://点击提现按钮
  43. $res = WithdrawService::apply($chatId, $messageId);
  44. $telegram->editMessageText($res);
  45. break;
  46. default:
  47. //选择提现地址
  48. $pattern = "/^withdrawAddress@@choose\d+$/";
  49. if (preg_match($pattern, $data)) {
  50. $id = preg_replace('/^withdrawAddress@@choose/', '', $data);
  51. $res = WithdrawService::chooseAddress($chatId, $firstName, $messageId, $id);
  52. $telegram->editMessageText($res);
  53. }
  54. //删除地址
  55. $pattern = "/^withdrawAddress@@del\d+$/";
  56. if (preg_match($pattern, $data)) {
  57. $id = preg_replace('/^withdrawAddress@@del/', '', $data);
  58. $res = WithdrawService::delAddress($chatId, $id, $messageId);
  59. $telegram->editMessageText($res);
  60. }
  61. //地址详情
  62. $pattern = "/^withdrawAddress@@detail\d+$/";
  63. if (preg_match($pattern, $data)) {
  64. $id = preg_replace('/^withdrawAddress@@detail/', '', $data);
  65. $res = WithdrawService::addressDetails($chatId, $messageId, $id);
  66. $telegram->editMessageText($res);
  67. }
  68. //提现账单,下一页
  69. $pattern = "/^withdrawBillNextPage@@\d+$/";
  70. if (preg_match($pattern, $data)) {
  71. $page = preg_replace('/^withdrawBillNextPage@@/', '', $data);
  72. $page = intval($page);
  73. $res = WithdrawService::bill($chatId, $firstName, $messageId, $page);
  74. $telegram->editMessageText($res);
  75. }
  76. break;
  77. }
  78. }
  79. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  80. {
  81. switch ($stepStatus) {
  82. case StepStatus::INPUT_WITHDRAW_MONEY:
  83. $res = WithdrawService::inputAmount($chatId, $text, $messageId);
  84. $res = $res[0];
  85. break;
  86. case StepStatus::INPUT_ADDRESS_TRC20:
  87. $res = WithdrawService::inputAddress($chatId, $text, $messageId);
  88. break;
  89. case StepStatus::LEGACY_INPUT_ADDRESS_ALIAS:
  90. $address = Cache::get("{$chatId}_address");
  91. $res = is_string($address) && $address !== ''
  92. ? self::inputAddress($chatId, $address, $messageId)
  93. : self::addAddress($chatId, $messageId);
  94. break;
  95. case StepStatus::INPUT_WITHDRAW_REAL_NAME:
  96. $address = Cache::get("{$chatId}_address");
  97. $res = is_string($address) && $address !== ''
  98. ? self::saveAddress($chatId, $address, $messageId, $text)
  99. : self::addAddress($chatId, $messageId);
  100. break;
  101. default:
  102. $res = null;
  103. break;
  104. }
  105. return $res;
  106. }
  107. public static function model(): string
  108. {
  109. return Withdraw::class;
  110. }
  111. public static function enum(): string
  112. {
  113. return '';
  114. }
  115. public static function getWhere(array $search = []): array
  116. {
  117. $where = [];
  118. if (isset($search['id']) && !empty($search['id'])) {
  119. $where[] = ['withdraws.id', '=', $search['id']];
  120. }
  121. if (isset($search['member_id']) && !empty($search['member_id'])) {
  122. $where[] = ['withdraws.member_id', '=', $search['member_id']];
  123. }
  124. if (isset($search['status']) && $search['status'] != '') {
  125. $where[] = ['withdraws.status', '=', $search['status']];
  126. }
  127. if (!empty($search['account'])) {
  128. $where[] = ['users.account', 'like', '%' . trim((string)$search['account']) . '%'];
  129. }
  130. if (isset($search['first_name']) && !empty($search['first_name'])) {
  131. $where[] = ['users.first_name', 'like', "%" . $search['first_name'] . "%"];
  132. }
  133. return $where;
  134. }
  135. public static function findOne(array $search): ?Withdraw
  136. {
  137. return self::model()::where(self::getWhere($search))->first();
  138. }
  139. public static function findAll(array $search = [])
  140. {
  141. return self::model()::where(self::getWhere($search))->get();
  142. }
  143. public static function paginate(array $search = [])
  144. {
  145. $limit = isset($search['limit']) ? $search['limit'] : 10;
  146. $paginator = self::model()::where(self::getWhere($search))
  147. ->orderBy('created_at', 'desc')->paginate($limit);
  148. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  149. }
  150. public function __construct()
  151. {
  152. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  153. $serviceCharge = floatval($serviceCharge);
  154. $this->serviceCharge = $serviceCharge;
  155. }
  156. public static function getServiceCharge(): float
  157. {
  158. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  159. return floatval($serviceCharge);
  160. }
  161. public static function notify($data)
  162. {
  163. try {
  164. $telegram = new Api(config('services.telegram.token'));
  165. $telegram->sendMessage($data);
  166. } catch (TelegramSDKException $e) {
  167. return false;
  168. }
  169. return true;
  170. }
  171. //提现账单
  172. public static function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  173. {
  174. $list = Withdraw::where('member_id', $chatId)
  175. ->orderBy('id', 'desc')
  176. ->forPage($page, $limit)
  177. ->get();
  178. $count = Withdraw::where('member_id', $chatId)
  179. ->whereIn('status', [0, 1])
  180. ->count();
  181. $status = [lang('等待放行'), lang('已到账'), lang("失败")];
  182. $text = "👤 {$firstName}({$chatId}) " . lang("钱包提现记录") . "\n\n";
  183. foreach ($list as $item) {
  184. $amount = floatval($item->amount);
  185. $balance = floatval($item->after_balance);
  186. $text .= "-------------------------------------\n";
  187. $text .= "➖ {$amount} USDT\n" . lang("余额") . ":{$balance} \n";
  188. $text .= lang("类别") . "类别:" . lang("提现") . "\n";
  189. $text .= lang("状态") . ":{$status[$item->status]}\n";
  190. if ($item->remark) {
  191. $text .= lang("说明") . ":{$item->remark}\n";
  192. }
  193. $text .= lang("日期") . ":{$item->created_at}\n";
  194. }
  195. if ($page > 1) {
  196. $keyboard[] = [
  197. ['text' => lang("👆上一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page - 1)]
  198. ];
  199. }
  200. $allPage = ceil($count / $limit);
  201. if ($allPage > $page) {
  202. if ($page > 1) {
  203. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)];
  204. } else {
  205. $keyboard[] = [
  206. ['text' => lang("👇下一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)]
  207. ];
  208. }
  209. }
  210. $keyboard[] = [
  211. ['text' => lang("↩️返回"), 'callback_data' => "withdraw@@home"]
  212. ];
  213. return [
  214. 'chat_id' => $chatId,
  215. 'text' => $text,
  216. 'message_id' => $messageId,
  217. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  218. ];
  219. }
  220. //删除地址
  221. public static function delAddress($chatId, $id, $messageId)
  222. {
  223. Address::where('id', $id)
  224. ->where('member_id', $chatId)->delete();
  225. return WithdrawService::getAddress($chatId, $messageId);
  226. }
  227. //地址详情
  228. public static function addressDetails($chatId, $messageId, $id)
  229. {
  230. $text = lang("*TRC20 地址管理*") . "\n\n";
  231. $address = Address::where('id', $id)
  232. ->where('member_id', $chatId)->first();
  233. $text .= lang("地址") . ":{$address->address}\n";
  234. $keyboard = [
  235. [['text' => lang('❌删除该地址'), 'callback_data' => "withdrawAddress@@del{$id}"]],
  236. [['text' => lang('↩️返回列表'), 'callback_data' => 'withdraw@@address']]
  237. ];
  238. return [
  239. 'chat_id' => $chatId,
  240. 'parse_mode' => 'MarkdownV2',
  241. 'text' => $text,
  242. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  243. 'message_id' => $messageId
  244. ];
  245. }
  246. //用户输入TRC20地址
  247. public static function inputAddress($chatId, $address, $messageId)
  248. {
  249. if (!preg_match('/^T[a-zA-Z1-9]{33}$/', $address)) {
  250. return [
  251. 'chat_id' => $chatId,
  252. 'text' => lang("地址输入不正确,请重新输入TRC20地址"),
  253. 'reply_to_message_id' => $messageId,
  254. ];
  255. }
  256. $user = User::where('member_id', $chatId)->first();
  257. if ($user?->real_name === null || $user->real_name === '') {
  258. Cache::put("{$chatId}_address", $address);
  259. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_REAL_NAME);
  260. return ['chat_id' => $chatId, 'text' => lang('请输入真实姓名'), 'message_id' => $messageId];
  261. }
  262. return self::saveAddress($chatId, $address, $messageId);
  263. }
  264. private static function saveAddress($chatId, string $address, $messageId, ?string $name = null): array
  265. {
  266. try {
  267. DB::transaction(function () use ($chatId, $address, $name) {
  268. $user = User::where('member_id', $chatId)->lockForUpdate()->firstOrFail();
  269. WithdrawalName::bind($user, $name);
  270. if (!Address::create(['member_id' => $chatId, 'address' => $address])->exists) {
  271. throw new Exception(lang('系统繁忙,请稍后重试!'), HttpStatus::CUSTOM_ERROR);
  272. }
  273. });
  274. } catch (Exception $e) {
  275. if ($e->getCode() !== HttpStatus::CUSTOM_ERROR) LogService::error($e);
  276. $text = $e->getCode() === HttpStatus::CUSTOM_ERROR ? $e->getMessage() : lang('系统繁忙,请稍后重试!');
  277. return ['chat_id' => $chatId, 'text' => $text, 'message_id' => $messageId];
  278. }
  279. Cache::delete("{$chatId}_address");
  280. Cache::delete(get_step_key($chatId));
  281. return self::getAddress($chatId, $messageId);
  282. }
  283. //添加地址
  284. public static function addAddress($chatId, $messageId)
  285. {
  286. $text = lang('请输入新的TRC20地址') . "\n";
  287. $keyboard = [[
  288. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  289. ]];
  290. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_TRC20);
  291. return [
  292. 'chat_id' => $chatId,
  293. 'text' => $text,
  294. 'message_id' => $messageId,
  295. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  296. ];
  297. }
  298. //地址管理
  299. public static function getAddress($chatId, $messageId)
  300. {
  301. $text = lang("🏠 地址管理");
  302. $text .= "\n--------------------------\n";
  303. $list = Address::where('member_id', $chatId)
  304. ->get();
  305. $keyboard = [];
  306. foreach ($list as $item) {
  307. $keyboard[] = [['text' => $item->address, 'callback_data' => "withdrawAddress@@detail{$item->id}"]];
  308. }
  309. if (count($list) < 5) {
  310. $keyboard[] = [['text' => lang("➕ 添加地址"), 'callback_data' => "withdrawAddress@@add"]];
  311. }
  312. $keyboard[] = [['text' => lang("↩️返回"), 'callback_data' => "withdraw@@home"]];
  313. return [
  314. 'chat_id' => $chatId,
  315. 'text' => $text,
  316. 'message_id' => $messageId,
  317. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  318. ];
  319. }
  320. public static function done($chatId, $messageId, $firstName)
  321. {
  322. try {
  323. WithdrawalName::required(User::where('member_id', $chatId)->first());
  324. } catch (Exception $e) {
  325. return ['chat_id' => $chatId, 'text' => $e->getMessage(), 'message_id' => $messageId];
  326. }
  327. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  328. $wagerLeft = WageringService::remainingFor((string)$chatId);
  329. if ($wagerLeft !== null) {
  330. $text = lang("打码量未完成,剩余打码量") . ":{$wagerLeft}\n";
  331. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  332. return [
  333. 'chat_id' => $chatId,
  334. 'text' => $text,
  335. 'message_id' => $messageId,
  336. ];
  337. }
  338. $serviceCharge = (new WithdrawService())->serviceCharge;
  339. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  340. $address = Cache::get("{$chatId}_WITHDRAW_ADDRESS", '');
  341. if (!$amount || !$address) return WithdrawService::index($chatId, $firstName, $messageId);
  342. $real = bcsub($amount, $serviceCharge, 10);
  343. $real = floatval($real);
  344. if ($amount <= $serviceCharge) {
  345. return [
  346. 'chat_id' => $chatId,
  347. 'text' => lang("⚠️提现不能少于") . "{$serviceCharge} USDT," . lang("请重试"),
  348. 'message_id' => $messageId
  349. ];
  350. }
  351. $wallet = Wallet::where('member_id', $chatId)->first();
  352. $temp = floatval($wallet->available_balance);
  353. // 汇率
  354. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  355. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  356. $rate = bcadd($rate, $exchange_rate_difference, 2);
  357. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  358. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  359. if ($amount > $rate_usdt_amount) {
  360. return [
  361. 'chat_id' => $chatId,
  362. 'text' => lang("⚠️可用余额不足,请重试"),
  363. 'message_id' => $messageId
  364. ];
  365. }
  366. $ru = RoomUser::where('member_id', $chatId)
  367. ->whereIn('status', [0, 1, 2, 3])
  368. ->first();
  369. if ($ru) {
  370. $text = lang("游戏未结算") . "\n";
  371. $text .= "⚠️ " . lang("您还有进行中的游戏,所有游戏结算后方可提现") . "\n";
  372. return [
  373. 'chat_id' => $chatId,
  374. 'text' => $text,
  375. 'message_id' => $messageId,
  376. 'reply_markup' => json_encode(['inline_keyboard' => [[
  377. ['text' => lang('进入房间'), 'callback_data' => "games@@home{$ru->room_id}"],
  378. ]]])
  379. ];
  380. }
  381. // 校验打码量 → 可用余额转入冻结余额 → 创建提现单 → 重置打码周期
  382. try {
  383. [, $wallet] = WithdrawFundsService::hold((string)$chatId, $rate_rmb_amount, '提现', '', WithdrawFundsService::WITHDRAWS,
  384. fn ($before, $after) => Withdraw::create([
  385. 'member_id' => $chatId,
  386. 'amount' => $amount,
  387. 'service_charge' => $serviceCharge,
  388. 'to_account' => $real,
  389. 'address' => $address,
  390. 'exchange_rate' => $rate,
  391. 'status' => 0,
  392. 'after_balance' => $after,
  393. ]));
  394. } catch (\Exception $e) {
  395. if ($e->getCode() !== HttpStatus::CUSTOM_ERROR) LogService::error($e);
  396. return [
  397. 'chat_id' => $chatId,
  398. 'text' => '⚠️' . ($e->getCode() === HttpStatus::CUSTOM_ERROR ? $e->getMessage() : lang('系统繁忙,请稍后重试!')),
  399. 'message_id' => $messageId,
  400. ];
  401. }
  402. $afterBalance = $wallet->available_balance;
  403. $temp = floatval($afterBalance);
  404. $text = "✅ " . lang("提现申请已提交!") . "\n\n";
  405. $text .= lang("钱包余额") . ":{$temp} RMB\n";
  406. $text .= lang("汇率") . ":1 USDT = {$rate} RMB\n";
  407. $text .= lang("剩余可用USDT余额") . ":" . number_format(bcdiv($temp, $rate, 2), 2) . " USDT\n";
  408. $text .= lang("提现金额") . ":{$amount} USDT\n";
  409. $text .= lang("实际到账") . ":{$real} USDT\n";
  410. $text .= lang("手续费") . ":{$serviceCharge} USDT\n\n";
  411. $text .= lang("⌛️请等待系统处理, 到账时间可能需要几分钟!") . "\n";
  412. Cache::delete(get_step_key($chatId));
  413. return [
  414. 'chat_id' => $chatId,
  415. 'text' => $text,
  416. 'message_id' => $messageId,
  417. ];
  418. }
  419. public static function chooseAddress($chatId, $firstName, $messageId, $id)
  420. {
  421. $serviceCharge = (new WithdrawService())->serviceCharge;
  422. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  423. if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
  424. $amount = floatval($amount);
  425. $real = bcsub($amount, $serviceCharge, 10);
  426. $real = floatval($real);
  427. $address = Address::where('id', $id)
  428. ->where('member_id', $chatId)->first();
  429. $text = lang("请确认") . "\n\n";
  430. $text .= lang('手续费') . ":{$serviceCharge} USDT\n";
  431. $text .= lang("提现金额") . ":{$amount} USDT\n";
  432. $text .= lang("实际到账") . ":{$real} USDT\n";
  433. $text .= lang("提现地址") . ":{$address->address}\n";
  434. $keyboard = [
  435. [
  436. ['text' => lang('✅ 确认'), 'callback_data' => "withdrawAddress@@done"],
  437. ],
  438. [
  439. ['text' => lang('❌取消'), 'callback_data' => "message@@close"]
  440. ]
  441. ];
  442. Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
  443. return [
  444. 'chat_id' => $chatId,
  445. 'text' => $text,
  446. 'message_id' => $messageId,
  447. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  448. ];
  449. }
  450. //用户输入提现金额(USDT)
  451. public static function inputAmount($chatId, $amount, $messageId)
  452. {
  453. if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
  454. return [[
  455. 'chat_id' => $chatId,
  456. 'text' => lang("金额输入不正确,请发送提现数字"),
  457. 'reply_to_message_id' => $messageId
  458. ]];
  459. }
  460. if ($amount < 20) {
  461. return [[
  462. 'chat_id' => $chatId,
  463. 'text' => lang("提现不能少于20 USDT,请重试"),
  464. 'reply_to_message_id' => $messageId
  465. ]];
  466. }
  467. $wallet = Wallet::where('member_id', $chatId)->first();
  468. $temp = floatval($wallet->available_balance);
  469. // 汇率
  470. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  471. $rate_amount = bcdiv($temp, $rate, 2);
  472. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  473. $serviceCharge = floatval($serviceCharge);
  474. if ($amount <= $serviceCharge) {
  475. return [[
  476. 'chat_id' => $chatId,
  477. 'text' => lang("⚠️提现不能少于") . "{$serviceCharge} USDT," . lang('请重试'),
  478. 'reply_to_message_id' => $messageId
  479. ]];
  480. }
  481. if ($amount > $rate_amount) {
  482. return [[
  483. 'chat_id' => $chatId,
  484. 'text' => lang("可用余额不足,请重试"),
  485. 'reply_to_message_id' => $messageId
  486. ]];
  487. }
  488. $ru = RoomUser::where('member_id', $chatId)
  489. ->whereIn('status', [0, 1, 2, 3])
  490. ->first();
  491. if ($ru) {
  492. $text = lang('*游戏未结算*') . "\n";
  493. $text .= lang("⚠️ 您还有进行中的游戏,所有游戏结算后方可提现") . "\n";
  494. return [[
  495. 'chat_id' => $chatId,
  496. 'text' => $text,
  497. 'parse_mode' => 'MarkdownV2',
  498. 'reply_markup' => json_encode(['inline_keyboard' => [[
  499. ['text' => lang('进入房间'), 'callback_data' => "games@@home{$ru->room_id}"],
  500. ]]])
  501. ]];
  502. }
  503. $list = Address::where('member_id', $chatId)->get();
  504. $keyboard = [];
  505. foreach ($list as $item) {
  506. $keyboard[] = [['text' => $item->address, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
  507. }
  508. $keyboard[] = [
  509. ['text' => lang("🏠 地址管理"), 'callback_data' => "withdraw@@address"],
  510. ['text' => lang('❌取消'), 'callback_data' => "message@@close"]
  511. ];
  512. $text = lang('请直接选择下面的地址') . "\n";
  513. $text .= lang("⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!");
  514. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  515. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
  516. return [[
  517. 'chat_id' => $chatId,
  518. 'text' => $text,
  519. 'reply_to_message_id' => $messageId,
  520. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  521. ]];
  522. }
  523. //申请提现
  524. public static function apply($chatId, $messageId)
  525. {
  526. $wallet = Wallet::where('member_id', $chatId)->first();
  527. // 汇率
  528. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  529. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  530. $rate = bcadd($rate, $exchange_rate_difference, 2);
  531. $temp = floatval($wallet->available_balance);
  532. $amount = bcdiv($temp, $rate, 2);
  533. $serviceCharge = static::getServiceCharge();
  534. $text = lang("请发送提现金额");
  535. $text .= " (USDT)";
  536. $text .= "\n💰 " . lang('当前余额') . ":{$temp} RMB ({$amount} USDT)\n";
  537. $text .= "⚠️" . lang('汇率') . ":1 USDT = {$rate} RMB\n";
  538. $text .= "⚠️ " . lang('提现将收取') . "{$serviceCharge}" . lang('U作为手续费') . "\n";
  539. // $keyboard = [[
  540. // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
  541. // ]];
  542. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
  543. return [
  544. 'chat_id' => $chatId,
  545. 'text' => $text,
  546. 'message_id' => $messageId,
  547. // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  548. ];
  549. }
  550. //提现管理
  551. public static function index($chatId, $firstName, $messageId = null)
  552. {
  553. $wallet = Wallet::where('member_id', $chatId)->first();
  554. $text = "👤 {$firstName}({$chatId})\n\n";
  555. $text .= "💰钱包余额\n";
  556. $temp = floatval($wallet->available_balance);
  557. $text .= "USDT:{$temp}\n";
  558. $text .= "--------------------------\n";
  559. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  560. $keyboard = [
  561. [
  562. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  563. ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
  564. ],
  565. [
  566. ['text' => lang("🏠 地址管理"), 'callback_data' => "withdraw@@address"],
  567. ['text' => lang('👩 客服帮助'), 'url' => "https://t.me/{$serviceAccount}"]
  568. ],
  569. [
  570. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  571. ]
  572. ];
  573. return [
  574. 'chat_id' => $chatId,
  575. 'text' => $text,
  576. 'message_id' => $messageId,
  577. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  578. ];
  579. }
  580. public static function setStatus($id, $status, $remark)
  581. {
  582. $w = DB::transaction(function () use ($id, $status, $remark) {
  583. $w = Withdraw::where('id', $id)->where('status', 0)->lockForUpdate()->first();
  584. if (!$w) return null;
  585. // 汇率
  586. $rate = $w->exchange_rate ?? 1;
  587. if ($status == 1) {
  588. $w->status = 1;
  589. $w->save();
  590. // 提现成功:扣除冻结余额
  591. WithdrawFundsService::settle(WithdrawFundsService::WITHDRAWS, $w);
  592. } else if ($status == 2) {
  593. $rate_rmb_amount = bcmul($w->amount, $rate, 2); // 提现金额 折合RMB(旧单退回金额)
  594. $w->status = 2;
  595. $w->remark = $remark;
  596. $w->save();
  597. // 审核拒绝:冻结余额退回可用余额
  598. WithdrawFundsService::release(WithdrawFundsService::WITHDRAWS, $w, $rate_rmb_amount, '提现', '');
  599. }
  600. return $w;
  601. }, 3);
  602. if (!$w) return 0;
  603. $arr = ['⏳️申请中', '✅️成功', '❌️失败'];
  604. $text = "📢 提现结果通知\n\n";
  605. $temp = floatval($w->service_charge);
  606. $text .= "手续费:{$temp} USDT\n";
  607. $temp = floatval($w->amount);
  608. $text .= "提现金额:{$temp} USDT\n";
  609. $temp = floatval($w->to_account);
  610. $text .= "到账金额:{$temp} USDT\n";
  611. $text .= "提现地址:{$w->address}\n\n";
  612. $text .= "状态:{$arr[$w->status]}\n";
  613. if ($w->remark) $text .= "说明:{$w->remark}";
  614. WithdrawService::notify([
  615. 'chat_id' => $w->member_id,
  616. 'text' => $text,
  617. ]);
  618. return 1;
  619. }
  620. public static function batchReject()
  621. {
  622. $list = Withdraw::where('status', 0)->get();
  623. foreach ($list as $k => $v) {
  624. WithdrawService::setStatus($v->id, 2, '赠送彩金,提现需满足活动需求,提现只允许走rmb渠道');
  625. }
  626. }
  627. }