首次提交 Laravel 專案
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class tg_group extends Model{
|
||||
protected $primaryKey = 'chat_id';
|
||||
public $incrementing = false; // Telegram 的 chat_id 是 bigint,可為負數
|
||||
protected $keyType = 'string';
|
||||
protected $fillable = [
|
||||
'chat_id',
|
||||
'type',
|
||||
'title',
|
||||
'creator',
|
||||
'description',
|
||||
'invite_link',
|
||||
'photo_small_file_id',
|
||||
'photo_big_file_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'chat_id' => 'integer',
|
||||
'type' => 'string',
|
||||
'title' => 'string',
|
||||
'username' => 'string',
|
||||
'description' => 'string',
|
||||
'invite_link' => 'string',
|
||||
'photo_small_file_id' => 'string',
|
||||
'photo_big_file_id' => 'string',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
public static function updateOrCreateFromTelegram(array $chatData): self
|
||||
{
|
||||
return self::updateOrCreate(
|
||||
['chat_id' => $chatData['id']],
|
||||
[
|
||||
'type' => $chatData['type'] ?? null,
|
||||
'title' => $chatData['title'] ?? null,
|
||||
'creator' => $chatData['creator'] ?? null,
|
||||
'invite_link' => $chatData['invite_link'] ?? null,
|
||||
'photo_small_file_id' => $chatData['photo']['small_file_id'] ?? null,
|
||||
'photo_big_file_id' => $chatData['photo']['big_file_id'] ?? null,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
class TgGroupUsers extends Model
|
||||
{
|
||||
protected $table = 'tg_group_users';
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
// 时间字段类型自动转为 Carbon 实例
|
||||
protected $casts = [
|
||||
'raw_data' => 'array',
|
||||
];
|
||||
|
||||
// Telegram status 常量定义
|
||||
public const STATUS_CREATOR = 'creator'; // 群主
|
||||
public const STATUS_ADMINISTRATOR = 'administrator'; // 管理员
|
||||
public const STATUS_MEMBER = 'member'; // 普通成员
|
||||
public const STATUS_RESTRICTED = 'restricted'; // 被限制的成员
|
||||
public const STATUS_LEFT = 'left'; // 已离开
|
||||
public const STATUS_KICKED = 'kicked'; // 被踢出
|
||||
|
||||
// 可选:提供一个状态中文对照表(静态方法)
|
||||
public static function statusLabel(string $status): string
|
||||
{
|
||||
return match ($status) {
|
||||
self::STATUS_CREATOR => '群主',
|
||||
self::STATUS_ADMINISTRATOR => '管理员',
|
||||
self::STATUS_MEMBER => '成员',
|
||||
self::STATUS_RESTRICTED => '受限成员',
|
||||
self::STATUS_LEFT => '已离开',
|
||||
self::STATUS_KICKED => '被踢出',
|
||||
default => '未知',
|
||||
};
|
||||
}
|
||||
|
||||
public static function updateOrCreateFromTelegram(array $chatMemberData, int|string $chatId): self
|
||||
{
|
||||
// 改进的安全判断方式
|
||||
if (
|
||||
!isset($chatMemberData['user']) ||
|
||||
!is_array($chatMemberData['user']) ||
|
||||
!isset($chatMemberData['user']['id']) ||
|
||||
!isset($chatMemberData['status'])
|
||||
) {
|
||||
throw new \InvalidArgumentException('无效的 Telegram ChatMember 数据结构');
|
||||
}
|
||||
|
||||
$user = $chatMemberData['user'];
|
||||
|
||||
return self::updateOrCreate(
|
||||
[
|
||||
'chat_id' => $chatId,
|
||||
'user_id' => $user['id'],
|
||||
],
|
||||
[
|
||||
'first_name' => $user['first_name'] ?? null,
|
||||
'last_name' => $user['last_name'] ?? null,
|
||||
'username' => $user['username'] ?? null,
|
||||
'status' => $chatMemberData['status'],
|
||||
'is_bot' => $user['is_bot'] ?? false,
|
||||
'language_code' => $user['language_code'] ?? null,
|
||||
'raw_data' => json_encode($chatMemberData, JSON_UNESCAPED_UNICODE),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function GetUsername($username){
|
||||
$res=TgGroupUsers::where('username', $username)->first();
|
||||
if($res){
|
||||
return $res;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user