diff --git a/apps/web/src/pages/album.vue b/apps/web/src/pages/album.vue new file mode 100644 index 0000000..7defe8c --- /dev/null +++ b/apps/web/src/pages/album.vue @@ -0,0 +1,115 @@ + + + + + diff --git a/packages/core/src/stores/post.ts b/packages/core/src/stores/post.ts index 04f79ab..b5ebe16 100644 --- a/packages/core/src/stores/post.ts +++ b/packages/core/src/stores/post.ts @@ -1,6 +1,6 @@ import { defineStore } from 'pinia' import { useRoute, useRouter } from 'vue-router' -import type { Post, UID, UserBio, UserInfo } from '@shared' +import type { Album, Post, UID, UserBio, UserInfo } from '@shared' import { EmptyIDB, IDB } from '../utils/storage' export const usePostStore = defineStore('post', () => { @@ -213,6 +213,35 @@ export const usePostStore = defineStore('post', () => { .then(data => data.sort((a, b) => a.name.localeCompare(b.name))) } + /** + * 获取所有图片,以月份分组 + */ + async function getAllImgs() { + await waitIDB() + const imgs = await idb.value.getImgs() + + /** + * [ + * { + * date: '2021年01月', + * imgs: ['url1', 'url2', ...] + * } + * ] + */ + const result: Album[] = [] + + for (const { img, date, id } of imgs) { + const year = date.getFullYear() + const month = date.getMonth() + 1 + const key = `${year}年${month.toString().padStart(2, '0')}月` + + const data = { url: img, id, date: key } + result.push(data) + } + + return result + } + return { total, totalDB, @@ -245,5 +274,6 @@ export const usePostStore = defineStore('post', () => { searchPost, searchAndTime, getFollowings, + getAllImgs, } }) diff --git a/packages/core/src/utils/storage.ts b/packages/core/src/utils/storage.ts index efb7ef4..cce6535 100644 --- a/packages/core/src/utils/storage.ts +++ b/packages/core/src/utils/storage.ts @@ -3,7 +3,12 @@ import Fuse from 'fuse.js' import dayjs from 'dayjs' import type { FuseResult } from 'fuse.js' import type { DBSchema, IDBPDatabase } from 'idb' -import type { Post, UID, UserBio, UserInfo } from '@shared' +import type { + Post, + UID, + UserBio, + UserInfo, +} from '@shared' const POST_STORE = 'posts' const USER_STORE = 'user' @@ -52,6 +57,7 @@ export async function checkDB(uid: number) { export class IDB { idb: Promise> name: UID + posts: Post[] = [] constructor( name: UID, @@ -140,8 +146,13 @@ export class IDB { * 获取所有帖子 */ async getAllDBPosts() { + if (this.posts.length) + return this.posts + const db = await this.idb - return await db.getAll(POST_STORE) + const posts = await db.getAll(POST_STORE) + this.posts = posts + return posts } /** @@ -364,6 +375,31 @@ export class IDB { const db = await this.idb return await db.getAll(FLOWERINGS_STORE) } + + async getImgs() { + const result: Album[] = [] + const posts = await this.getAllDBPosts() + + posts + .reverse() + .forEach((post) => { + post.imgs.forEach((img) => { + result.push({ + img, + date: new Date(post.created_at), + id: post.mblogid, + }) + }) + }) + + return result + } +} + +interface Album { + img: string + id: string + date: Date } export type SeachResult = FuseResult<{ @@ -399,4 +435,5 @@ export class EmptyIDB extends IDB { async setUserInfo(_user: UserInfo): Promise {} async addFollowings(_followings: UserBio[]): Promise {} async getFollowings(): Promise { return [] } + async getImgs(): Promise { return [] } } diff --git a/packages/shared/src/types/post.ts b/packages/shared/src/types/post.ts index 784cada..4a8e735 100644 --- a/packages/shared/src/types/post.ts +++ b/packages/shared/src/types/post.ts @@ -85,3 +85,9 @@ export interface PostData { user: UserInfo followings: UserBio[] } + +export interface Album { + url: string + id: string + date: string +}