From 6c2080f53a4e058bdbfd963d01fd6e34766bb367 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Tue, 3 Oct 2023 01:29:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=9A=AE=E8=82=A4=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/Route.tsx | 2 + frontend/src/views/Layout.tsx | 10 ++-- frontend/src/views/profile/Profile.tsx | 10 ++-- frontend/src/views/profile/Textures.tsx | 9 +++ service/yggdrasil/texture.go | 8 +++ service/yggdrasil/user.go | 80 +++++++++++++++++-------- 6 files changed, 82 insertions(+), 37 deletions(-) diff --git a/frontend/src/Route.tsx b/frontend/src/Route.tsx index 81f03f7..ac3246a 100644 --- a/frontend/src/Route.tsx +++ b/frontend/src/Route.tsx @@ -3,6 +3,7 @@ import { ScrollRestoration } from "react-router-dom"; import Login from '@/views/Login' import Register from '@/views/Register' import Profile from '@/views/profile/Profile' +import Textures from '@/views/profile/Textures' import Layout from '@/views/Layout' const router = createBrowserRouter([ @@ -17,6 +18,7 @@ function Root() { } /> } /> } /> + } /> diff --git a/frontend/src/views/Layout.tsx b/frontend/src/views/Layout.tsx index a6bff43..79a02c6 100644 --- a/frontend/src/views/Layout.tsx +++ b/frontend/src/views/Layout.tsx @@ -65,7 +65,6 @@ const Layout = memo(function Layout() { const userinfo = useRequest(() => userInfo(nowToken), { refreshDeps: [nowToken], cacheKey: "/api/v1/user", - cacheTime: 10000, onError: e => { if (e instanceof ApiErr && e.code == 5) { navigate("/login") @@ -167,7 +166,6 @@ const MyToolbar = memo(function MyToolbar(p: { setOpen: (v: boolean) => void }) const server = useRequest(serverInfo, { cacheKey: "/api/yggdrasil", - cacheTime: 100000, onError: e => { console.warn(e) setErr(String(e)) @@ -253,12 +251,12 @@ const MyList = memo(function MyList(p: { list: ListItem[] }) { ) }) -const MyListItem = memo(function MyListItem(p: ListItem) { +const MyListItem = function MyListItem(p: ListItem) { const navigate = useNavigate(); - const handleClick = useMemoizedFn(() => { + const handleClick = () => { navigate(p.link) - }) + } return ( @@ -270,6 +268,6 @@ const MyListItem = memo(function MyListItem(p: ListItem) { ) -}) +} export default Layout \ No newline at end of file diff --git a/frontend/src/views/profile/Profile.tsx b/frontend/src/views/profile/Profile.tsx index 8bd6e36..07ca8cf 100644 --- a/frontend/src/views/profile/Profile.tsx +++ b/frontend/src/views/profile/Profile.tsx @@ -28,7 +28,6 @@ const Profile = memo(function Profile() { const userinfo = useRequest(() => userInfo(nowToken), { refreshDeps: [nowToken], cacheKey: "/api/v1/user", - cacheTime: 10000, onError: e => { if (e instanceof ApiErr && e.code == 5) { navigate("/login") @@ -39,6 +38,7 @@ const Profile = memo(function Profile() { }) const SkinInfo = useRequest(() => yggProfile(userinfo.data?.uuid ?? ""), { + cacheKey: "/api/yggdrasil/sessionserver/session/minecraft/profile/" + userinfo.data?.uuid, onError: e => { console.warn(e) setErr(String(e)) @@ -66,11 +66,11 @@ const Profile = memo(function Profile() { uid - {userinfo.loading ? : userinfo.data?.uid} + {(userinfo.loading && !userinfo.data) ? : userinfo.data?.uid} name - {SkinInfo.loading || userinfo.loading ? : SkinInfo.data?.name} + {(SkinInfo.loading || userinfo.loading) && !SkinInfo.data ? : SkinInfo.data?.name} uuid - {userinfo.loading ? : userinfo.data?.uuid} + {(userinfo.loading && !userinfo.data) ? : userinfo.data?.uuid} {/* @@ -80,7 +80,7 @@ const Profile = memo(function Profile() { { - SkinInfo.loading ? + (SkinInfo.loading && !SkinInfo.data) ? : (textures.skin != "" || textures.cape != "") && ( +

修改皮肤

+ ) +}) + +export default Textures \ No newline at end of file diff --git a/service/yggdrasil/texture.go b/service/yggdrasil/texture.go index 356c5b3..3e2d42e 100644 --- a/service/yggdrasil/texture.go +++ b/service/yggdrasil/texture.go @@ -94,6 +94,10 @@ func (y *Yggdrasil) DelTexture(ctx context.Context, uuid string, token string, t if err != nil { return fmt.Errorf("DelTexture: %w", err) } + err = y.cache.Del([]byte("Profile" + uuid)) + if err != nil { + return fmt.Errorf("DelTexture: %w", err) + } return nil } @@ -152,6 +156,10 @@ func (y *Yggdrasil) PutTexture(ctx context.Context, token string, texturebyte [] if err != nil { return fmt.Errorf("PutTexture: %w", err) } + err = y.cache.Del([]byte("Profile" + uuid)) + if err != nil { + return fmt.Errorf("PutTexture: %w", err) + } return nil } diff --git a/service/yggdrasil/user.go b/service/yggdrasil/user.go index 36a1ae0..9a8a659 100644 --- a/service/yggdrasil/user.go +++ b/service/yggdrasil/user.go @@ -17,6 +17,7 @@ import ( "github.com/google/uuid" "github.com/samber/lo" + "github.com/xmdhs/authlib-skin/db/cache" "github.com/xmdhs/authlib-skin/db/ent" "github.com/xmdhs/authlib-skin/db/ent/texture" "github.com/xmdhs/authlib-skin/db/ent/user" @@ -187,14 +188,6 @@ func (y *Yggdrasil) Refresh(ctx context.Context, token yggdrasil.RefreshToken) ( } func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool, host string) (yggdrasil.UserInfo, error) { - up, err := y.client.UserProfile.Query().Where(userprofile.UUID(uuid)).WithUsertexture().Only(ctx) - if err != nil { - var nf *ent.NotFoundError - if errors.As(err, &nf) { - return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", ErrNotUser) - } - return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) - } baseURl := func() string { if y.config.TextureBaseUrl == "" { u := &url.URL{} @@ -206,27 +199,62 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool, return y.config.TextureBaseUrl }() - ut := yggdrasil.UserTextures{ - ProfileID: up.UUID, - ProfileName: up.Name, - Textures: map[string]yggdrasil.Textures{}, - Timestamp: time.Now().UnixMilli(), + c := cache.CacheHelp[yggdrasil.UserTextures]{Cache: y.cache} + key := []byte("Profile" + uuid) + ut, err := c.Get(key) + if err != nil { + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) } + if ut.ProfileName != "" { + for k, v := range ut.Textures { + u, err := url.Parse(v.Url) + if err != nil { + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) + } + baseu, err := url.Parse(baseURl) + if err != nil { + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) + } + u.Host = baseu.Host + v.Url = u.String() + ut.Textures[k] = v + } + } else { + up, err := y.client.UserProfile.Query().Where(userprofile.UUID(uuid)).WithUsertexture().Only(ctx) + if err != nil { + var nf *ent.NotFoundError + if errors.As(err, &nf) { + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", ErrNotUser) + } + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) + } - for _, v := range up.Edges.Usertexture { - dt, err := y.client.Texture.Query().Where(texture.ID(v.TextureID)).Only(ctx) + ut = yggdrasil.UserTextures{ + ProfileID: up.UUID, + ProfileName: up.Name, + Textures: map[string]yggdrasil.Textures{}, + Timestamp: time.Now().UnixMilli(), + } + + for _, v := range up.Edges.Usertexture { + dt, err := y.client.Texture.Query().Where(texture.ID(v.TextureID)).Only(ctx) + if err != nil { + return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) + } + hashstr := dt.TextureHash + t := yggdrasil.Textures{ + Url: lo.Must1(url.JoinPath(baseURl, hashstr[:2], hashstr[2:4], hashstr)), + Metadata: map[string]string{}, + } + if v.Variant == "slim" { + t.Metadata["model"] = "slim" + } + ut.Textures[strings.ToTitle(v.Type)] = t + } + err = c.Put(key, ut, time.Now().Add(30*time.Minute)) if err != nil { return yggdrasil.UserInfo{}, fmt.Errorf("GetProfile: %w", err) } - hashstr := dt.TextureHash - t := yggdrasil.Textures{ - Url: lo.Must1(url.JoinPath(baseURl, hashstr[:2], hashstr[2:4], hashstr)), - Metadata: map[string]string{}, - } - if v.Variant == "slim" { - t.Metadata["model"] = "slim" - } - ut.Textures[strings.ToTitle(v.Type)] = t } texturesBase64 := ut.Base64() @@ -253,8 +281,8 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool, } uinfo := yggdrasil.UserInfo{ - ID: up.UUID, - Name: up.Name, + ID: ut.ProfileID, + Name: ut.ProfileName, Properties: pl, }