首次提交 Laravel 專案

This commit is contained in:
2025-05-18 08:52:56 +07:00
commit 5e392ae581
264 changed files with 88756 additions and 0 deletions
@@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tg_groups', function (Blueprint $table) {
$table->id(); // Laravel 主鍵
$table->bigInteger('chat_id')->unique(); // Telegram 群組/頻道 ID(可能為負數)
$table->string('type')->nullable()->comment('群类型'); // group、supergroup、channel、private
$table->string('title')->nullable()->comment('群名稱');
$table->string('creator')->nullable()->comment('群主ID');
$table->longText('description')->nullable()->comment('群組描述'); // 群組描述(如有)
$table->string('invite_link')->nullable()->comment('群組邀請連結'); // 群組邀請連結(如有)
// 可選擴充
$table->string('photo_small_file_id')->nullable()->comment('群头像160x160');
$table->string('photo_big_file_id')->nullable()->comment('群头像640x640');
$table->dateTime('created_at')->useCurrent()->nullable();
$table->dateTime('updated_at')->nullable()->useCurrentOnUpdate();
$table->comment('飞机群');
// 设置字符集与排序规则
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_0900_ai_ci';
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tg_group');
}
};
@@ -0,0 +1,79 @@
<?php
namespace App\Models;
use App\Services\UserFun;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class tg_Msg extends Model
{
protected $table = 'tg_msg';
protected $fillable = [
'update_id',
'message_message_id',
'message_from_id',
'message_from_is_bot',
'message_from_first_name',
'message_from_last_name',
'message_from_username',
'message_from_language_code',
'message_chat_id',
'message_chat_title',
'message_chat_type',
'message_chat_all_members_are_administrators',
'message_date',
'message_text',
'message_entities_0_offset',
'message_entities_0_length',
'message_entities_0_type',
'raw',
'token',
];
protected $casts = [
'message_date' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'raw' => 'array',
];
public static function storeFromWebhookJson(string|array $json): self
{
// 當$json是 string 時才做 Json2Arr,否則直接當成已展平的資料用
$data = is_array($json) ? $json : UserFun::Json2Arr($json);
// 要儲存的欄位清單
$fields = collect($data)->only([
'message_message_id',
'message_from_id',
'message_from_is_bot',
'message_from_first_name',
'message_from_last_name',
'message_from_username',
'message_from_language_code',
'message_chat_id',
'message_chat_title',
'message_chat_type',
'message_chat_all_members_are_administrators',
'message_text',
'message_entities_0_offset',
'message_entities_0_length',
'message_entities_0_type',
'token',
])->toArray();
// 處理 message_date (timestamp -> datetime)
if (isset($data['message_date'])) {
$fields['message_date'] = date('Y-m-d H:i:s', $data['message_date']);
}
// 原始扁平化資料存入 raw 欄位
$fields['raw'] = $data;
return self::updateOrCreate(
['update_id' => $data['update_id']],
$fields
);
}
}