mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-04-03 06:55:06 +08:00
@@ -2,7 +2,7 @@
|
||||
* @file plugins Mys request news.ts
|
||||
* @description Mys 插件咨讯请求
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
*/
|
||||
|
||||
import { http } from "@tauri-apps/api";
|
||||
@@ -10,7 +10,7 @@ import { NewsData, NewsResponse } from "../interface/news";
|
||||
|
||||
// 咨讯 API
|
||||
const NEWS_LIST_API =
|
||||
"https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids=2&page_size={page_size}&type={news_type}&last_id={last_id}";
|
||||
"https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids={gid}&page_size={page_size}&type={news_type}&last_id={last_id}";
|
||||
|
||||
/**
|
||||
* @description 咨讯类型
|
||||
@@ -29,16 +29,19 @@ enum NewsType {
|
||||
|
||||
/**
|
||||
* @description 获取 Notice 列表
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
* @param {string} gid gid: 1 为崩坏3, 2 为原神,3 为崩坏2,4 为未定事件簿,5 为大别野,6 为崩坏:星穹铁道,8 为绝区零
|
||||
* @param {number} page_size 返回数量
|
||||
* @param {number} last_id 上一次请求的最后一条数据的 id
|
||||
* @return {Promise<NewsData>}
|
||||
*/
|
||||
export async function getNoticeList(
|
||||
gid: string = "2",
|
||||
page_size: number = 20,
|
||||
last_id: number = 0
|
||||
): Promise<NewsData> {
|
||||
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString())
|
||||
.replace("{gid}", gid)
|
||||
.replace("{news_type}", NewsType.NOTICE)
|
||||
.replace("{last_id}", last_id.toString());
|
||||
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
|
||||
@@ -46,16 +49,19 @@ export async function getNoticeList(
|
||||
|
||||
/**
|
||||
* @description 获取 Activity 列表
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
* @param {string} gid gid: 1 为崩坏3, 2 为原神,3 为崩坏2,4 为未定事件簿,5 为大别野,6 为崩坏:星穹铁道,8 为绝区零
|
||||
* @param {number} page_size 返回数量
|
||||
* @param {number} last_id 上一次请求的最后一条数据的 id
|
||||
* @return {Promise<NewsData>}
|
||||
*/
|
||||
export async function getActivityList(
|
||||
gid: string = "2",
|
||||
page_size: number = 20,
|
||||
last_id: number = 0
|
||||
): Promise<NewsData> {
|
||||
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString())
|
||||
.replace("{gid}", gid)
|
||||
.replace("{news_type}", NewsType.ACTIVITY)
|
||||
.replace("{last_id}", last_id.toString());
|
||||
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
|
||||
@@ -63,13 +69,19 @@ export async function getActivityList(
|
||||
|
||||
/**
|
||||
* @description 获取 News 列表
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
* @param {string} gid gid: 1 为崩坏3, 2 为原神,3 为崩坏2,4 为未定事件簿,5 为大别野,6 为崩坏:星穹铁道,8 为绝区零
|
||||
* @param {number} page_size 返回数量
|
||||
* @param {number} last_id 上一次请求的最后一条数据的 id
|
||||
* @return {Promise<NewsData>}
|
||||
*/
|
||||
export async function getNewsList(page_size: number = 20, last_id: number = 0): Promise<NewsData> {
|
||||
export async function getNewsList(
|
||||
gid: string = "2",
|
||||
page_size: number = 20,
|
||||
last_id: number = 0
|
||||
): Promise<NewsData> {
|
||||
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString())
|
||||
.replace("{gid}", gid)
|
||||
.replace("{news_type}", NewsType.NEWS)
|
||||
.replace("{last_id}", last_id.toString());
|
||||
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
|
||||
|
||||
@@ -2,19 +2,19 @@
|
||||
* @file plugins Mys request post.ts
|
||||
* @description Mys帖子请求
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
* @since Alpha v0.1.2
|
||||
*/
|
||||
|
||||
import { http } from "@tauri-apps/api";
|
||||
import { PostResponse, PostData } from "../interface/post";
|
||||
|
||||
// 帖子 API
|
||||
const POST_API = "https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=2&post_id={post_id}";
|
||||
const POST_REFERER = "https://bbs.mihoyo.com/ys/article/{post_id}";
|
||||
const POST_API = "https://bbs-api.mihoyo.com/post/wapi/getPostFull?post_id={post_id}";
|
||||
const POST_REFERER = "https://bbs.mihoyo.com/";
|
||||
|
||||
/**
|
||||
* @description 获取帖子信息
|
||||
* @since Alpha
|
||||
* @since Alpha v0.1.2
|
||||
* @param {number} post_id 帖子 ID
|
||||
* @return {Promise<PostData>}
|
||||
*/
|
||||
@@ -24,7 +24,7 @@ export async function getPostData(post_id: number): Promise<PostData> {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Referer: POST_REFERER.replace("{post_id}", post_id.toString()),
|
||||
Referer: POST_REFERER,
|
||||
},
|
||||
})
|
||||
.then(res => {
|
||||
|
||||
@@ -2,21 +2,20 @@
|
||||
* @file plugins Mys utils PostParser.ts
|
||||
* @description 用于解析Mys数据的工具
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
*/
|
||||
import { PostContent, PostData, PostStructuredContent } from "../interface/post";
|
||||
|
||||
/**
|
||||
* @description 检测链接是否是米游社帖子
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
* @param {string} url 链接
|
||||
* @returns {boolean} 是否是米游社帖子
|
||||
*/
|
||||
export function IsMysPost(url: string): boolean {
|
||||
return (
|
||||
url.startsWith("https://bbs.mihoyo.com/ys/article/") ||
|
||||
url.startsWith("https://www.miyoushe.com/ys/article/")
|
||||
);
|
||||
const regBBS = /^https:\/\/bbs\.mihoyo\.com\/\w+\/article\/\d+$/;
|
||||
const regMYS = /^https:\/\/www\.miyoushe\.com\/\w+\/article\/\d+$/;
|
||||
return regBBS.test(url) || regMYS.test(url);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +57,7 @@ export function contentParser(content: string): string {
|
||||
|
||||
/**
|
||||
* @description 解析Mys数据
|
||||
* @since Alpha v0.1.1
|
||||
* @since Alpha v0.1.2
|
||||
* @param {PostData} post Mys数据
|
||||
* @description 为了安全考虑,不会解析所有的属性,只会解析几个常用的属性
|
||||
* @returns {string} 解析后的HTML,可作为 v-html 使用
|
||||
@@ -69,17 +68,16 @@ export function PostParser(post: PostData): string {
|
||||
if (postContent.startsWith("<")) {
|
||||
parserData = post.post.structured_content;
|
||||
} else {
|
||||
parserData = contentParser(postContent);
|
||||
try {
|
||||
parserData = contentParser(post.post.content);
|
||||
} catch (error) {
|
||||
parserData = post.post.structured_content;
|
||||
}
|
||||
}
|
||||
// Json 化
|
||||
let jsonData: PostStructuredContent[] = JSON.parse(parserData);
|
||||
// 创建 div
|
||||
const doc = document.createElement("div");
|
||||
// 遍历 Json 数据
|
||||
jsonData.forEach((item: any) => {
|
||||
// 解析
|
||||
const parsed = ParserTransfer(item);
|
||||
// 插入
|
||||
doc.appendChild(parsed);
|
||||
});
|
||||
return doc.innerHTML;
|
||||
|
||||
Reference in New Issue
Block a user