SecretService.php 7.9 KB

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