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