♻️ 重构收藏数据库,更加beauty

This commit is contained in:
目棃
2024-03-20 02:25:51 +08:00
parent 66c6d3a3a5
commit 5d59245761
12 changed files with 609 additions and 187 deletions

View File

@@ -1,4 +1,5 @@
<template>
<!-- todo 编辑收藏合集的 overlay -->
<div class="collect-box" data-html2canvas-ignore>
<div class="collect-btn" @click="switchCollect()" :title="isCollected ? '取消收藏' : '收藏'">
<v-icon :color="isCollected ? 'yellow' : 'white'">
@@ -8,14 +9,17 @@
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import DataBase from "tauri-plugin-sql-api";
import { onBeforeMount, ref } from "vue";
import TGSqlite from "../../plugins/Sqlite";
import TSUserCollection from "../../plugins/Sqlite/modules/userCollect";
import showConfirm from "../func/confirm";
import showSnackbar from "../func/snackbar";
const isCollected = ref(false);
const collect = ref<Array<string>>([]);
const collect = ref<Array<TGApp.Sqlite.UserCollection.UFMap>>([]);
const db = ref<DataBase | undefined>(undefined);
interface TSetCollectProps {
modelValue: number;
@@ -24,35 +28,34 @@ interface TSetCollectProps {
const props = defineProps<TSetCollectProps>();
onMounted(async () => await getCollect());
async function getCollect(): Promise<void> {
const res = await TGSqlite.checkPostCollect(props.modelValue.toString());
if (res !== false) {
onBeforeMount(async () => {
db.value = await TGSqlite.getDB();
const check = await TSUserCollection.getCollectPost(db.value, props.modelValue.toString());
if (typeof check === "boolean") {
isCollected.value = check;
return;
}
isCollected.value = true;
try {
collect.value = JSON.parse(res);
console.warn(collect.value);
} catch (e) {
showSnackbar({
text: `收藏数据解析失败: ${res}`,
color: "error",
});
}
}
}
collect.value = check;
});
async function switchCollect(): Promise<void> {
if (isCollected.value === false) {
collect.value = ["default"];
if (props.data === undefined) {
if (db.value === undefined) {
showSnackbar({
text: "获取帖子数据失败",
text: "获取到数据库",
color: "error",
});
return;
}
await TGSqlite.collectPost(props.data, collect.value);
if (isCollected.value === false) {
if (props.data === undefined) {
showSnackbar({
text: "未获取到帖子信息",
color: "error",
});
return;
}
await TSUserCollection.addCollect(db.value, props.modelValue.toString(), props.data);
isCollected.value = true;
showSnackbar({
text: "收藏成功",
@@ -69,7 +72,7 @@ async function switchCollect(): Promise<void> {
return;
}
}
await TGSqlite.cancelCollect(props.modelValue.toString());
await TSUserCollection.deletePostCollect(db.value, props.modelValue.toString(), true);
isCollected.value = false;
showSnackbar({
text: "取消收藏成功",

View File

@@ -171,8 +171,8 @@ function checkCover(data: TGApp.Plugins.Mys.Gacha.Data[]): boolean {
const cover = homeStore.poolCover;
if (cover === undefined) return false;
let checkList = data.length;
Object.entries(cover).forEach(([key, value]) => {
const pool = data.find((item) => item.id.toString() === key);
Object.entries(cover).forEach(([key, value]: [string, unknown]) => {
const pool = data.find((item: TGApp.Plugins.Mys.Gacha.Data) => item.id.toString() === key);
if (pool && value !== "/source/UI/empty.webp") {
checkList--;
}
@@ -345,11 +345,11 @@ onUnmounted(() => {
.pool-character {
display: flex;
overflow: hidden auto;
width: auto;
max-width: 280px;
height: 60px;
margin: 10px;
overflow: hidden auto;
}
.pool-character::-webkit-scrollbar-thumb {

View File

@@ -45,7 +45,7 @@
</v-card>
</template>
<script lang="ts" setup>
import { computed, nextTick, onBeforeMount, onMounted, ref, toRaw, watch } from "vue";
import { onBeforeMount, ref } from "vue";
import { createPost } from "../../utils/TGWindow";
import TpAvatar from "../post/tp-avatar.vue";

View File

@@ -5,13 +5,17 @@
<v-select
v-model="curSelect"
class="pc-select"
:items="Array.from(selects)"
:items="collections.map((i) => i.title)"
:clearable="curSelect !== '未分类'"
variant="outlined"
label="合集"
/>
<v-btn rounded class="pc-btn" prepend-icon="mdi-refresh" @click="freshUser"
<v-btn rounded class="pc-btn" prepend-icon="mdi-refresh" @click="freshUser()"
>获取用户收藏</v-btn
>
<v-btn rounded class="pc-btn" prepend-icon="mdi-import" @click="freshOther"
>导入其他用户收藏</v-btn
>
<!-- todo 编辑收藏 -->
<v-pagination class="pc-page" v-model="page" :length="length" />
</div>
@@ -24,13 +28,15 @@
</template>
<script lang="ts" setup>
import { storeToRefs } from "pinia";
import { onMounted, ref, watch } from "vue";
import Database from "tauri-plugin-sql-api";
import { computed, onBeforeMount, onMounted, ref, watch } from "vue";
import showConfirm from "../../components/func/confirm";
import showSnackbar from "../../components/func/snackbar";
import TPostCard from "../../components/main/t-postcard.vue";
import ToLoading from "../../components/overlay/to-loading.vue";
import TGSqlite from "../../plugins/Sqlite";
import { insertPostCollectData } from "../../plugins/Sqlite/sql/insertData";
import TSUserCollection from "../../plugins/Sqlite/modules/userCollect";
import { useUserStore } from "../../store/modules/user";
import TGLogger from "../../utils/TGLogger";
import TGRequest from "../../web/request/TGRequest";
@@ -39,55 +45,71 @@ const loading = ref(false);
const loadingTitle = ref("加载中...");
const loadingSub = ref("");
const userStore = storeToRefs(useUserStore());
const db = ref<Database | undefined>(undefined);
const collections = ref<TGApp.Sqlite.UserCollection.SingleTable[]>([]);
const selected = ref<TGApp.Sqlite.UserCollection.SingleTable[]>([]);
const selects = ref<Set<string>>(new Set());
const curSelect = ref<string>("default");
const collections = ref<TGApp.Sqlite.UserCollection.UFCollection[]>([]);
const selected = ref<TGApp.Sqlite.UserCollection.UFPost[]>([]);
const curSelect = ref<string>("未分类");
const page = ref(1);
const length = ref(5);
const length = computed(() => Math.ceil(selected.value.length / 12));
onBeforeMount(async () => {
if (!(await TGSqlite.checkTableExist("UFPost"))) {
await TGSqlite.update();
showSnackbar({
text: "数据库已更新",
color: "success",
});
}
});
onMounted(async () => {
loadingTitle.value = "检测 UserCollection 表...";
loading.value = true;
const check = await TGSqlite.checkTableExist("UserCollection");
if (!check) {
loadingTitle.value = "创建 UserCollection 表...";
await createCollectTable();
}
loadingTitle.value = "获取收藏帖子...";
await getCollect();
filterBySelect();
loading.value = true;
db.value = await TGSqlite.getDB();
if (!db.value) {
showSnackbar({
text: "数据库未初始化",
color: "error",
});
loading.value = false;
return;
}
loadingTitle.value = "获取收藏合集...";
collections.value = await TSUserCollection.getCollectList(db.value);
loadingTitle.value = "获取未分类帖子...";
const postUnCollect = await TSUserCollection.getUnCollectPostList(db.value);
if (postUnCollect.length > 0) {
selected.value = postUnCollect;
} else {
selected.value = await TSUserCollection.getCollectPostList(
db.value,
collections.value[0].title,
);
}
loading.value = false;
});
async function createCollectTable(): Promise<void> {
const db = await TGSqlite.getDB();
const sql = `
create table if not exists UserCollection
(
postId text not null, -- 帖子ID
title text not null, -- 帖子标题
content text, -- 帖子内容
collect text, -- 合集标题
uid text, -- 用户ID
updated text not null, -- 收藏时间
primary key (postId)
);
`;
await db.execute(sql);
showSnackbar({
text: "创建 UserCollection 表成功",
color: "success",
});
}
// 根据合集筛选
function filterBySelect(): void {
selected.value = collections.value.filter((item) => item.collect.includes(curSelect.value));
length.value = Math.ceil(selected.value.length / 12);
async function freshPost(select: string | null): Promise<void> {
if (!db.value) {
showSnackbar({
text: `筛选合集 ${curSelect.value} 成功,共 ${selected.value.length} 条数据`,
text: "数据库未初始化",
color: "error",
});
return;
}
loadingTitle.value = `获取合集 ${select}...`;
loading.value = true;
if (select === null || select === "未分类") {
curSelect.value = "未分类";
selected.value = await TSUserCollection.getUnCollectPostList(db.value);
} else {
selected.value = await TSUserCollection.getCollectPostList(db.value, select);
}
loading.value = false;
showSnackbar({
text: `切换合集 ${select},共 ${selected.value.length} 条帖子`,
color: "success",
});
}
@@ -107,28 +129,34 @@ function getPageItems(): TGApp.Plugins.Mys.Post.FullData[] {
return card;
}
watch(curSelect, () => {
filterBySelect();
watch(curSelect, async () => {
await freshPost(curSelect.value);
});
async function getCollect(): Promise<void> {
const db = await TGSqlite.getDB();
const sql = "SELECT * FROM UserCollection";
collections.value = await db.select(sql);
for (const item of collections.value) {
try {
const parse: string[] = JSON.parse(item.collect);
for (const p of parse) {
selects.value.add(p);
}
} catch (e) {
await TGLogger.Error("[PostCollect] getCollect");
await TGLogger.Error(<string>e);
async function freshOther(): Promise<void> {
const input = await showConfirm({
mode: "input",
title: "导入其他用户收藏",
text: "请输入用户米游社UID",
});
if (typeof input === "string") {
if (isNaN(Number(input))) {
showSnackbar({
text: "UID 格式错误,请输入数字",
color: "error",
});
return;
}
await freshUser(input);
return;
}
showSnackbar({
text: "取消导入",
color: "cancel",
});
}
async function freshUser(): Promise<void> {
async function freshUser(uid?: string): Promise<void> {
if (!userStore.cookie.value) {
showSnackbar({
text: "请先登录",
@@ -142,25 +170,37 @@ async function freshUser(): Promise<void> {
};
loadingTitle.value = "获取用户收藏...";
loading.value = true;
let res = await TGRequest.User.byCookie.getCollect(cookie);
let res = await TGRequest.User.getCollect(cookie, uid || userStore.briefInfo.value.uid);
let is_last = false;
while (!is_last) {
if ("retcode" in res) {
if (res.retcode === 1001) {
showSnackbar({
text: "用户收藏已设为私密,无法获取",
color: "error",
});
} else {
showSnackbar({
text: `[${res.retcode}] ${res.message}`,
color: "error",
});
}
loading.value = false;
return;
}
let posts = res.list;
loadingTitle.value = `合并收藏帖子 [offset]${res.next_offset}...`;
await mergePosts(posts);
await mergePosts(posts, uid || userStore.briefInfo.value.uid);
if (res.is_last) {
is_last = true;
} else {
loadingTitle.value = "获取用户收藏...";
loadingSub.value = `[offset]${res.next_offset} [is_last]${res.is_last}`;
res = await TGRequest.User.byCookie.getCollect(cookie, res.next_offset);
res = await TGRequest.User.getCollect(
cookie,
uid || userStore.briefInfo.value.uid,
res.next_offset,
);
}
}
loading.value = false;
@@ -172,32 +212,19 @@ async function freshUser(): Promise<void> {
}
// 合并收藏帖子
async function mergePosts(posts: TGApp.Plugins.Mys.Post.FullData[]): Promise<void> {
const db = await TGSqlite.getDB();
const collectTitle = `${userStore.briefInfo.value.nickname} 的收藏`;
async function mergePosts(
posts: TGApp.Plugins.Mys.Post.FullData[],
collect: string,
): Promise<void> {
if (!db.value) return;
const title = `用户收藏-${collect}`;
for (const post of posts) {
loadingSub.value = `合并帖子 ${post.post.post_id} ${post.post.subject}`;
const postId = post.post.post_id;
const collect = await TGSqlite.checkPostCollect(postId.toString());
let collects = new Set<string>();
if (collect !== false) {
try {
const parse: string[] = JSON.parse(collect);
for (const item of parse) {
collects.add(item);
loadingTitle.value = `收藏帖子 [${post.post.post_id}]...`;
loadingSub.value = `[POST]${post.post.subject} [collection]${title}`;
const res = await TSUserCollection.addCollect(db.value, post.post.post_id, post, title, true);
if (!res) {
await TGLogger.Error(`[PostCollect] mergePosts [${post.post.post_id}]`);
}
} catch (e) {
collects.add("default");
showSnackbar({
text: `收藏数据解析失败: ${collect}`,
color: "error",
});
await new Promise((resolve) => setTimeout(resolve, 1000));
}
collects.add(collectTitle);
}
const sql = insertPostCollectData(post, Array.from(collects), userStore.briefInfo.value.uid);
await db.execute(sql);
}
}
</script>

View File

@@ -13,7 +13,6 @@ import {
insertAbyssData,
insertAppData,
insertGameAccountData,
insertPostCollectData,
insertRecordData,
insertRoleData,
} from "./sql/insertData";
@@ -501,59 +500,11 @@ class Sqlite {
const db = await this.getDB();
const sql = `SELECT name
FROM sqlite_master
WHERE type='table'
AND name='${table}';`;
WHERE type = 'table'
AND name = '${table}';`;
const res: Array<{ name: string }> = await db.select(sql);
return res.length > 0;
}
/**
* @description 检测帖子是否已收藏
* @since Beta v0.4.5
* @param {string} postId 帖子 id
* @returns {Promise<false|string>} 当该帖子被归到多个分类时,返回分类数量
*/
async checkPostCollect(postId: string): Promise<false | string> {
const db = await this.getDB();
const sql = `SELECT collect
FROM UserCollection
WHERE postId = '${postId}';`;
const res: Array<{ collect: string }> = await db.select(sql);
if (res.length === 0) return false;
return res[0].collect;
}
/**
* @description 收藏单个帖子
* @since Beta v0.4.5
* @param {TGApp.Plugins.Mys.Post.FullData} post 帖子
* @param {Array<string>} collect 分类
* @param {string} uid 用户 uid
* @returns {Promise<void>}
*/
async collectPost(
post: TGApp.Plugins.Mys.Post.FullData,
collect: string[],
uid?: string,
): Promise<void> {
const db = await this.getDB();
const sql = insertPostCollectData(post, collect, uid);
await db.execute(sql);
}
/**
* @description 取消收藏
* @since Beta v0.4.5
* @param {string} postId 帖子 id
* @returns {Promise<void>}
*/
async cancelCollect(postId: string): Promise<void> {
const db = await this.getDB();
const sql = `DELETE
FROM UserCollection
WHERE postId = '${postId}';`;
await db.execute(sql);
}
}
const TGSqlite = new Sqlite();

View File

@@ -0,0 +1,381 @@
/**
* @file plugins/Sqlite/modules/userCollect.ts
* @description 用户收藏模块
* @since Beta v0.4.5
*/
import type DataBase from "tauri-plugin-sql-api";
/**
* @description 获取单个帖子的收藏信息
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} postId 文章 id
* @return {Promise<TGApp.Sqlite.UserCollection.UFMap[]|boolean>} 返回收藏信息
*/
async function getCollectPost(
db: DataBase,
postId: string,
): Promise<TGApp.Sqlite.UserCollection.UFMap[] | boolean> {
const sql = "SELECT * FROM UFMap WHERE postId = ?";
const res: TGApp.Sqlite.UserCollection.UFMap[] = await db.select(sql, [postId]);
if (res.length > 0) {
return res;
}
const unclassifiedSql = "SELECT * FROM UFPost WHERE id = ?";
const unclassifiedRes: TGApp.Sqlite.UserCollection.UFPost[] = await db.select(unclassifiedSql, [
postId,
]);
return unclassifiedRes.length > 0;
}
/**
* @description 获取收藏合集列表
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @return {Promise<TGApp.Sqlite.UserCollection.UFCollection[]>} 返回收藏合集列表
*/
async function getCollectList(db: DataBase): Promise<TGApp.Sqlite.UserCollection.UFCollection[]> {
const sql = "SELECT * FROM UFCollection";
return await db.select(sql);
}
/**
* @description 获取收藏合集中的帖子列表
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} collection 收藏合集标题
* @return {Promise<TGApp.Sqlite.UserCollection.UFPost[]>} 返回收藏合集中的帖子列表
*/
async function getCollectPostList(
db: DataBase,
collection: string,
): Promise<TGApp.Sqlite.UserCollection.UFPost[]> {
const sql = "SELECT * FROM UFMap WHERE collection = ?";
const res: TGApp.Sqlite.UserCollection.UFMap[] = await db.select(sql, [collection]);
const postList: TGApp.Sqlite.UserCollection.UFPost[] = [];
for (let i = 0; i < res.length; i++) {
const postSql = "SELECT * FROM UFPost WHERE id = ?";
const postRes: TGApp.Sqlite.UserCollection.UFPost[] = await db.select(postSql, [res[i].postId]);
postList.push(postRes[0]);
}
return postList;
}
/**
* @description 获取未分类的收藏帖子列表
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @return {Promise<TGApp.Sqlite.UserCollection.UFPost[]>} 返回未分类的收藏帖子列表
*/
async function getUnCollectPostList(db: DataBase): Promise<TGApp.Sqlite.UserCollection.UFPost[]> {
const sql = "SELECT * FROM UFPost WHERE id NOT IN (SELECT postId FROM UFMap)";
return await db.select(sql);
}
/**
* @description 新建收藏合集
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} title 收藏合集标题
* @param {string} desc 收藏合集描述
* @return {Promise<boolean>} 返回收藏合集 id
*/
async function createCollect(db: DataBase, title: string, desc: string): Promise<boolean> {
if (title === "未分类" || title === "") return false;
const sql = "SELECT id FROM UFCollection WHERE title = ?";
const res: Array<{ id: number }> = await db.select(sql, [title]);
if (res.length > 0) {
return false;
}
const insertSql = "INSERT INTO UFCollection (title, desc,updated) VALUES (?, ?,?)";
await db.execute(insertSql, [title, desc, new Date().getTime()]);
return true;
}
/**
* @description 删除收藏合集
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} title 收藏合集标题
* @return {Promise<boolean>} 返回是否删除成功
*/
async function deleteCollect(db: DataBase, title: string): Promise<boolean> {
const sql = "SELECT id FROM UFCollection WHERE title = ?";
const res: Array<{ id: number }> = await db.select(sql, [title]);
if (res.length === 0) {
return false;
}
const deleteSql = "DELETE FROM UFCollection WHERE title = ?";
await db.execute(deleteSql, [title]);
const deleteRefSql = "DELETE FROM UFMap WHERE collectionId = ?";
await db.execute(deleteRefSql, [res[0].id]);
return true;
}
/**
* @description 更新收藏合集信息,标题/描述
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} title 收藏合集标题
* @param {string} newTitle 新标题
* @param {string} newDesc 新描述
* @return {Promise<boolean>} 返回是否更新成功
*/
async function updateCollect(
db: DataBase,
title: string,
newTitle: string,
newDesc: string,
): Promise<boolean> {
const sql = "SELECT id FROM UFCollection WHERE title = ?";
const res: Array<{ id: number }> = await db.select(sql, [title]);
if (res.length === 0) {
return false;
}
const updateSql = "UPDATE UFCollection SET title = ?, desc = ?, updated = ? WHERE id = ?";
await db.execute(updateSql, [newTitle, newDesc, new Date().getTime(), res[0].id]);
const updateRefSql = "UPDATE UFMap SET collection = ?,desc=?,updated=? WHERE collectionId = ?";
await db.execute(updateRefSql, [newTitle, newDesc, new Date().getTime(), res[0].id]);
return true;
}
/**
* @description 添加收藏
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} postId 文章 id
* @param {TGApp.Plugins.Mys.Post.FullData} post 文章信息
* @param {string} collection 收藏合集标题,可能为 undefined
* @param {boolean} recursive 是否递归添加
* @return {Promise<boolean>} 返回是否添加成功
*/
async function addCollect(
db: DataBase,
postId: string,
post: TGApp.Plugins.Mys.Post.FullData,
collection: string | undefined = undefined,
recursive: boolean = false,
): Promise<boolean> {
const sql = "SELECT id FROM UFPost WHERE id = ?";
const res: Array<{ id: number }> = await db.select(sql, [postId]);
if (res.length > 0) {
return false;
}
const insertSql = "INSERT INTO UFPost (id, title, content, updated) VALUES (?, ?, ?, ?)";
await db.execute(insertSql, [
postId,
post.post.subject,
JSON.stringify(post),
new Date().getTime(),
]);
if (collection !== undefined) {
const collectionSql = "SELECT * FROM UFCollection WHERE title = ?";
let collectionRes: TGApp.Sqlite.UserCollection.UFCollection[] = await db.select(collectionSql, [
collection,
]);
if (collectionRes.length === 0 || collectionRes.length > 1) {
if (!recursive) {
return false;
}
const createCollectionRes = await createCollect(db, collection, collection);
if (!createCollectionRes) {
return false;
}
collectionRes = await db.select(collectionSql, [collection]);
}
const insertMapSql =
"INSERT INTO UFMap (postId, collectionId,post, collection, desc, updated) VALUES (?, ?, ?, ?, ?, ?)";
await db.execute(insertMapSql, [
postId,
collectionRes[0].id,
post.post.subject,
collection,
collectionRes[0].desc,
new Date().getTime(),
]);
}
return true;
}
/**
* @description 删除合集中的收藏
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} postId 文章 id
* @param {string} collection 收藏合集标题
* @return {Promise<boolean>} 返回是否删除成功
*/
async function deleteCollectPost(
db: DataBase,
postId: string,
collection: string,
): Promise<boolean> {
const sql = "SELECT id FROM UFCollection WHERE title = ?";
const res: Array<{ id: number }> = await db.select(sql, [collection]);
if (res.length === 0) {
return false;
}
const deleteSql = "DELETE FROM UFMap WHERE postId = ? AND collectionId = ?";
await db.execute(deleteSql, [postId, res[0].id]);
return true;
}
/**
* @description 删除某个帖子的收藏,可选是否强制删除
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} postId 文章 id
* @param {boolean} force 是否强制删除
* @return {Promise<boolean>} 返回是否删除成功
*/
async function deletePostCollect(
db: DataBase,
postId: string,
force: boolean = false,
): Promise<boolean> {
const sql = "SELECT id FROM UFPost WHERE id = ?";
const res: Array<{ id: number }> = await db.select(sql, [postId]);
if (res.length === 0) {
return false;
}
const selectSql = "SELECT * FROM UFMap WHERE postId = ?";
const selectRes: TGApp.Sqlite.UserCollection.UFMap[] = await db.select(selectSql, [postId]);
if (selectRes.length === 0 && !force) {
return false;
}
const deleteSql = "DELETE FROM UFMap WHERE postId = ?";
await db.execute(deleteSql, [postId]);
if (force) {
const deletePostSql = "DELETE FROM UFPost WHERE id = ?";
await db.execute(deletePostSql, [postId]);
}
return true;
}
/**
* @description 修改单个帖子的收藏合集
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string} postId 文章 id
* @param {string[]} collections 收藏合集标题
* @return {Promise<boolean>} 返回是否修改成功
*/
async function updateCollectPost(
db: DataBase,
postId: string,
collections: string[],
): Promise<boolean> {
const sql = "SELECT id,title FROM UFPost WHERE id = ?";
const res: Array<{ id: number; title: string }> = await db.select(sql, [postId]);
if (res.length === 0) {
return false;
}
const deleteSql = "DELETE FROM UFMap WHERE postId = ?";
await db.execute(deleteSql, [postId]);
for (let i = 0; i < collections.length; i++) {
const collectionSql = "SELECT * FROM UFCollection WHERE title = ?";
const collectionRes: TGApp.Sqlite.UserCollection.UFCollection[] = await db.select(
collectionSql,
[collections[i]],
);
if (collectionRes.length === 0) {
return false;
}
const insertSql =
"INSERT INTO UFMap (postId, collectionId,post, collection, desc, updated) VALUES (?, ?, ?, ?, ?, ?)";
await db.execute(insertSql, [
postId,
collectionRes[0].id,
res[0].title,
collections[i],
collectionRes[0].desc,
new Date().getTime(),
]);
}
return true;
}
/**
* @description 批量修改帖子的收藏合集
* @since Beta v0.4.5
* @param {DataBase} db 数据库
* @param {string[]} postIds 文章 id
* @param {string} collection 收藏合集标题
* @param {string} oldCollection 旧的收藏合集标题
* @return {Promise<boolean>} 返回是否修改成功
*/
async function updateCollectPosts(
db: DataBase,
postIds: string[],
collection: string,
oldCollection: string | undefined,
): Promise<boolean> {
const collectionSql = "SELECT * FROM UFCollection WHERE title = ?";
const collectionRes: TGApp.Sqlite.UserCollection.UFCollection[] = await db.select(collectionSql, [
collection,
]);
if (collectionRes.length === 0) {
return false;
}
let oldCollectionInfo: TGApp.Sqlite.UserCollection.UFCollection | undefined;
if (oldCollection !== undefined) {
const oldCollectionRes: TGApp.Sqlite.UserCollection.UFCollection[] = await db.select(
collectionSql,
[oldCollection],
);
if (oldCollectionRes.length === 0) {
return false;
}
oldCollectionInfo = oldCollectionRes[0];
}
for (let i = 0; i < postIds.length; i++) {
const postSql = "SELECT id,title FROM UFPost WHERE id = ?";
const postRes: Array<{ id: number; title: string }> = await db.select(postSql, [postIds[i]]);
if (postRes.length === 0) {
return false;
}
if (oldCollectionInfo !== undefined) {
const updateSql =
"UPDATE UFMap SET collectionId = ?,post=?,collection=?,desc=?,updated=? WHERE postId = ? AND collectionId = ?";
await db.execute(updateSql, [
collectionRes[0].id,
postRes[0].title,
collection,
collectionRes[0].desc,
new Date().getTime(),
postIds[i],
oldCollectionInfo.id,
]);
continue;
}
const insertSql =
"INSERT INTO UFMap (postId, collectionId,post, collection, desc, updated) VALUES (?, ?, ?, ?, ?, ?)";
await db.execute(insertSql, [
postIds[i],
collectionRes[0].id,
postRes[0].title,
collection,
collectionRes[0].desc,
new Date().getTime(),
]);
}
return true;
}
const TSUserCollection = {
getCollectPost,
getCollectList,
getCollectPostList,
getUnCollectPostList,
createCollect,
deleteCollect,
updateCollect,
addCollect,
deleteCollectPost,
deletePostCollect,
updateCollectPost,
updateCollectPosts,
};
export default TSUserCollection;

View File

@@ -1,7 +1,6 @@
-- @file plugins Sqlite sql createTable.sql
-- @brief sqlite数据库创建表语句
-- @author BTMuli <bt-muli@outlook.com>
-- @since Alpha v0.2.3
-- @since Beta v0.4.5
-- @brief 创建成就数据表
create table if not exists Achievements
@@ -151,3 +150,35 @@ create table if not exists GachaRecords
count text,
updated text
);
-- @brief 创建用户帖子收藏
create table if not exists UFPost
(
id text not null, -- 帖子ID
title text not null, -- 帖子标题
content text, -- 帖子内容
updated text not null, -- 收藏时间
primary key (id)
);
-- @brief 创建用户帖子合集
create table if not exists UFCollection
(
id integer primary key autoincrement,-- 自增合集ID
title text not null, -- 合集标题
desc text, -- 合集描述
updated text not null -- 创建时间
);
-- @brief 创建用户帖子收藏-合集对照表
create table if not exists UFMap
(
postId text not null, -- 帖子ID
collectionId integer not null, -- 合集ID
post text not null, -- 帖子标题
collection text not null, -- 合集标题
desc text, -- 合集描述
updated text not null, -- 收藏时间
primary key (postId, collectionId)
);

View File

@@ -12,30 +12,58 @@
*/
declare namespace TGApp.Sqlite.UserCollection {
/**
* @description 数据库-用户收藏表
* @description 数据库-用户收藏帖子
* @since Beta v0.4.5
* @interface SingleTable
* @property {string} postId - 帖子 ID
* @interface UFPost
* @property {string} id - 帖子 ID
* @property {string} title - 标题
* @description 反序列化后是 TGApp.Plugins.Mys.Post.FullData
* @property {string} content - 内容
* @property {string} collect - 合集
* @property {string} uid - 用户 UID
* @property {string} updated - 更新时间
* @return SingleTable
* @return UFPost
*/
interface SingleTable {
postId: string;
interface UFPost {
id: string;
title: string;
content: string;
collect: string;
uid: string;
updated: string;
}
/**
* @description 渲染卡片
* @description 数据库-用户收藏合集表
* @since Beta v0.4.5
* @interface RenderCard
* @interface UFCollection
* @property {string} id - 合集 ID
* @property {string} title - 标题
* @property {string} desc - 描述
* @property {string} updated - 更新时间
* @return UFCollection
*/
type RenderCard = TGApp.Plugins.Mys.Forum.RenderCard;
interface UFCollection {
id: string;
title: string;
desc: string;
updated: string;
}
/**
* @description 数据库-用户收藏帖子合集关联表
* @since Beta v0.4.5
* @interface UFMap
* @property {string} postId - 帖子 ID
* @property {string} collectionId - 合集 ID
* @property {string} post - 帖子标题
* @property {string} collection - 合集标题
* @property {string} desc - 合集描述
* @property {string} updated - 更新时间
* @return UFMap
*/
interface UFMap {
postId: string;
collectionId: string;
post: string;
collection: string;
desc: string;
updated: string;
}
}

View File

@@ -1,3 +1,4 @@
<!-- todo 做成 overlay -->
<template>
<TSwitchTheme />
<ToLoading v-model="loading" :empty="loadingEmpty" :title="loadingTitle" />

View File

@@ -1,6 +1,6 @@
<template>
<TSwitchTheme />
<TSetCollect v-if="collectExist" :model-value="postId" :data="postData" />
<TSetCollect :model-value="postId" :data="postData" />
<TShareBtn
v-show="!loadingEmpty"
v-model="postRef"
@@ -122,7 +122,6 @@ const shareTime = ref<number>(Math.floor(Date.now() / 1000));
const shareTimeTimer = ref<any>();
// 合集
const showCollection = ref<boolean>(false);
const collectExist = ref<boolean>(false);
onMounted(async () => {
await appWindow.show();
@@ -164,7 +163,6 @@ onMounted(async () => {
await TGLogger.Info(`[t-post][${postId}][onMounted] 打开 JSON 窗口`);
createPostJson(postId);
}
collectExist.value = await TGSqlite.checkTableExist("UserCollection");
await nextTick(() => {
shareTimeTimer.value = setInterval(() => {
shareTime.value = Math.floor(Date.now() / 1000);

View File

@@ -35,6 +35,7 @@ const TGRequest = {
User: {
getAuthkey: genAuthkey,
getAuthkey2: genAuthkey2,
getCollect: getUserCollect,
getGachaLog,
getRecord: getGameRecord,
byLoginTicket: {
@@ -44,7 +45,6 @@ const TGRequest = {
getAbyss,
getAccounts: getGameAccountsByCookie,
getUserInfo: getUserInfoByCookie,
getCollect: getUserCollect,
},
byLToken: {
verify: verifyLToken,

View File

@@ -12,15 +12,17 @@ import TGUtils from "../utils/TGUtils";
* @description 获取用户收藏帖子
* @since Beta v0.4.5
* @param {Record<string, string>} cookie - 用户 cookie
* @param {string} uid - 用户 uid
* @param {string} offset - 偏移量
* @returns {Promise<TGApp.BBS.Collection.PostRespData|TGApp.BBS.Response.Base>} 用户收藏帖子
*/
export async function getUserCollect(
cookie: Record<string, string>,
uid: string,
offset: string = "",
): Promise<TGApp.BBS.Collection.PostRespData | TGApp.BBS.Response.Base> {
const url = "https://bbs-api.miyoushe.com/post/wapi/userFavouritePost";
const params = { size: "20", offset };
const params = { size: "20", uid, offset };
const header = TGUtils.User.getHeader(cookie, "GET", params, "common");
return await http
.fetch<TGApp.BBS.Collection.PostResponse | TGApp.BBS.Response.Base>(url, {