SecretService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\User;
  5. use App\Models\Wallet;
  6. use Illuminate\Support\Facades\Cache;
  7. use Telegram\Bot\Api;
  8. use Telegram\Bot\Exceptions\TelegramSDKException;
  9. class SecretService
  10. {
  11. /**
  12. * @param Api $telegram
  13. * @param $data
  14. * @param $chatId
  15. * @param $firstName
  16. * @param $messageId
  17. * @throws TelegramSDKException
  18. */
  19. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  20. {
  21. if ($data === "secret@@index") {
  22. $res = SecretService::index($chatId, $messageId);
  23. $telegram->editMessageText($res);
  24. }
  25. //找回账号
  26. if ($data === "secret@@retrieve") {
  27. $res = SecretService::retrieve($chatId, $messageId);
  28. $telegram->editMessageText($res);
  29. }
  30. }
  31. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  32. {
  33. return match ($stepStatus) {
  34. StepStatus::MY_INPUT_OLD_SECRET => SecretService::inputOldSecret($chatId, $text, $messageId),
  35. default => null
  36. };
  37. }
  38. private static function inputOldSecret($chatId, $secret, $messageId): array
  39. {
  40. $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  41. if (!$user) {
  42. return [
  43. 'chat_id' => $chatId,
  44. 'text' => "输入错误,请重新输入",
  45. 'reply_to_message_id' => $messageId
  46. ];
  47. }
  48. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  49. $keyboard = [
  50. [
  51. ['text' => '确认', 'callback_data' => 'secret@@confirm'],
  52. ]
  53. ];
  54. $text = "原账号信息:\n";
  55. $text .= "用户ID:{$user->getMemberId()}\n";
  56. $text .= "用户名:{$user->getUsername()}\n}";
  57. $text .= "昵称:{$user->getFirstName()}\n";
  58. $text .= "余额:{$wallet->available_balance}\n";
  59. $text .= "\n-------------------------------\n\n";
  60. $text .= "注意:请确认原账号信息,点击确认后,原账号将注销,原账号的余额以及其他信息将同步到新账号中!此操作不可撤销";
  61. return [
  62. 'chat_id' => $chatId,
  63. 'text' => $text,
  64. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  65. ];
  66. }
  67. private static function retrieve($chatId, $messageId): array
  68. {
  69. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_OLD_SECRET);
  70. return [
  71. 'chat_id' => $chatId,
  72. 'text' => '请输入原账号的秘钥',
  73. 'message_id' => $messageId,
  74. ];
  75. }
  76. private static function index($chatId, $messageId): array
  77. {
  78. $keyboard = [
  79. [
  80. ['text' => '查看秘钥', 'callback_data' => 'secret@@view'],
  81. ['text' => '找回账号', 'callback_data' => 'secret@@retrieve'],
  82. ],
  83. [
  84. ['text' => '返回', 'callback_data' => 'topUp@@home'],
  85. ]
  86. ];
  87. $text = "秘钥管理\n";
  88. $text .= "请选择业务类型";
  89. return [
  90. 'chat_id' => $chatId,
  91. 'text' => $text,
  92. 'message_id' => $messageId,
  93. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  94. ];
  95. }
  96. }