🎨 refractor(genshin): 将 genshin 插件转移到 core 目录下

This commit is contained in:
BTMuli
2023-04-10 00:16:45 +08:00
parent a238dc5eed
commit d7494739f6
19 changed files with 337 additions and 302 deletions

View File

@@ -1,22 +0,0 @@
/**
* @file plugins Genshin index.ts
* @description Genshin plugin index
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
// Announcement
import { getAnnouncementList, getAnnouncementContent } from "./request/announcements";
import { getAnnoCards } from "./utils/announcements";
import { parseAnnoContent } from "./utils/annoParser";
const GenshinOper = {
Announcement: {
getList: getAnnouncementList,
getContent: getAnnouncementContent,
card: getAnnoCards,
parser: parseAnnoContent,
},
};
export default GenshinOper;

View File

@@ -1,211 +0,0 @@
/**
* @file plugins Genshin annoList.ts
* @description 原神游戏内公告列表接口
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
import { type Hk4eResponse } from "./base";
/**
* @description 原神游戏内公告列表返回
* @since Alpha v0.1.1
* @see ANNO_LIST_API
* @interface AnnoListResponse
* @extends Hk4eResponse
* @property {AnnoListData} data 公告数据
* @returns {AnnoListResponse}
*/
export interface AnnoListResponse extends Hk4eResponse {
data: AnnoListData
}
/**
* @description 原神游戏内公告内容返回
* @since Alpha v0.1.1
* @see ANNO_CONTENT_API
* @interface AnnoContentResponse
* @extends Hk4eResponse
* @property {AnnoContentData} data 公告数据
* @returns {AnnoContentResponse}
*/
export interface AnnoContentResponse extends Hk4eResponse {
data: AnnoContentData
}
/**
* @description 公告列表数据
* @since Alpha v0.1.1
* @interface AnnoListData
* @property {Announcement[]} list 公告列表
* @property {number} total 公告总数
* @property {AnnoTypeList[]} type_list 公告类型列表
* @property {boolean} alert 是否有紧急公告
* @property {number} alert_id 紧急公告 ID
* @property {number} time_zone 时区
* @property {string} t 系统时间
* @property {unknown[]} pic_list 图片列表
* @property {number} pic_total 图片总数
* @property {unknown[]} pic_type_list 图片类型列表
* @property {boolean} pic_alert 是否有紧急图片
* @property {number} pic_alert_id 紧急图片 ID
* @property {unknown} static_sign 静态签名
* @returns {AnnoListData}
*/
export interface AnnoListData {
list: Announcement[]
total: number
type_list: AnnoTypeList[]
alert: boolean
alert_id: number
time_zone: number
t: string
pic_list: unknown[]
pic_total: number
pic_type_list: unknown[]
pic_alert: boolean
pic_alert_id: number
static_sign: unknown
}
/**
* @description 公告内容数据
* @since Alpha v0.1.1
* @interface AnnoContentData
* @property {AnnoContentItem[]} list 公告列表
* @property {number} total 公告总数
* @property {unknown[]} pic_list 图片列表
* @property {number} pic_total 图片总数
* @returns {AnnoContentData}
*/
export interface AnnoContentData {
list: AnnoContentItem[]
total: number
pic_list: unknown[]
pic_total: number
}
/**
* @description 公告类型列表
* @since Alpha v0.1.1
* @interface AnnoTypeList
* @property {number} id 类型 ID
* @property {string} name 类型名称
* @property {string} mi18n_name 类型名称
* @returns {AnnoTypeList}
*/
export interface AnnoTypeList {
id: number
name: string
mi18n_name: string
}
/**
* @description 公告
* @since Alpha v0.1.1
* @interface Announcement
* @property {AnnoListItem[]} list 公告列表
* @property {number} type_id 类型 ID
* @property {string} type_label 类型标签
* @returns {Announcement}
*/
export interface Announcement {
list: AnnoListItem[]
type_id: number
type_label: string
}
/**
* @description 公告列表项
* @since Alpha v0.1.1
* @interface AnnoListItem
* @property {number} ann_id 公告 ID
* @property {string} title 公告标题
* @property {string} subtitle 公告副标题
* @property {string} banner 公告图片
* @property {unknown} content 公告内容
* @property {string} type_label 公告类型标签
* @property {string} tag_label 公告标签
* @property {string} tag_icon 公告标签图标
* @property {number} login_alert 是否登录提示
* @property {string} lang 公告语言
* @property {string} start_time 公告开始时间 // "2023-03-01 07:00:00"
* @property {string} end_time 公告结束时间 // "2023-04-12 06:00:00"
* @property {number} type 公告类型
* @property {number} remind 公告提醒
* @property {number} alert 公告紧急
* @property {string} tag_start_time 公告标签开始时间 // "2000-01-02 15:04:05"
* @property {string} tag_end_time 公告标签结束时间 // "2030-01-02 15:04:05"
* @property {number} remind_ver 公告提醒版本
* @property {boolean} has_content 是否有内容
* @property {boolean} extra_remind 是否有额外提醒
* @returns {AnnoListItem}
*/
export interface AnnoListItem {
ann_id: number
title: string
subtitle: string
banner: string
content: unknown
type_label: string
tag_label: string
tag_icon: string
login_alert: number
lang: string
start_time: string
end_time: string
type: number
remind: number
alert: number
tag_start_time: string
tag_end_time: string
remind_ver: number
has_content: boolean
extra_remind: boolean
}
/**
* @description 公告内容列表
* @since Alpha v0.1.1
* @interface AnnoContentItem
* @property {number} ann_id 公告 ID
* @property {string} title 公告标题
* @property {string} subtitle 公告副标题
* @property {string} banner 公告图片
* @property {string} content 公告内容为 HTML
* @property {string} lang 公告语言
* @returns {AnnoContentItem}
*/
export interface AnnoContentItem {
ann_id: number
title: string
subtitle: string
banner: string
content: string
lang: string
}
/**
* @description 渲染用公告列表数据
* @since Alpha v0.1.1
* @interface AnnoListCard
* @property {number} id 公告 ID
* @property {string} title 公告标题
* @property {string} subtitle 公告副标题
* @property {string} banner 公告图片
* @property {string} type_label 公告类型标签
* @property {string} tag_icon 公告标签图标
* @property {string} start_time 公告开始时间
* @property {string} end_time 公告结束时间
* @returns {AnnoListCard}
*/
export interface AnnoListCard {
id: number
title: string
subtitle: string
banner: string
type_label: string
tag_icon: string
start_time: string
end_time: string
}

View File

@@ -1,21 +0,0 @@
/**
* @file plugins Genshin interface base.ts
* @description 原神插件基础接口
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
/**
* @description Mys Response 统一接口,负责游戏内数据获取
* @since Alpha v0.1.1
* @interface Hk4eResponse
* @property {number} retcode 状态码
* @property {string} message 状态信息
* @property {any} data 数据
* @returns {Hk4eResponse}
*/
export interface Hk4eResponse {
retcode: number
message: string
data: any
}

View File

@@ -1,49 +0,0 @@
/**
* @file plugins Genshin request announcements.ts
* @description 原神游戏内公告请求
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
*/
import { http } from "@tauri-apps/api";
import { Hk4eAnnoApi } from "./base";
import {
type AnnoListResponse,
type AnnoContentResponse,
type AnnoListData,
type AnnoContentItem,
} from "../interface/announcement";
// 公告 API
const ANNO_LIST_API = `${Hk4eAnnoApi}/getAnnList?`;
const ANNO_CONTENT_API = `${Hk4eAnnoApi}/getAnnContent?`;
// 公告 Query
const ANNO_QUERY =
"game=hk4e&game_biz=hk4e_cn&lang=zh-cn&bundle_id=hk4e_cn&platform=pc&region=cn_gf01&level=60&uid=500299765";
/**
* @description 获取游戏内公告列表
* @since Alpha v0.1.1
* @returns {Promise<AnnoListData>}
*/
export async function getAnnouncementList (): Promise<AnnoListData> {
return await http.fetch<AnnoListResponse>(`${ANNO_LIST_API}${ANNO_QUERY}`).then((res) => res.data.data);
}
/**
* @description 获取游戏内公告内容
* @since Alpha v0.1.2
* @param {number} annId 公告 ID
* @returns {Promise<AnnoContentItem>}
*/
export async function getAnnouncementContent (annId: number): Promise<AnnoContentItem> {
const annoContents: AnnoContentItem[] = await http
.fetch<AnnoContentResponse>(`${ANNO_CONTENT_API}${ANNO_QUERY}`)
.then((res) => res.data.data.list);
const annoContent = annoContents.find((item) => item.ann_id === annId);
if (annoContent) {
return annoContent;
} else {
throw new Error("公告内容不存在");
}
}

View File

@@ -1,11 +0,0 @@
/**
* @file plugins Genshin request base.ts
* @description 游戏内数据请求的基础类
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
*/
// Hk4e API目前先用这几个后续有需要再加
const Hk4eApi = "https://hk4e-api.mihoyo.com"; // 基础 API
export const Hk4eAnnoApi = `${Hk4eApi}/common/hk4e_cn/announcement/api`; // 公告 API
export const Hk4eGachaApi = `${Hk4eApi}/event/gacha_info/api;`; // 卡池 API

View File

@@ -1,78 +0,0 @@
/**
* @file plugins Genshin utils annoParser.ts
* @description 原神游戏内公告解析工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
/**
* @description 解析游戏内公告数据
* @since Alpha v0.1.1
* @param {string} data 游戏内公告数据
* @returns {string} 解析后的数据
*/
export function parseAnnoContent (data: string): string {
const htmlBase = new DOMParser().parseFromString(data, "text/html");
// 遍历所有 span 标签
htmlBase.querySelectorAll("span").forEach((span) => {
if (span.style.fontSize) {
span.style.fontSize = "";
}
if (span.children.length === 0) {
return (span.innerHTML = decodeRegExp(span.innerHTML));
} else {
span.querySelectorAll("*").forEach((child) => {
if (child.children.length === 0) {
return (child.innerHTML = decodeRegExp(child.innerHTML));
}
});
}
});
// 遍历所有 p 标签
htmlBase.querySelectorAll("p").forEach((p) => {
// 如果没有子元素
if (p.children.length === 0) {
return (p.innerHTML = decodeRegExp(p.innerHTML));
} else {
p.querySelectorAll("*").forEach((child) => {
if (child.children.length === 0) {
return (child.innerHTML = decodeRegExp(child.innerHTML));
}
});
}
});
// 遍历所有 a 标签
htmlBase.querySelectorAll("a").forEach((a) => {
const span = htmlBase.createElement("i");
span.classList.add("mdi", "mdi-link-variant", "anno-link-icon");
// 添加到 a 标签中
a.prepend(span);
if (a.href.startsWith("javascript:miHoYoGameJSSDK.openInBrowser")) {
a.href = a.href.replace("javascript:miHoYoGameJSSDK.openInBrowser('", "").replace("');", "");
a.target = "_blank";
} else if (a.href.startsWith("javascript:miHoYoGameJSSDK.openInWebview")) {
a.href = a.href.replace("javascript:miHoYoGameJSSDK.openInWebview('", "").replace("');", "");
a.target = "_blank";
}
});
return htmlBase.body.innerHTML;
}
/**
* @description 转义正则表达式
* @since Alpha v0.1.2
* @param {string} data 内容
* @returns {string} 转义后的内容
*/
export function decodeRegExp (data: string): string {
let res = data;
if (res.length === 0) return res;
res = res.replace(/&lt;/g, "<");
res = res.replace(/&gt;/g, ">");
res = res.replace(/&nbsp;/g, " ");
res = res.replace(/&#39;/g, "'");
res = res.replace(/&quot;/g, "\"");
res = res.replace(/&apos;/g, "'");
res = res.replace(/&amp;/g, "&");
return res;
}

View File

@@ -1,33 +0,0 @@
/**
* @file plugins Genshin utils announcements.ts
* @description 原神游戏内公告工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.1
*/
import { type AnnoListData, type AnnoListCard, type Announcement, type AnnoListItem } from "../interface/announcement";
/**
* @description 将获取到的数据转为渲染用的卡片
* @since Alpha v0.1.1
* @param {AnnoListData} data 公告数据
* @returns {AnnoListCard[]} 渲染用的卡片
*/
export function getAnnoCards (data: AnnoListData): AnnoListCard[] {
const cards: AnnoListCard[] = [];
data.list.map((annoList: Announcement) => {
return annoList.list.map((anno: AnnoListItem) => {
return cards.push({
id: anno.ann_id,
title: anno.title,
subtitle: anno.subtitle,
banner: anno.banner,
type_label: anno.type_label,
tag_icon: anno.tag_icon,
start_time: anno.start_time,
end_time: anno.end_time,
});
});
});
return cards;
}