PublicService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\User;
  5. use Exception;
  6. use Illuminate\Support\Facades\App;
  7. use Illuminate\Support\Facades\Schema;
  8. use App\Services\Sharing\ShareRelations;
  9. class PublicService extends BaseService
  10. {
  11. public static function init($telegram, $data, $chatId, $firstName, $messageId): void
  12. {
  13. switch ($data) {
  14. case "message@@close"://关闭本条消息
  15. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  16. break;
  17. }
  18. }
  19. /**
  20. * 获取Tg用户的 id 昵称 用户名
  21. * @param $message
  22. * @param $from
  23. * @return array
  24. */
  25. public static function getChatInfo($message, $from): array
  26. {
  27. $chatId = $message->chat->id;
  28. $firstName = $message->chat->firstName;
  29. $username = '';
  30. if (!$from->isBot) {
  31. $chatId = $from->id;
  32. $firstName = $from->firstName;
  33. $username = $from->username;
  34. }
  35. return [$chatId, $firstName, $username];
  36. }
  37. /**
  38. * 用户初始化 (用户注册)/ 更新用户昵称
  39. * @param $chatId
  40. * @param $username
  41. * @param $firstName
  42. * @return User
  43. */
  44. public static function index($chatId, $username, $firstName): User
  45. {
  46. $user = User::where('member_id', $chatId)->first();
  47. $newMember = !$user;
  48. if (!$user) {
  49. $user = new User();
  50. $user->from = -1;
  51. $user->user_code = User::generateUserCode();
  52. }
  53. $user->member_id = $chatId;
  54. $user->first_name = $firstName;
  55. if ($username) $user->username = $username;
  56. if (empty($user->user_id)) {
  57. $user->user_id = $chatId;
  58. }
  59. $user->save();
  60. //给每个用户生成一个专属的钱包
  61. WalletService::getUserWallet($chatId);
  62. // Telegram real-money members can arrive after the bulk sharing initialization.
  63. if ($newMember && Schema::hasTable('share_system_state')) {
  64. (new ShareRelations())->bind((int)$user->id, null, (string)$user->user_code, 'root');
  65. }
  66. return $user;
  67. }
  68. public static function getWhere(array $search = []): array
  69. {
  70. // TODO: Implement getWhere() method.
  71. return [];
  72. }
  73. }