WithdrawService.php 26 KB

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