首次提交 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
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Models;
use App\Services\UserFun;
use Illuminate\Database\Eloquent\Model;
class Ledger extends Model
{
// 显式指定表名
protected $table = 'ledger';
protected $guarded = ['id'];
// 时间字段类型自动转为 Carbon 实例
protected $casts = [
'date' => 'datetime',
'amount' => 'decimal:2',
];
public static function AddIncome($data){
$ledger= new Ledger();
$ledger->chat_id=$data['chat_id'];
$ledger->user_id=$data['user_id'];
$ledger->date=time();
$ledger->amount=$data['amount'];
if($ledger->save()){
return UserFun::success();
} else {
return UserFun::error('资料写入失败');
}
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TgBot extends Model
{
// 显式指定表名
protected $table = 'tg_bot';
protected $guarded = ['id'];
// 可批量赋值的字段
protected $fillable = [
'bot_id',
'bot_token',
'bot_username',
'subjection_username',
'subjection_mobile',
'subjection',
'webhook',
'memo',
];
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class TgComm extends Model
{
// 显式指定表名
protected $table = 'tg_comm';
protected $guarded = ['id'];
//将所有的指令转换成数组文字档
public static function TgComm2Arr(){
$data = tg_comm::all()->toArray();
$export = '<?php return ' . var_export($data, true) . ';';
Storage::disk('dataconfig')->put('TgComm.php', $export);
}
}
+77
View File
@@ -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;
}
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TgGroups extends Model
{
protected $table = 'tg_groups';
public $incrementing = false; // Telegram 的 chat_id 是 bigint,可為負數
protected $keyType = 'string';
protected $guarded = ['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,
]
);
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace App\Models;
use App\Services\UserFun;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class TgMsg extends Model
{
protected $table = 'tg_msg';
protected $guarded = ['id'];
protected $casts = [
'message_date' => '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
);
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace App\Models;
use App\Services\UserFun;
use Illuminate\Database\Eloquent\Model;
class UserAccount extends Model
{
// 显式指定表名
protected $table = ' user_account';
protected $guarded = ['id'];
// 时间字段类型自动转为 Carbon 实例
protected $casts = [
'date' => 'datetime',
'amount' => 'decimal:2',
'balance' => 'decimal:2',
];
//检查账号是否存在
public static function CheckUserId($user){
return UserAccount::where([
'chat_id' => $user['chat_id'],
'user_id' => $user['user_id'],
])->exists();
}
//更新账号资料
public static function UpdateUser($user){
UserAccount::where([
'chat_id' => $user['chat_id'],
'user_id' => $user['user_id'],
])->increment('balance', $user['amount']);
$balance = UserAccount::where([
'chat_id' => $user['chat_id'],
'user_id' => $user['user_id'],
])->value('balance');
return $balance;
}
//新建账号
public static function CreatUser($user){
$UserAccount=new UserAccount();
$UserAccount->chat_id=$user['chat_id'];
$UserAccount->user_id=$user['user_id'];
$UserAccount->username=$user['username'];
$UserAccount->stime=now();
$UserAccount->balance=$user['amount'];
$UserAccount->subjection=$user['token'];
$UserAccount->save();
return $user['amount'];
}
}