no message
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\user\User;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 短链接分享海报文本与素材的统一构造服务
|
||||
*/
|
||||
class ShortLinkPosterDataService
|
||||
{
|
||||
/**
|
||||
* 获取海报展示用的全部文本与素材
|
||||
*/
|
||||
public static function build(string $pageType, $pageId, ?int $userId, string $clientTitle = '', string $clientDesc = ''): array
|
||||
{
|
||||
$title = trim($clientTitle);
|
||||
$description = trim($clientDesc);
|
||||
|
||||
if ($title === '' || $description === '') {
|
||||
$detail = self::resolvePageDetail($pageType, $pageId);
|
||||
if ($title === '') {
|
||||
$title = $detail['title'];
|
||||
}
|
||||
if ($description === '') {
|
||||
$description = $detail['description'];
|
||||
}
|
||||
}
|
||||
|
||||
$sharer = ['nickname' => '', 'avatar' => ''];
|
||||
if (!empty($userId)) {
|
||||
$user = User::field('nickname,avatar')->findOrEmpty($userId);
|
||||
if (!$user->isEmpty()) {
|
||||
$sharer['nickname'] = (string) $user->getData('nickname');
|
||||
$sharer['avatar'] = (string) $user->avatar;
|
||||
}
|
||||
}
|
||||
|
||||
$posterImageUri = (string) ConfigService::get('website', 'share_poster', '');
|
||||
$posterImage = $posterImageUri ? FileService::getFileUrl($posterImageUri) : '';
|
||||
$posterTitle = (string) ConfigService::get('website', 'share_poster_title', '');
|
||||
|
||||
return [
|
||||
'poster_image_uri' => $posterImageUri,
|
||||
'poster_image' => $posterImage,
|
||||
'poster_title' => $posterTitle,
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'sharer' => $sharer,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 page_type / page_id 自动取页面标题与描述
|
||||
*/
|
||||
private static function resolvePageDetail(string $pageType, $pageId): array
|
||||
{
|
||||
$empty = ['title' => '', 'description' => ''];
|
||||
if ($pageId === '' || $pageId === null) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
switch ($pageType) {
|
||||
case 'news':
|
||||
$row = Db::name('article')
|
||||
->field('title,desc,abstract')
|
||||
->where('id', (int) $pageId)
|
||||
->find();
|
||||
if (empty($row)) {
|
||||
return $empty;
|
||||
}
|
||||
$desc = $row['desc'] ?: mb_substr(strip_tags((string) $row['abstract']), 0, 80);
|
||||
return ['title' => (string) $row['title'], 'description' => (string) $desc];
|
||||
|
||||
case 'match':
|
||||
$row = Db::name('match')
|
||||
->field('home_team,away_team,league_name')
|
||||
->where('id', (int) $pageId)
|
||||
->find();
|
||||
if (empty($row)) {
|
||||
return $empty;
|
||||
}
|
||||
return [
|
||||
'title' => $row['home_team'] . ' vs ' . $row['away_team'],
|
||||
'description' => (string) $row['league_name'],
|
||||
];
|
||||
|
||||
case 'post':
|
||||
case 'community':
|
||||
$row = Db::name('community_post')
|
||||
->field('content')
|
||||
->where('id', (int) $pageId)
|
||||
->find();
|
||||
if (empty($row)) {
|
||||
return $empty;
|
||||
}
|
||||
$text = mb_substr(strip_tags((string) $row['content']), 0, 80);
|
||||
return ['title' => $text ?: '社区帖子', 'description' => ''];
|
||||
|
||||
case 'crypto':
|
||||
return ['title' => $pageId . '/USDT 行情', 'description' => '加密货币实时行情'];
|
||||
|
||||
case 'invite':
|
||||
return ['title' => '邀请你加入我的团队', 'description' => ''];
|
||||
}
|
||||
|
||||
return $empty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user