WithdrawService.php 25 KB

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