| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Services;
- use App\Constants\HttpStatus;
- use App\Models\User;
- use Exception;
- use Illuminate\Support\Facades\App;
- use Illuminate\Support\Facades\Schema;
- use App\Services\Sharing\ShareRelations;
- class PublicService extends BaseService
- {
- public static function init($telegram, $data, $chatId, $firstName, $messageId): void
- {
- switch ($data) {
- case "message@@close"://关闭本条消息
- $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
- break;
- }
- }
- /**
- * 获取Tg用户的 id 昵称 用户名
- * @param $message
- * @param $from
- * @return array
- */
- public static function getChatInfo($message, $from): array
- {
- $chatId = $message->chat->id;
- $firstName = $message->chat->firstName;
- $username = '';
- if (!$from->isBot) {
- $chatId = $from->id;
- $firstName = $from->firstName;
- $username = $from->username;
- }
- return [$chatId, $firstName, $username];
- }
- /**
- * 用户初始化 (用户注册)/ 更新用户昵称
- * @param $chatId
- * @param $username
- * @param $firstName
- * @return User
- */
- public static function index($chatId, $username, $firstName): User
- {
- $user = User::where('member_id', $chatId)->first();
- $newMember = !$user;
- if (!$user) {
- $user = new User();
- $user->from = -1;
- $user->user_code = User::generateUserCode();
- }
- $user->member_id = $chatId;
- $user->first_name = $firstName;
- if ($username) $user->username = $username;
- if (empty($user->user_id)) {
- $user->user_id = $chatId;
- }
- $user->save();
- //给每个用户生成一个专属的钱包
- WalletService::getUserWallet($chatId);
- // Telegram real-money members can arrive after the bulk sharing initialization.
- if ($newMember && Schema::hasTable('share_system_state')) {
- (new ShareRelations())->bind((int)$user->id, null, (string)$user->user_code, 'root');
- }
- return $user;
- }
- public static function getWhere(array $search = []): array
- {
- // TODO: Implement getWhere() method.
- return [];
- }
- }
|