SecretService.php 6.5 KB

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