SecretService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Address;
  5. use App\Models\BalanceLog;
  6. use App\Models\Bank;
  7. use App\Models\Bet;
  8. use App\Models\PaymentOrder;
  9. use App\Models\Rebate;
  10. use App\Models\User;
  11. use App\Models\Wallet;
  12. use Illuminate\Support\Facades\Cache;
  13. use Telegram\Bot\Api;
  14. use Telegram\Bot\Exceptions\TelegramSDKException;
  15. class SecretService
  16. {
  17. /**
  18. * @param Api $telegram
  19. * @param $data
  20. * @param $chatId
  21. * @param $firstName
  22. * @param $messageId
  23. * @throws TelegramSDKException
  24. */
  25. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  26. {
  27. //秘钥管理首页
  28. if ($data === "secret@@index") {
  29. $res = SecretService::index($chatId, $messageId);
  30. $telegram->editMessageText($res);
  31. }
  32. //查看秘钥
  33. if ($data === "secret@@view") {
  34. $res = SecretService::view($chatId, $messageId);
  35. $telegram->editMessageText($res);
  36. }
  37. //找回账号
  38. if ($data === "secret@@retrieve") {
  39. $res = SecretService::retrieve($chatId, $messageId);
  40. $telegram->editMessageText($res);
  41. }
  42. //确认找回
  43. if ($data === "secret@@confirm") {
  44. $res = SecretService::confirm($chatId, $messageId);
  45. $telegram->editMessageText($res);
  46. }
  47. }
  48. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  49. {
  50. switch ($stepStatus) {
  51. case StepStatus::MY_INPUT_OLD_SECRET://输入原账户的秘钥
  52. $res = SecretService::inputOldSecret($chatId, $text, $messageId);
  53. break;
  54. case StepStatus::MY_INPUT_SECRET_PASS://输入查看秘钥的密码
  55. $res = SecretService::showSecretKey($chatId, $text, $messageId);
  56. break;
  57. default:
  58. $res = null;
  59. break;
  60. }
  61. return $res;
  62. }
  63. private static function showSecretKey($chatId, $password, $messageId): array
  64. {
  65. $user = User::where('member_id', $chatId)->first();
  66. if ($user->secret_pass == '') {
  67. $user->secret_pass = $password;
  68. $user->save();
  69. }
  70. if ($user->secret_pass !== $password) {
  71. $text = lang("密码错误");
  72. return [
  73. 'chat_id' => $chatId,
  74. 'text' => $text,
  75. 'reply_to_message_id' => $messageId
  76. ];
  77. }
  78. $text = lang("秘钥") . ":{$user->secret_key}\n\n";
  79. $text .= lang("请保管好你的秘钥");
  80. return [
  81. 'chat_id' => $chatId,
  82. 'text' => $text,
  83. ];
  84. }
  85. private static function view($chatId, $messageId): array
  86. {
  87. $text = lang("请输入查看密码");
  88. $user = User::where('member_id', $chatId)->first();
  89. if ($user->secret_pass == '') {
  90. $secretKey = SecretService::generateRandomString(22);
  91. $user->secret_key = $secretKey;
  92. $user->save();
  93. }
  94. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_SECRET_PASS);
  95. return [
  96. 'chat_id' => $chatId,
  97. 'text' => $text,
  98. 'message_id' => $messageId,
  99. ];
  100. }
  101. /**
  102. * @param string $chatId 接收者的member_id
  103. * @param string $secretKey 被接收者的 秘钥
  104. * @return bool
  105. */
  106. public static function migration(string $chatId, string $secretKey): bool
  107. {
  108. $user = User::where('secret_key', $secretKey)->where('member_id', '!=', $chatId)->first();
  109. $newUser = User::where('member_id', $chatId)->first();
  110. if (!$newUser || !$user) return false;
  111. $oldMemberId = $user->getMemberId();
  112. // $oldUserId = $user->id;
  113. $newMemberId = $chatId;
  114. $newUserId = $newUser->id;
  115. Address::where('member_id', $oldMemberId)->update(['member_id' => $newMemberId]);
  116. Bank::where('member_id', $oldMemberId)->update(['member_id' => $newMemberId]);
  117. Bet::where('member_id', $oldMemberId)->update(['member_id' => $newMemberId, 'user_id' => $newUserId]);
  118. PaymentOrder::where('member_id', $oldMemberId)->update(['member_id' => $newMemberId]);
  119. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  120. if ($wallet->available_balance > 0) {
  121. $res = WalletService::updateBalance($user->getMemberId(), $wallet->available_balance * -1);
  122. BalanceLogService::addLog(
  123. $oldMemberId,
  124. $wallet->available_balance * -1,
  125. $res['before_balance'],
  126. $res['after_balance'],
  127. "资产转移",
  128. null,
  129. "转移至 {$newUser->getMemberId()}"
  130. );
  131. $res = WalletService::updateBalance($chatId, $wallet->available_balance);
  132. BalanceLogService::addLog(
  133. $chatId,
  134. $wallet->available_balance,
  135. $res['before_balance'],
  136. $res['after_balance'],
  137. "资产转移",
  138. null,
  139. "由 {$user->getMemberId()} 转移"
  140. );
  141. }
  142. return true;
  143. }
  144. private static function confirm($chatId, $messageId): array
  145. {
  146. $secret = Cache::get("{$chatId}_OLD_SECRET");
  147. // $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  148. // $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  149. $res = SecretService::migration($chatId, $secret);
  150. if ($res) {
  151. return [
  152. 'chat_id' => $chatId,
  153. 'text' => lang('已完成迁移'),
  154. 'message_id' => $messageId,
  155. ];
  156. } else {
  157. return [
  158. 'chat_id' => $chatId,
  159. 'text' => lang('迁移失败'),
  160. 'message_id' => $messageId,
  161. ];
  162. }
  163. }
  164. private static function inputOldSecret($chatId, $secret, $messageId): array
  165. {
  166. $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  167. if (!$user) {
  168. return [
  169. 'chat_id' => $chatId,
  170. 'text' => lang("输入错误,请重新输入"),
  171. 'reply_to_message_id' => $messageId
  172. ];
  173. }
  174. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  175. $keyboard = [
  176. [
  177. ['text' => lang('取消'), 'callback_data' => 'topUp@@home'],
  178. ['text' => lang('确认'), 'callback_data' => 'secret@@confirm'],
  179. ]
  180. ];
  181. $wallet->available_balance = bcadd($wallet->available_balance, 0, 2);
  182. $available_balance = $wallet->available_balance;
  183. $text = lang("原账号信息") . ":\n";
  184. $text .= lang("用户ID") . ":{$user->getMemberId()}\n";
  185. $text .= lang("用户名") . ":{$user->getUsername()}\n";
  186. $text .= lang("昵称") . ":{$user->getFirstName()}\n";
  187. $text .= lang("余额") . ":{$wallet->available_balance} RMB\n";
  188. $text .= "-------------------------------\n";
  189. $user = User::where('member_id', $chatId)->first();
  190. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  191. $wallet->available_balance = bcadd($wallet->available_balance, 0, 2);
  192. $text .= lang("新账号信息") . ":\n";
  193. $text .= lang('用户ID') . ":{$user->getMemberId()}\n";
  194. $text .= lang("用户名") . ":{$user->getUsername()}\n";
  195. $text .= lang('昵称') . ":{$user->getFirstName()}\n";
  196. $text .= lang('余额') . ":{$wallet->available_balance} RMB\n";
  197. $available_balance = bcadd($available_balance, $wallet->available_balance, 2);
  198. $text .= "-------------------------------\n";
  199. $text .= lang('合并后余额') . ":{$available_balance} RMB\n\n";
  200. $text .= lang("注意:请确认原账号信息,点击确认后,原账号将注销,原账号的余额以及其他信息将合并到新账号中!\n此操作不可撤销。请取消或确认。");
  201. Cache::put("{$chatId}_OLD_SECRET", $secret);
  202. return [
  203. 'chat_id' => $chatId,
  204. 'text' => $text,
  205. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  206. ];
  207. }
  208. private static function retrieve($chatId, $messageId): array
  209. {
  210. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_OLD_SECRET);
  211. return [
  212. 'chat_id' => $chatId,
  213. 'text' => lang('请输入原账号的秘钥'),
  214. 'message_id' => $messageId,
  215. ];
  216. }
  217. private static function index($chatId, $messageId): array
  218. {
  219. $keyboard = [
  220. [
  221. ['text' => lang('查看秘钥'), 'callback_data' => 'secret@@view'],
  222. ['text' => lang('找回账号'), 'callback_data' => 'secret@@retrieve'],
  223. ],
  224. [
  225. ['text' => lang('返回'), 'callback_data' => 'topUp@@home'],
  226. ]
  227. ];
  228. $text = lang("秘钥管理") . "\n";
  229. $text .= lang("请选择业务类型");
  230. return [
  231. 'chat_id' => $chatId,
  232. 'text' => $text,
  233. 'message_id' => $messageId,
  234. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  235. ];
  236. }
  237. private static function generateRandomString($length = 10)
  238. {
  239. // 定义大写字母和数字的字符集
  240. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  241. // 获取字符集的长度
  242. $charactersLength = strlen($characters);
  243. // 初始化随机字符串
  244. $randomString = '';
  245. // 生成随机字符串
  246. for ($i = 0; $i < $length; $i++) {
  247. // 从字符集随机选择一个字符
  248. $randomString .= $characters[rand(0, $charactersLength - 1)];
  249. }
  250. return $randomString;
  251. }
  252. }