SecretService.php 6.9 KB

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