SecretService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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($oldMemberId, $wallet->available_balance * -1, $res['before_balance'], $res['after_balance'], "资产转移", null, "转移至 {$newUser->getMemberId()}");
  123. $res = WalletService::updateBalance($chatId, $wallet->available_balance);
  124. BalanceLogService::addLog($chatId, $wallet->available_balance, $res['before_balance'], $res['after_balance'], "资产转移", null, "由 {$user->getMemberId()} 转移");
  125. }
  126. return true;
  127. }
  128. private static function confirm($chatId, $messageId): array
  129. {
  130. $secret = Cache::get("{$chatId}_OLD_SECRET");
  131. // $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  132. // $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  133. $res = SecretService::migration($chatId, $secret);
  134. if ($res) {
  135. return [
  136. 'chat_id' => $chatId,
  137. 'text' => lang('已完成迁移'),
  138. 'message_id' => $messageId,
  139. ];
  140. } else {
  141. return [
  142. 'chat_id' => $chatId,
  143. 'text' => lang('迁移失败'),
  144. 'message_id' => $messageId,
  145. ];
  146. }
  147. }
  148. private static function inputOldSecret($chatId, $secret, $messageId): array
  149. {
  150. $user = User::where(['secret_key' => $secret])->where('member_id', '!=', $chatId)->first();
  151. if (!$user) {
  152. return [
  153. 'chat_id' => $chatId,
  154. 'text' => lang("输入错误,请重新输入"),
  155. 'reply_to_message_id' => $messageId
  156. ];
  157. }
  158. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  159. $keyboard = [
  160. [
  161. ['text' => lang('取消'), 'callback_data' => 'topUp@@home'],
  162. ['text' => lang('确认'), 'callback_data' => 'secret@@confirm'],
  163. ]
  164. ];
  165. $wallet->available_balance = bcadd($wallet->available_balance, 0, 2);
  166. $available_balance = $wallet->available_balance;
  167. $text = lang("原账号信息") . ":\n";
  168. $text .= lang("用户ID") . ":{$user->getMemberId()}\n";
  169. $text .= lang("用户名") . ":{$user->getUsername()}\n";
  170. $text .= lang("昵称") . ":{$user->getFirstName()}\n";
  171. $text .= lang("余额") . ":{$wallet->available_balance} RMB\n";
  172. $text .= "-------------------------------\n";
  173. $user = User::where('member_id', $chatId)->first();
  174. $wallet = Wallet::where('member_id', $user->getMemberId())->first();
  175. $wallet->available_balance = bcadd($wallet->available_balance, 0, 2);
  176. $text .= lang("新账号信息") . ":\n";
  177. $text .= lang('用户ID') . ":{$user->getMemberId()}\n";
  178. $text .= lang("用户名") . ":{$user->getUsername()}\n";
  179. $text .= lang('昵称') . ":{$user->getFirstName()}\n";
  180. $text .= lang('余额') . ":{$wallet->available_balance} RMB\n";
  181. $available_balance = bcadd($available_balance, $wallet->available_balance, 2);
  182. $text .= "-------------------------------\n";
  183. $text .= lang('合并后余额') . ":{$available_balance} RMB\n\n";
  184. $text .= lang("注意:请确认原账号信息,点击确认后,原账号将注销,原账号的余额以及其他信息将合并到新账号中!\n此操作不可撤销。请取消或确认。");
  185. Cache::put("{$chatId}_OLD_SECRET", $secret);
  186. return [
  187. 'chat_id' => $chatId,
  188. 'text' => $text,
  189. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  190. ];
  191. }
  192. private static function retrieve($chatId, $messageId): array
  193. {
  194. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_OLD_SECRET);
  195. return [
  196. 'chat_id' => $chatId,
  197. 'text' => lang('请输入原账号的秘钥'),
  198. 'message_id' => $messageId,
  199. ];
  200. }
  201. private static function index($chatId, $messageId): array
  202. {
  203. $keyboard = [
  204. [
  205. ['text' => lang('查看秘钥'), 'callback_data' => 'secret@@view'],
  206. ['text' => lang('找回账号'), 'callback_data' => 'secret@@retrieve'],
  207. ],
  208. [
  209. ['text' => lang('返回'), 'callback_data' => 'topUp@@home'],
  210. ]
  211. ];
  212. $text = lang("秘钥管理") . "\n";
  213. $text .= lang("请选择业务类型");
  214. return [
  215. 'chat_id' => $chatId,
  216. 'text' => $text,
  217. 'message_id' => $messageId,
  218. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  219. ];
  220. }
  221. private static function generateRandomString($length = 10)
  222. {
  223. // 定义大写字母和数字的字符集
  224. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  225. // 获取字符集的长度
  226. $charactersLength = strlen($characters);
  227. // 初始化随机字符串
  228. $randomString = '';
  229. // 生成随机字符串
  230. for ($i = 0; $i < $length; $i++) {
  231. // 从字符集随机选择一个字符
  232. $randomString .= $characters[rand(0, $charactersLength - 1)];
  233. }
  234. return $randomString;
  235. }
  236. }