Files
DevandCursor 60daff2365 搭建轻舟一期门户、后台和统一登录。
让分类、会员和示例工具共用同一账号,本地即可跑通登录与授权。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-09 16:46:35 +08:00

96 lines
3.1 KiB
PHP

<?php
namespace App\Support;
use App\Models\Plan;
use App\Models\Tool;
use App\Models\User;
use App\Services\MembershipService;
class Presenter
{
public static function mask(string $type, string $value): string
{
if ($type === 'phone' && strlen($value) >= 7) {
return substr($value, 0, 3).'****'.substr($value, -4);
}
if ($type === 'email' && str_contains($value, '@')) {
[$name, $domain] = explode('@', $value, 2);
return mb_substr($name, 0, 1).'***@'.$domain;
}
return $value;
}
public static function user(User $user, bool $full = false): array
{
$user->loadMissing(['identities', 'subscription.plan']);
$membership = app(MembershipService::class)->snapshot($user);
return [
'id' => $user->id,
'nickname' => $user->nickname,
'role' => $user->role,
'status' => $user->status,
'disabled_reason' => $full ? $user->disabled_reason : null,
'entitlement_version' => $user->entitlement_version,
'identities' => $user->identities->map(fn ($identity) => [
'id' => $identity->id,
'type' => $identity->type,
'masked' => self::mask($identity->type, $identity->identifier),
'identifier' => $full ? $identity->identifier : null,
'verified' => $identity->verified_at !== null,
])->values(),
'subscription' => $membership,
'created_at' => $user->created_at?->toIso8601String(),
];
}
public static function toolCard(Tool $tool): array
{
return [
'id' => $tool->id,
'name' => $tool->name,
'slug' => $tool->slug,
'icon' => $tool->icon,
'summary' => $tool->summary,
'type' => $tool->type,
'badge' => $tool->badge,
'tags' => $tool->tags->map(fn ($tag) => [
'name' => $tag->name,
'slug' => $tag->slug,
])->values(),
];
}
public static function toolDetail(Tool $tool): array
{
return self::toolCard($tool) + [
'content' => $tool->content,
'open_url' => $tool->type === 'link' ? url('/api/v1/tools/'.$tool->slug.'/go') : $tool->open_url,
'required_entitlement' => $tool->required_entitlement,
'clicks' => $tool->clicks,
'language' => $tool->language,
];
}
public static function plan(Plan $plan): array
{
return [
'id' => $plan->id,
'code' => $plan->code,
'name' => $plan->name,
'price_cents' => $plan->price_cents,
'period' => $plan->period,
'description' => $plan->description,
'sort' => $plan->sort,
'status' => $plan->status,
'entitlements' => $plan->entitlements->map(fn ($item) => [
'key' => $item->entitlement_key,
'value' => $item->entitlement_value,
])->values(),
];
}
}