🐛 修复帖子跳转异常

This commit is contained in:
BTMuli
2023-12-18 18:24:01 +08:00
parent 430a703c2d
commit 8a409f3e9b
3 changed files with 67 additions and 2 deletions

View File

@@ -14,8 +14,9 @@
</template>
<script lang="ts" setup>
import { toRaw } from "vue";
import { useRouter } from "vue-router";
import { parseLink } from "../../utils/linkParser";
import { parseLink, parsePost } from "../../utils/linkParser";
import showSnackbar from "../func/snackbar";
interface TpLinkCard {
@@ -41,11 +42,22 @@ interface TpLinkCardProps {
}
const props = defineProps<TpLinkCardProps>();
const router = useRouter();
console.log("tpLinkCard", props.data.insert.link_card.card_id, toRaw(props.data).insert.link_card);
async function toLink() {
const link = props.data.insert.link_card.landing_url;
const isPost = await parsePost(link);
if (isPost !== false) {
await router.push({
name: "帖子详情",
params: {
post_id: isPost,
},
});
return;
}
const res = await parseLink(link);
if (res === true) return;
if (res === false) {

View File

@@ -22,9 +22,10 @@
</template>
<script lang="ts" setup>
import { onMounted, ref, StyleValue, toRaw } from "vue";
import { useRouter } from "vue-router";
import { getEmojis } from "../../plugins/Mys/request/getEmojis";
import { parseLink } from "../../utils/linkParser";
import { parseLink, parsePost } from "../../utils/linkParser";
import { isColorSimilar } from "../../utils/toolFunc";
import showSnackbar from "../func/snackbar";
@@ -46,6 +47,7 @@ const props = defineProps<TpTextProps>();
const mode = ref<string>("text");
const localEmojis = ref(localStorage.getItem("emojis"));
const emojis = ref<TpText[]>([]);
const router = useRouter();
console.log("tpText", JSON.stringify(props.data.insert), toRaw(props.data)?.attributes);
@@ -101,6 +103,16 @@ async function toLink() {
if (!props.data.attributes) return;
if (!props.data.attributes.link) return;
const link = props.data.attributes.link;
const isPost = await parsePost(link);
if (isPost !== false) {
await router.push({
name: "帖子详情",
params: {
post_id: isPost,
},
});
return;
}
const res = await parseLink(link);
if (res === true) return;
if (res === false) {

View File

@@ -8,6 +8,44 @@ import TGClient from "./TGClient";
import { createPost } from "./TGWindow";
import showConfirm from "../components/func/confirm";
/**
* @function parsePost
* @since Beta v0.3.8
* @description 处理帖子
* @param {string} link
* @returns {Promise<false|string>} - 处理情况,或者转换后的链接
*/
export async function parsePost(link: string): Promise<false | string> {
const url = new URL(link);
if (url.protocol !== "https:" && url.protocol !== "http:") {
if (url.protocol === "mihoyobbs:") {
if (url.pathname.startsWith("//article/")) {
const postId = url.pathname.split("/").pop();
if (!postId) return false;
return postId;
}
if (url.pathname === "//webview" && url.search.startsWith("?link=")) {
const urlTransform = decodeURIComponent(url.search.replace("?link=", ""));
return await parsePost(urlTransform);
}
// todo 不保证转换后的链接可用
if (url.pathname === "//openURL" && url.search.startsWith("?url=")) {
const urlTransform = decodeURIComponent(url.search.replace("?url=", ""));
return await parsePost(urlTransform);
}
}
return false;
}
if (url.hostname === "bbs.mihoyo.com" || url.hostname === "www.miyoushe.com") {
if (url.pathname.includes("/article/")) {
const postId = url.pathname.split("/").pop();
if (typeof postId !== "string") return false;
return postId;
}
}
return false;
}
/**
* @function parseLink
* @since Beta v0.3.8
@@ -45,6 +83,9 @@ export async function parseLink(
if (url.pathname.includes("/article/")) {
const postId = url.pathname.split("/").pop();
if (typeof postId !== "string") return false;
if (!useInner) {
return "post";
}
createPost(postId);
return true;
}