WithdrawService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Address;
  5. use App\Models\BalanceLog;
  6. use App\Models\Config;
  7. use App\Models\RoomUser;
  8. use App\Models\User;
  9. use App\Models\Wallet;
  10. use App\Models\Withdraw;
  11. use Illuminate\Support\Facades\Cache;
  12. use LaravelLang\Publisher\Console\Add;
  13. use Telegram\Bot\Api;
  14. use Telegram\Bot\Exceptions\TelegramSDKException;
  15. class WithdrawService
  16. {
  17. private $serviceCharge;
  18. public static function model(): string
  19. {
  20. return Withdraw::class;
  21. }
  22. public static function enum(): string
  23. {
  24. return '';
  25. }
  26. public static function getWhere(array $search = []): array
  27. {
  28. $where = [];
  29. if (isset($search['id']) && !empty($search['id'])) {
  30. $where[] = ['id', '=', $search['id']];
  31. }
  32. if (isset($search['member_id']) && !empty($search['member_id'])) {
  33. $where[] = ['member_id', '=', $search['member_id']];
  34. }
  35. if (isset($search['status']) && $search['status'] != '') {
  36. $where[] = ['status', '=', $search['status']];
  37. }
  38. return $where;
  39. }
  40. public static function findOne(array $search): ?Withdraw
  41. {
  42. return self::model()::where(self::getWhere($search))->first();
  43. }
  44. public static function findAll(array $search = [])
  45. {
  46. return self::model()::where(self::getWhere($search))->get();
  47. }
  48. public static function paginate(array $search = [])
  49. {
  50. $limit = isset($search['limit']) ? $search['limit'] : 10;
  51. $paginator = self::model()::where(self::getWhere($search))
  52. ->orderBy('created_at', 'desc')->paginate($limit);
  53. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  54. }
  55. public function __construct()
  56. {
  57. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  58. $serviceCharge = floatval($serviceCharge);
  59. $this->serviceCharge = $serviceCharge;
  60. }
  61. public static function notify($data)
  62. {
  63. try {
  64. $telegram = new Api(config('services.telegram.token'));
  65. $telegram->sendMessage($data);
  66. } catch (TelegramSDKException $e) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. //提现账单
  72. public function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  73. {
  74. $list = Withdraw::where('member_id', $chatId)
  75. ->orderBy('id', 'desc')
  76. ->forPage($page, $limit)
  77. ->get();
  78. $count = Withdraw::where('member_id', $chatId)
  79. ->whereIn('status', [0, 1])
  80. ->count();
  81. $status = ['等待放行', '已到账', "失败"];
  82. $text = "👤 {$firstName}({$chatId}) 钱包提现记录\n\n";
  83. foreach ($list as $item) {
  84. $amount = floatval($item->amount);
  85. $balance = floatval($item->after_balance);
  86. $text .= "-------------------------------------\n";
  87. $text .= "➖ {$amount} USDT\n余额:{$balance} \n";
  88. $text .= "类别:提现\n";
  89. $text .= "状态:{$status[$item->status]}\n";
  90. if ($item->remark) {
  91. $text .= "说明:{$item->remark}\n";
  92. }
  93. $text .= "日期:{$item->created_at}\n";
  94. }
  95. if ($page > 1) {
  96. $keyboard[] = [
  97. ['text' => "👆上一页", 'callback_data' => "withdrawBillNextPage@@" . ($page - 1)]
  98. ];
  99. }
  100. $allPage = ceil($count / $limit);
  101. if ($allPage > $page) {
  102. if ($page > 1) {
  103. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)];
  104. } else {
  105. $keyboard[] = [
  106. ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)]
  107. ];
  108. }
  109. }
  110. $keyboard[] = [
  111. ['text' => "🔙返回", 'callback_data' => "withdraw@@home"]
  112. ];
  113. return [
  114. 'chat_id' => $chatId,
  115. 'text' => $text,
  116. 'message_id' => $messageId,
  117. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  118. ];
  119. }
  120. //删除地址
  121. public static function delAddress($chatId, $id, $messageId)
  122. {
  123. Address::where('id', $id)
  124. ->where('member_id', $chatId)->delete();
  125. return WithdrawService::getAddress($chatId, $messageId);
  126. }
  127. //地址详情
  128. public static function addressDetails($chatId, $messageId, $id)
  129. {
  130. $text = "*TRC20 地址管理*\n\n";
  131. $address = Address::where('id', $id)
  132. ->where('member_id', $chatId)->first();
  133. $text .= "地址:{$address->address}\n";
  134. $text .= "别名:{$address->alias}";
  135. $keyboard = [
  136. [['text' => '❌删除该地址', 'callback_data' => "withdrawAddress@@del{$id}"]],
  137. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@address']]
  138. ];
  139. return [
  140. 'chat_id' => $chatId,
  141. 'parse_mode' => 'MarkdownV2',
  142. 'text' => $text,
  143. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  144. 'message_id' => $messageId
  145. ];
  146. }
  147. //输入别名
  148. public static function inputAlias($chatId, $alias, $messageId)
  149. {
  150. if (mb_strlen($alias) > 20) {
  151. return [
  152. 'chat_id' => $chatId,
  153. 'text' => "别名不能超过20个字,请重新输入",
  154. 'reply_to_message_id' => $messageId,
  155. ];
  156. }
  157. $address = Cache::get("{$chatId}_address");
  158. $a = new Address();
  159. $a->address = $address;
  160. $a->alias = $alias;
  161. $a->member_id = $chatId;
  162. $a->save();
  163. Cache::delete("{$chatId}_address");
  164. Cache::delete(get_step_key($chatId));
  165. return self::getAddress($chatId, $messageId);
  166. }
  167. //用户输入TRC20地址
  168. public static function inputAddress($chatId, $address, $messageId)
  169. {
  170. if (!preg_match('/^T[a-zA-Z1-9]{33}$/', $address)) {
  171. return [
  172. 'chat_id' => $chatId,
  173. 'text' => "地址输入不正确,请重新输入TRC20地址",
  174. 'reply_to_message_id' => $messageId,
  175. ];
  176. }
  177. Cache::put("{$chatId}_address", $address);
  178. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_ALIAS);
  179. return [
  180. 'chat_id' => $chatId,
  181. 'text' => "请输入地址别名,便于区分多个地址",
  182. ];
  183. }
  184. //添加地址
  185. public static function addAddress($chatId, $messageId)
  186. {
  187. $text = "请输入新的TRC20地址\n";
  188. $keyboard = [[
  189. ['text' => '❌取消', 'callback_data' => "message@@close"],
  190. ]];
  191. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_TRC20);
  192. return [
  193. 'chat_id' => $chatId,
  194. 'text' => $text,
  195. 'message_id' => $messageId,
  196. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  197. ];
  198. }
  199. //地址管理
  200. public static function getAddress($chatId, $messageId)
  201. {
  202. $text = "🏠 地址管理\n";
  203. $text .= "--------------------------\n";
  204. $list = Address::where('member_id', $chatId)
  205. ->get();
  206. $keyboard = [];
  207. foreach ($list as $item) {
  208. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@detail{$item->id}"]];
  209. }
  210. if (count($list) < 5) {
  211. $keyboard[] = [['text' => "➕ 添加地址", 'callback_data' => "withdrawAddress@@add"]];
  212. }
  213. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "withdraw@@home"]];
  214. return [
  215. 'chat_id' => $chatId,
  216. 'text' => $text,
  217. 'message_id' => $messageId,
  218. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  219. ];
  220. }
  221. //设置提现地址
  222. public function setAddress($chatId, $address)
  223. {
  224. $address = trim($address);
  225. if (!$address) return [['chat_id' => $chatId, 'text' => "提现地址不能为空"]];
  226. $user = User::where('member_id', $chatId)->first();
  227. $user->usdt = $address;
  228. $user->save();
  229. $wallet = Wallet::where('member_id', $chatId)->first();
  230. $temp = floatval($wallet->available_balance);
  231. $text = "✅ 提现地址已设置\n\n";
  232. $text .= "请发送提现金额\n";
  233. $text .= "💰 当前可用USDT余额:{$temp}\n";
  234. $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
  235. $text .= "------------------------------------\n";
  236. $text .= "例如您要提现100 USDT\n";
  237. $text .= "那么请发送:【提现】100\n";
  238. return [['chat_id' => $chatId, 'text' => $text]];
  239. }
  240. public static function done($chatId, $messageId, $firstName)
  241. {
  242. $serviceCharge = (new WithdrawService())->serviceCharge;
  243. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  244. $address = Cache::get("{$chatId}_WITHDRAW_ADDRESS", '');
  245. if (!$amount || !$address) return WithdrawService::index($chatId, $firstName, $messageId);
  246. $real = bcsub($amount, $serviceCharge, 10);
  247. $real = floatval($real);
  248. if ($amount <= $serviceCharge) {
  249. return [
  250. 'chat_id' => $chatId,
  251. 'text' => "⚠️提现不能少于{$serviceCharge} USDT,请重试",
  252. 'message_id' => $messageId
  253. ];
  254. }
  255. $wallet = Wallet::where('member_id', $chatId)->first();
  256. $temp = floatval($wallet->available_balance);
  257. // 汇率
  258. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val??1;
  259. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  260. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  261. if ($amount > $rate_usdt_amount) {
  262. return [
  263. 'chat_id' => $chatId,
  264. 'text' => "⚠️可用余额不足,请重试",
  265. 'message_id' => $messageId
  266. ];
  267. }
  268. $ru = RoomUser::where('member_id', $chatId)
  269. ->whereIn('status', [0, 1, 2, 3])
  270. ->first();
  271. if ($ru) {
  272. $text = "游戏未结算\n";
  273. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  274. return [
  275. 'chat_id' => $chatId,
  276. 'text' => $text,
  277. 'message_id' => $messageId,
  278. 'reply_markup' => json_encode(['inline_keyboard' => [[
  279. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  280. ]]])
  281. ];
  282. }
  283. $wallet = Wallet::where('member_id', $chatId)->first();
  284. $bl = new BalanceLog();
  285. $bl->member_id = $chatId;
  286. $bl->amount = bcmul(($amount * -1),$rate,10); // 扣除的金额 RMB
  287. $bl->before_balance = $wallet->available_balance;
  288. $wallet->available_balance = bcsub($wallet->available_balance, $rate_rmb_amount, 10);
  289. $wallet->save();
  290. $bl->after_balance = $wallet->available_balance;
  291. $bl->change_type = '提现';
  292. $withdraw = new Withdraw();
  293. $withdraw->member_id = $chatId;
  294. $withdraw->amount = $amount;
  295. $withdraw->service_charge = $serviceCharge;
  296. $withdraw->to_account = $real;
  297. $withdraw->address = $address;
  298. $withdraw->exchange_rate = $rate;
  299. $withdraw->status = 0;
  300. $withdraw->after_balance = bcdiv($wallet->available_balance,$rate,2);
  301. $withdraw->save();
  302. $bl->related_id = $withdraw->id;
  303. $bl->save();
  304. $temp = floatval($wallet->available_balance);
  305. $text = "✅ 提现申请已提交!\n\n";
  306. $text .= "钱包余额:{$temp} RMB\n";
  307. $text .= "汇率:1 USDT = {$rate} RMB\n";
  308. $text .= "剩余可用USDT余额:".number_format(bcdiv($temp, $rate, 2), 2)." USDT\n";
  309. $text .= "提现金额:{$amount} USDT\n";
  310. $text .= "实际到账:{$real} USDT\n";
  311. $text .= "手续费:{$serviceCharge} USDT\n\n";
  312. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  313. Cache::delete(get_step_key($chatId));
  314. return [
  315. 'chat_id' => $chatId,
  316. 'text' => $text,
  317. 'message_id' => $messageId,
  318. ];
  319. }
  320. public static function chooseAddress($chatId, $firstName, $messageId, $id)
  321. {
  322. $serviceCharge = (new WithdrawService())->serviceCharge;
  323. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  324. if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
  325. $amount = floatval($amount);
  326. $real = bcsub($amount, $serviceCharge, 10);
  327. $real = floatval($real);
  328. $address = Address::where('id', $id)
  329. ->where('member_id', $chatId)->first();
  330. $text = "请确认\n\n";
  331. $text .= "手续费:{$serviceCharge} USDT\n";
  332. $text .= "提现金额:{$amount} USDT\n";
  333. $text .= "实际到账:{$real} USDT\n";
  334. $text .= "提现地址:{$address->address}\n";
  335. $keyboard = [
  336. [
  337. ['text' => '✅ 确认', 'callback_data' => "withdrawAddress@@done"],
  338. ],
  339. [
  340. ['text' => '❌取消', 'callback_data' => "message@@close"]
  341. ]
  342. ];
  343. Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
  344. return [
  345. 'chat_id' => $chatId,
  346. 'text' => $text,
  347. 'message_id' => $messageId,
  348. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  349. ];
  350. }
  351. //用户输入提现金额
  352. public function inputAmount($chatId, $amount, $messageId)
  353. {
  354. if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
  355. return [[
  356. 'chat_id' => $chatId,
  357. 'text' => "金额输入不正确,请发送提现数字",
  358. 'reply_to_message_id' => $messageId
  359. ]];
  360. }
  361. $wallet = Wallet::where('member_id', $chatId)->first();
  362. $temp = floatval($wallet->available_balance);
  363. // 汇率
  364. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val??1;
  365. $rate_amount = bcdiv($temp, $rate, 2);
  366. if ($amount <= $this->serviceCharge) {
  367. return [[
  368. 'chat_id' => $chatId,
  369. 'text' => "⚠️提现不能少于{$this->serviceCharge} USDT,请重试",
  370. 'reply_to_message_id' => $messageId
  371. ]];
  372. }
  373. if ($amount > $rate_amount) {
  374. return [[
  375. 'chat_id' => $chatId,
  376. 'text' => "⚠️可用余额不足,请重试",
  377. 'reply_to_message_id' => $messageId
  378. ]];
  379. }
  380. $ru = RoomUser::where('member_id', $chatId)
  381. ->whereIn('status', [0, 1, 2, 3])
  382. ->first();
  383. if ($ru) {
  384. $text = "*游戏未结算*\n";
  385. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  386. return [[
  387. 'chat_id' => $chatId,
  388. 'text' => $text,
  389. 'parse_mode' => 'MarkdownV2',
  390. 'reply_markup' => json_encode(['inline_keyboard' => [[
  391. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  392. ]]])
  393. ]];
  394. }
  395. $list = Address::where('member_id', $chatId)->get();
  396. $keyboard = [];
  397. foreach ($list as $item) {
  398. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
  399. }
  400. $keyboard[] = [
  401. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  402. ['text' => '❌取消', 'callback_data' => "message@@close"]
  403. ];
  404. $text = "请直接选择下面的地址\n";
  405. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  406. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  407. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
  408. return [[
  409. 'chat_id' => $chatId,
  410. 'text' => $text,
  411. 'reply_to_message_id' => $messageId,
  412. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  413. ]];
  414. }
  415. //申请提现
  416. public function apply($chatId, $messageId)
  417. {
  418. $wallet = Wallet::where('member_id', $chatId)->first();
  419. // 汇率
  420. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val??1;
  421. $temp = floatval($wallet->available_balance);
  422. $amount = bcdiv($temp, $rate, 2);
  423. $text = "请发送提现金额\n";
  424. $text .= "💰 当前余额{$temp} RMB\n";
  425. $text .= "⚠️汇率:1 USDT = {$rate} RMB\n";
  426. $text .= "💰 当前可用USDT余额:{$amount}\n";
  427. $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
  428. // $keyboard = [[
  429. // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
  430. // ]];
  431. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
  432. return [
  433. 'chat_id' => $chatId,
  434. 'text' => $text,
  435. 'message_id' => $messageId,
  436. // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  437. ];
  438. }
  439. //提现管理
  440. public static function index($chatId, $firstName, $messageId = null)
  441. {
  442. $wallet = Wallet::where('member_id', $chatId)->first();
  443. $text = "👤 {$firstName}({$chatId})\n\n";
  444. $text .= "💰钱包余额\n";
  445. $temp = floatval($wallet->available_balance);
  446. $text .= "USDT:{$temp}\n";
  447. $text .= "--------------------------\n";
  448. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  449. $keyboard = [
  450. [
  451. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  452. ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
  453. ],
  454. [
  455. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  456. ['text' => '👩 客服帮助', 'url' => "https://t.me/{$serviceAccount}"]
  457. ],
  458. [
  459. ['text' => '❌取消', 'callback_data' => "message@@close"],
  460. ]
  461. ];
  462. return [
  463. 'chat_id' => $chatId,
  464. 'text' => $text,
  465. 'message_id' => $messageId,
  466. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  467. ];
  468. }
  469. }