SecretService.php 9.4 KB

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