WithdrawService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. if ($amount > $temp) {
  258. return [
  259. 'chat_id' => $chatId,
  260. 'text' => "⚠️可用余额不足,请重试",
  261. 'message_id' => $messageId
  262. ];
  263. }
  264. $ru = RoomUser::where('member_id', $chatId)
  265. ->whereIn('status', [0, 1, 2, 3])
  266. ->first();
  267. if ($ru) {
  268. $text = "游戏未结算\n";
  269. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  270. return [
  271. 'chat_id' => $chatId,
  272. 'text' => $text,
  273. 'message_id' => $messageId,
  274. 'reply_markup' => json_encode(['inline_keyboard' => [[
  275. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  276. ]]])
  277. ];
  278. }
  279. $wallet = Wallet::where('member_id', $chatId)->first();
  280. $bl = new BalanceLog();
  281. $bl->member_id = $chatId;
  282. $bl->amount = $amount * -1;
  283. $bl->before_balance = $wallet->available_balance;
  284. $wallet->available_balance = bcsub($wallet->available_balance, $amount, 10);
  285. $wallet->save();
  286. $bl->after_balance = $wallet->available_balance;
  287. $bl->change_type = '提现';
  288. $withdraw = new Withdraw();
  289. $withdraw->member_id = $chatId;
  290. $withdraw->amount = $amount;
  291. $withdraw->service_charge = $serviceCharge;
  292. $withdraw->to_account = $real;
  293. $withdraw->address = $address;
  294. $withdraw->status = 0;
  295. $withdraw->after_balance = $wallet->available_balance;
  296. $withdraw->save();
  297. $bl->related_id = $withdraw->id;
  298. $bl->save();
  299. $temp = floatval($wallet->available_balance);
  300. $text = "✅ 提现申请已提交!\n\n";
  301. $text .= "钱包余额:{$temp} USDT\n";
  302. $text .= "提现金额:{$amount} USDT\n";
  303. $text .= "实际到账:{$real} USDT\n";
  304. $text .= "手续费:{$serviceCharge} USDT\n\n";
  305. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  306. Cache::delete(get_step_key($chatId));
  307. return [
  308. 'chat_id' => $chatId,
  309. 'text' => $text,
  310. 'message_id' => $messageId,
  311. ];
  312. }
  313. public static function chooseAddress($chatId, $firstName, $messageId, $id)
  314. {
  315. $serviceCharge = (new WithdrawService())->serviceCharge;
  316. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  317. if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
  318. $amount = floatval($amount);
  319. $real = bcsub($amount, $serviceCharge, 10);
  320. $real = floatval($real);
  321. $address = Address::where('id', $id)
  322. ->where('member_id', $chatId)->first();
  323. $text = "请确认\n\n";
  324. $text .= "手续费:{$serviceCharge} USDT\n";
  325. $text .= "提现金额:{$amount} USDT\n";
  326. $text .= "实际到账:{$real} USDT\n";
  327. $text .= "提现地址:{$address->address}\n";
  328. $keyboard = [
  329. [
  330. ['text' => '✅ 确认', 'callback_data' => "withdrawAddress@@done"],
  331. ],
  332. [
  333. ['text' => '❌取消', 'callback_data' => "message@@close"]
  334. ]
  335. ];
  336. Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
  337. return [
  338. 'chat_id' => $chatId,
  339. 'text' => $text,
  340. 'message_id' => $messageId,
  341. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  342. ];
  343. }
  344. //用户输入提现金额
  345. public function inputAmount($chatId, $amount, $messageId)
  346. {
  347. if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
  348. return [[
  349. 'chat_id' => $chatId,
  350. 'text' => "金额输入不正确,请发送提现数字",
  351. 'reply_to_message_id' => $messageId
  352. ]];
  353. }
  354. $wallet = Wallet::where('member_id', $chatId)->first();
  355. $temp = floatval($wallet->available_balance);
  356. if ($amount <= $this->serviceCharge) {
  357. return [[
  358. 'chat_id' => $chatId,
  359. 'text' => "⚠️提现不能少于{$this->serviceCharge} USDT,请重试",
  360. 'reply_to_message_id' => $messageId
  361. ]];
  362. }
  363. if ($amount > $temp) {
  364. return [[
  365. 'chat_id' => $chatId,
  366. 'text' => "⚠️可用余额不足,请重试",
  367. 'reply_to_message_id' => $messageId
  368. ]];
  369. }
  370. $ru = RoomUser::where('member_id', $chatId)
  371. ->whereIn('status', [0, 1, 2, 3])
  372. ->first();
  373. if ($ru) {
  374. $text = "*游戏未结算*\n";
  375. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  376. return [[
  377. 'chat_id' => $chatId,
  378. 'text' => $text,
  379. 'parse_mode' => 'MarkdownV2',
  380. 'reply_markup' => json_encode(['inline_keyboard' => [[
  381. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  382. ]]])
  383. ]];
  384. }
  385. $list = Address::where('member_id', $chatId)->get();
  386. $keyboard = [];
  387. foreach ($list as $item) {
  388. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
  389. }
  390. $keyboard[] = [
  391. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  392. ['text' => '❌取消', 'callback_data' => "message@@close"]
  393. ];
  394. $text = "请直接选择下面的地址\n";
  395. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  396. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  397. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
  398. return [[
  399. 'chat_id' => $chatId,
  400. 'text' => $text,
  401. 'reply_to_message_id' => $messageId,
  402. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  403. ]];
  404. }
  405. //申请提现
  406. public function apply($chatId, $messageId)
  407. {
  408. $wallet = Wallet::where('member_id', $chatId)->first();
  409. $temp = floatval($wallet->available_balance);
  410. $text = "请发送提现金额\n";
  411. $text .= "💰 当前可用USDT余额:{$temp}\n";
  412. $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
  413. // $keyboard = [[
  414. // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
  415. // ]];
  416. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
  417. return [
  418. 'chat_id' => $chatId,
  419. 'text' => $text,
  420. 'message_id' => $messageId,
  421. // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  422. ];
  423. }
  424. //提现管理
  425. public static function index($chatId, $firstName, $messageId = null)
  426. {
  427. $wallet = Wallet::where('member_id', $chatId)->first();
  428. $text = "👤 {$firstName}({$chatId})\n\n";
  429. $text .= "💰钱包余额\n";
  430. $temp = floatval($wallet->available_balance);
  431. $text .= "USDT:{$temp}\n";
  432. $text .= "--------------------------\n";
  433. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  434. $keyboard = [
  435. [
  436. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  437. ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
  438. ],
  439. [
  440. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  441. ['text' => '👩 客服帮助', 'url' => "https://t.me/{$serviceAccount}"]
  442. ],
  443. [
  444. ['text' => '❌取消', 'callback_data' => "message@@close"],
  445. ]
  446. ];
  447. return [
  448. 'chat_id' => $chatId,
  449. 'text' => $text,
  450. 'message_id' => $messageId,
  451. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  452. ];
  453. }
  454. }