SecretService.php 6.6 KB

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