获取用户头像&昵称

This commit is contained in:
BTMuli
2023-05-21 16:05:23 +08:00
parent 64b4b4abcc
commit 821014c0a2
14 changed files with 215 additions and 85 deletions

View File

@@ -1,47 +0,0 @@
/**
* @file store modules hk4e.ts
* @description store modules hk4e.ts
* @author BTMuli<outlook.com>
* @since Alpha v0.1.2
*/
// pinia
import { defineStore } from "pinia";
export const useHk4eStore = defineStore(
"hk4e",
() => {
// getCookie
function getCookie (): string {
return localStorage.getItem("hk4eCookie") || "{}";
}
// 获取具体的 cookie
function getCookieItem (key: string): string {
const cookie = getCookie();
const cookieParsed = JSON.parse(cookie);
return cookieParsed.value[key];
}
// setCookie
function setCookie (value: string): void {
// 解析 cookie
const cookieObj: Record<string, string> = {};
value.split(";").forEach((item) => {
const [key, value] = item.split("=");
cookieObj[key.trim()] = value;
});
// 保存 cookie
localStorage.setItem("hk4eCookie", JSON.stringify(cookieObj));
}
return {
getCookie,
setCookie,
getCookieItem,
};
},
{
persist: true,
},
);

53
src/store/modules/user.ts Normal file
View File

@@ -0,0 +1,53 @@
/**
* @file store modules user.ts
* @description User store module
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
// vue
import { ref } from "vue";
// pinia
import { defineStore } from "pinia";
// utils
import TGSqlite from "../../utils/TGSqlite";
export const useUserStore = defineStore(
"user", () => {
const briefInfo = ref({
nickname: "",
avatar: "",
uid: "",
desc: "",
} as BTMuli.User.Base.BriefInfo);
const cookie = ref({} as Record<string, string>);
function setBriefInfo (info: BTMuli.User.Base.BriefInfo): void {
briefInfo.value = info;
}
function getBriefInfo (): Record<string, string> {
return briefInfo.value;
}
function getCookieItem (key: string): string {
return cookie.value[key] || "";
}
async function initCookie (): Promise<void> {
const ck = await TGSqlite.getCookie();
if (cookie.value !== ck) {
cookie.value = ck;
}
}
return {
getBriefInfo,
setBriefInfo,
getCookieItem,
initCookie,
};
},
{
persist: true,
},
);