Skip to content

Commit

Permalink
🐛 修复link_card_ids字段不识别
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Dec 25, 2024
1 parent f401b8d commit ab5dfa9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 52 deletions.
7 changes: 6 additions & 1 deletion src/components/viewPost/tp-linkCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
{{ props.data.insert.link_card.price }}
</div>
<div @click="toLink()" class="tp-link-card-btn">
{{ props.data.insert.link_card.button_text ?? "详情" }} >
{{
props.data.insert.link_card.button_text === ""
? "详情"
: props.data.insert.link_card.button_text
}}
>
</div>
</div>
</div>
Expand Down
53 changes: 44 additions & 9 deletions src/plugins/Mys/types/Post.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file plugins/Mys/types/post.d.ts
* @description Mys 插件帖子类型定义文件
* @since Beta v0.6.3
* @since Beta v0.6.7
*/

declare namespace TGApp.Plugins.Mys.Post {
Expand All @@ -13,15 +13,11 @@ declare namespace TGApp.Plugins.Mys.Post {
* @property {FullData} data.post 帖子数据
* @return Response
*/
interface Response extends TGApp.BBS.Response.BaseWithData {
data: {
post: FullData;
};
}
type Response = TGApp.BBS.Response.BaseWithData & { data: { post: FullData } };

/**
* @description 帖子数据
* @since Beta v0.5.0
* @since Beta v0.6.7
* @interface FullData
* @property {Post} post 帖子信息
* @property {Forum|null} forum 所属版块,可能为 null
Expand All @@ -42,7 +38,7 @@ declare namespace TGApp.Plugins.Mys.Post {
* @property {unknown[]} vod_list 视频列表,可能为空
* @property {boolean} is_block_on 是否被屏蔽
* @property {unknown} forum_rank_info 版块排行信息,可能为 null
* @property {unknown[]} link_card_list 链接卡片列表,可能为空
* @property {LinkCard[]} link_card_list 链接卡片列表,可能为空
* @property {TGApp.Plugins.Mys.News.Meta} news_meta 咨讯元数据,可能为 null
* @return FullData
*/
Expand All @@ -66,7 +62,7 @@ declare namespace TGApp.Plugins.Mys.Post {
vod_list: Vod[];
is_block_on: boolean;
forum_rank_info: unknown | null;
link_card_list: unknown[];
link_card_list: LinkCard[];
news_meta: TGApp.Plugins.Mys.News.Meta | null | undefined;
recommend_reason: unknown | null;
villa_card: unknown | null;
Expand Down Expand Up @@ -302,6 +298,45 @@ declare namespace TGApp.Plugins.Mys.Post {
next_post_view_type: number;
}

/**
* @description 链接卡片信息
* @since Beta v0.6.7
* @interface LinkCard
* @property {string} button_text 按钮文本
* @property {string} card_id 卡片 ID
* @property {unknown} card_meta 卡片元数据
* @property {number} card_status 卡片状态
* @property {string} cover 封面图 URL
* @property {string} landing_url 落地页 URL
* @property {number} landing_url_type 落地页类型
* @property {number} link_type 链接类型
* @property {string} market_price 市场价
* @property {string} origin_url 原始 URL
* @property {string} origin_user_avatar 原始用户头像 URL
* @property {string} origin_user_avatar_url 原始用户头像 URL
* @property {string} origin_user_nickname 原始用户名
* @property {string} price 价格
* @property {string} title 标题
* @returns LinkCard
*/
type LinkCard = {
button_text: string;
card_id: string;
card_meta: unknown;
card_status: number;
cover: string;
landing_url: string;
landing_url_type: number;
link_type: number;
market_price: string;
origin_url: string;
origin_user_avatar: string;
origin_user_avatar_url: string;
origin_user_nickname: string;
price: string;
title: string;
};

/**
* @description 视频信息
* @since Beta v0.3.7
Expand Down
29 changes: 7 additions & 22 deletions src/plugins/Mys/types/SctPost.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/**
* @file plugins/Mys/types/SctPost.d.ts
* @description Mys 插件 结构化帖子类型声明文件
* @since Beta v0.4.5
* @since Beta v0.6.7
*/

/**
* @description 结构化帖子类型命名空间
* @since Beta v0.4.5
* @namespace TGApp.Plugins.Mys.SctPost
* @memberof TGApp.Plugins.Mys
*/
declare namespace TGApp.Plugins.Mys.SctPost {
/**
* @description 帖子结构化数据-基础类型
Expand All @@ -20,11 +14,7 @@ declare namespace TGApp.Plugins.Mys.SctPost {
* @property {Base[]} children - 子帖子
* @return Base
*/
interface Base {
insert: any;
attributes?: any;
children?: Base[];
}
type Base = { insert: any; attributes?: any; children?: Base[] };

/**
* @description 帖子结构化数据-空类型
Expand All @@ -34,22 +24,17 @@ declare namespace TGApp.Plugins.Mys.SctPost {
* @property {never} attributes - 帖子属性
* @return Empty
*/
interface Empty {
insert: never;
attributes?: never;
}
type Empty = { insert: never; attributes?: never };

/**
* @description 帖子结构化数据-其他类型
* @since Beta v0.3.4
* @since Beta v0.6.7
* @property {string} describe - 描述
* @property {string[]} imgs - 图片链接
* @property {string[]} link_card_ids - 关联卡片ID
* @return Other
*/
interface Other {
describe: string;
imgs: string[];

type Other = { describe: string; imgs: string[]; link_card_ids?: string[] } & {
[key: string]: unknown;
}
};
}
59 changes: 39 additions & 20 deletions src/views/t-post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<VpBtnReply :gid="postData.post.game_id" :post-id="postData.post.post_id" v-if="postData" />
<div class="tp-post-body" v-if="postData">
<div class="tp-post-info">
<div class="tp-post-version">
<div class="tp-post-version" @click="openJson()">
PostID:{{ postId }} | Render by TeyvatGuide v{{ appVersion }}
</div>
<div class="tp-post-meta">
Expand Down Expand Up @@ -38,7 +38,7 @@
</div>
<TpAvatar :data="postData.user" position="right" v-if="postData.user" />
</div>
<div class="tp-post-title" @click="toPost()" title="点击查看评论">
<div class="tp-post-title" @click="toPost()">
<span class="mpt-official" v-if="postData.post.post_status.is_official">官</span>
<span>{{ postData.post.subject }}</span>
</div>
Expand Down Expand Up @@ -100,9 +100,9 @@ import { onMounted, onUnmounted, ref, shallowRef } from "vue";
import { useRoute } from "vue-router";
import { useAppStore } from "@/store/modules/app.js";
import TGClient from "@/utils/TGClient.js";
import TGLogger from "@/utils/TGLogger.js";
import { createTGWindow } from "@/utils/TGWindow.js";
import { CHANNEL_LIST } from "@/web/constant/bbs.js";
import TGConstant from "@/web/constant/TGConstant.js";
const appVersion = ref<string>();
Expand Down Expand Up @@ -141,17 +141,13 @@ onMounted(async () => {
}
postData.value = resp;
await showLoading.update("正在渲染数据");
renderPost.value = getRenderPost(postData.value);
renderPost.value = await getRenderPost(postData.value);
await webviewWindow
.getCurrentWebviewWindow()
.setTitle(`Post_${postId} ${postData.value.post.subject}`);
await TGLogger.Info(`[t-post][${postId}][onMounted] ${postData.value.post.subject}`);
const isDev = useAppStore().devMode ?? false;
if (isDev) {
await showLoading.update("正在打开调试窗口");
await TGLogger.Info(`[t-post][${postId}][onMounted] 打开 JSON 窗口`);
await createPostJson(postId);
}
if (isDev) await openJson();
if (shareTimer !== null) {
clearInterval(shareTimer);
shareTimer = null;
Expand All @@ -160,6 +156,12 @@ onMounted(async () => {
await showLoading.end();
});
async function openJson(): Promise<void> {
// @ts-expect-error import.meta
if (import.meta.env.MODE === "production") return;
await createPostJson(postId);
}
function getShareTimer(): void {
shareTime.value = Math.floor(Date.now() / 1000);
}
Expand All @@ -183,40 +185,53 @@ function getRepublishAuthorization(type: number): string {
}
}
function getRenderPost(data: TGApp.Plugins.Mys.Post.FullData): TGApp.Plugins.Mys.SctPost.Base[] {
async function getRenderPost(
data: TGApp.Plugins.Mys.Post.FullData,
): Promise<Array<TGApp.Plugins.Mys.SctPost.Base>> {
const postContent = data.post.content;
let jsonParse: string;
if (postContent.startsWith("<")) {
jsonParse = data.post.structured_content;
} else {
try {
jsonParse = parseContent(data.post.content);
jsonParse = await parseContent(data.post.content);
} catch (e) {
if (e instanceof SyntaxError) TGLogger.Warn(`[t-post][${postId}] ${e.name}: ${e.message}`);
if (e instanceof SyntaxError) {
await TGLogger.Warn(`[t-post][${postId}] ${e.name}: ${e.message}`);
}
jsonParse = data.post.structured_content;
}
}
return JSON.parse(jsonParse);
}
function parseContent(content: string): string {
async function parseContent(content: string): Promise<string> {
const data: TGApp.Plugins.Mys.SctPost.Other = JSON.parse(content);
const result: TGApp.Plugins.Mys.SctPost.Base[] = [];
const keys = Object.keys(data);
keys.forEach((key) => {
for (const key of Object.keys(data)) {
switch (key) {
case "describe":
result.push({ insert: data.describe });
break;
case "imgs":
data.imgs.forEach((item) => result.push({ insert: { image: item } }));
break;
case "link_card_ids":
if (!data.link_card_ids) break;
for (const item of data.link_card_ids) {
const dataFind = postData.value?.link_card_list.find((card) => card.card_id === item);
if (dataFind) result.push({ insert: { link_card: dataFind } });
else {
await TGLogger.Warn(`[t-post][${postId}][parseContent] link_card_ids: ${item} 未找到`);
}
}
break;
default:
TGLogger.Warn(`[t-post][${postId}][parseContent] Unknown key: ${key}`);
await TGLogger.Warn(`[t-post][${postId}][parseContent] Unknown key: ${key}`);
result.push({ insert: data[key] });
break;
}
});
}
return JSON.stringify(result);
}
Expand All @@ -226,9 +241,13 @@ async function createPostJson(postId: number): Promise<void> {
await createTGWindow(jsonPath, "Dev_JSON", jsonTitle, 960, 720, false, false);
}
async function toPost(): Promise<void> {
const url = `https://m.miyoushe.com/ys/#/article/${postId}`;
await TGClient.open("web_thin", url);
function toPost(): void {
const channel = CHANNEL_LIST.find((item) => item.gid === postData.value?.post.game_id.toString());
if (channel) {
window.open(`https://miyoushe.com/${channel.mini}/#/article/${postId}`);
} else {
window.open(`https://miyoushe.com/ys/#/article/${postId}`);
}
}
async function toTopic(topic: TGApp.Plugins.Mys.Topic.Info): Promise<void> {
Expand Down

0 comments on commit ab5dfa9

Please sign in to comment.