SecretService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if ($data === "secret@@view") {
  26. $res = SecretService::view($chatId, $messageId);
  27. $telegram->editMessageText($res);
  28. }
  29. //找回账号
  30. if ($data === "secret@@retrieve") {
  31. $res = SecretService::retrieve($chatId, $messageId);
  32. $telegram->editMessageText($res);
  33. }
  34. //确认找回
  35. if ($data === "secret@@confirm") {
  36. $res = SecretService::confirm($chatId, $messageId);
  37. $telegram->editMessageText($res);
  38. }
  39. }
  40. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  41. {
  42. switch ($stepStatus) {
  43. case StepStatus::MY_INPUT_OLD_SECRET:
  44. return SecretService::inputOldSecret($chatId, $text, $messageId);
  45. default:
  46. return null;
  47. }
  48. }
  49. private static function view($chatId, $messageId): array
  50. {
  51. $text = "请输入查看密码";
  52. $user = User::where('member_id', $chatId)->first();
  53. if ($user->secret_pass == '') {
  54. $secretKey = SecretService::generateRandomString(22);
  55. $user->secret_key = $secretKey;
  56. $user->save();
  57. }
  58. return [
  59. 'chat_id' => $chatId,
  60. 'text' => $text,
  61. 'message_id' => $messageId,
  62. ];
  63. }
  64. private static function confirm($chatId, $messageId): array
  65. {
  66. $secret = Cache::get("{$chatId}_OLD_SECRET");
  67. $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  68. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  69. // $res = WalletService::updateBalance($user->getMemberId(), $wallet->available_balance * -1);
  70. // BalanceLogService::addLog(
  71. // $chatId,
  72. // $wallet->available_balance * -1,
  73. // $res['before_balance'],
  74. // $res['after_balance'],
  75. // "账号找回",
  76. // '',
  77. // "新账号:{$chatId}"
  78. // );
  79. //
  80. // $res = WalletService::updateBalance($chatId, $wallet->available_balance);
  81. // BalanceLogService::addLog(
  82. // $chatId,
  83. // $wallet->available_balance,
  84. // $res['before_balance'],
  85. // $res['after_balance'],
  86. // "账号找回",
  87. // '',
  88. // "原账号:{$user->getMemberId()}"
  89. // );
  90. return [];
  91. }
  92. private static function inputOldSecret($chatId, $secret, $messageId): array
  93. {
  94. $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  95. if (!$user) {
  96. return [
  97. 'chat_id' => $chatId,
  98. 'text' => "输入错误,请重新输入",
  99. 'reply_to_message_id' => $messageId
  100. ];
  101. }
  102. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  103. $keyboard = [
  104. [
  105. ['text' => '确认', 'callback_data' => 'secret@@confirm'],
  106. ]
  107. ];
  108. $text = "原账号信息:\n";
  109. $text .= "用户ID:{$user->getMemberId()}\n";
  110. $text .= "用户名:{$user->getUsername()}\n}";
  111. $text .= "昵称:{$user->getFirstName()}\n";
  112. $text .= "余额:{$wallet->available_balance}\n";
  113. $text .= "\n-------------------------------\n\n";
  114. $text .= "注意:请确认原账号信息,点击确认后,原账号将注销,原账号的余额以及其他信息将同步到新账号中!此操作不可撤销";
  115. Cache::put("{$chatId}_OLD_SECRET", $secret);
  116. return [
  117. 'chat_id' => $chatId,
  118. 'text' => $text,
  119. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  120. ];
  121. }
  122. private static function retrieve($chatId, $messageId): array
  123. {
  124. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_OLD_SECRET);
  125. return [
  126. 'chat_id' => $chatId,
  127. 'text' => '请输入原账号的秘钥',
  128. 'message_id' => $messageId,
  129. ];
  130. }
  131. private static function index($chatId, $messageId): array
  132. {
  133. $keyboard = [
  134. [
  135. ['text' => '查看秘钥', 'callback_data' => 'secret@@view'],
  136. ['text' => '找回账号', 'callback_data' => 'secret@@retrieve'],
  137. ],
  138. [
  139. ['text' => '返回', 'callback_data' => 'topUp@@home'],
  140. ]
  141. ];
  142. $text = "秘钥管理\n";
  143. $text .= "请选择业务类型";
  144. return [
  145. 'chat_id' => $chatId,
  146. 'text' => $text,
  147. 'message_id' => $messageId,
  148. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  149. ];
  150. }
  151. private static function generateRandomString($length = 10)
  152. {
  153. // 定义大写字母和数字的字符集
  154. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  155. // 获取字符集的长度
  156. $charactersLength = strlen($characters);
  157. // 初始化随机字符串
  158. $randomString = '';
  159. // 生成随机字符串
  160. for ($i = 0; $i < $length; $i++) {
  161. // 从字符集随机选择一个字符
  162. $randomString .= $characters[rand(0, $charactersLength - 1)];
  163. }
  164. return $randomString;
  165. }
  166. }