获取皮肤增加缓存
This commit is contained in:
parent
221a035507
commit
6c2080f53a
@ -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() {
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
<Route path="/profile" element={<Profile />} />
|
||||
<Route path="/textures" element={<Textures />} />
|
||||
|
||||
</Route>
|
||||
</Routes>
|
||||
|
@ -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 (
|
||||
<ListItem disablePadding>
|
||||
@ -270,6 +268,6 @@ const MyListItem = memo(function MyListItem(p: ListItem) {
|
||||
</ListItemButton>
|
||||
</ListItem>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export default Layout
|
@ -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() {
|
||||
<CardHeader title="信息" />
|
||||
<CardContent sx={{ display: "grid", gridTemplateColumns: "4em auto" }}>
|
||||
<Typography>uid</Typography>
|
||||
<Typography sx={{ wordBreak: 'break-all' }}>{userinfo.loading ? <Skeleton /> : userinfo.data?.uid}</Typography>
|
||||
<Typography sx={{ wordBreak: 'break-all' }}>{(userinfo.loading && !userinfo.data) ? <Skeleton /> : userinfo.data?.uid}</Typography>
|
||||
<Typography>name</Typography>
|
||||
<Typography>{SkinInfo.loading || userinfo.loading ? <Skeleton /> : SkinInfo.data?.name}</Typography>
|
||||
<Typography>{(SkinInfo.loading || userinfo.loading) && !SkinInfo.data ? <Skeleton /> : SkinInfo.data?.name}</Typography>
|
||||
<Typography>uuid</Typography>
|
||||
<Typography sx={{ wordBreak: 'break-all' }}>{userinfo.loading ? <Skeleton /> : userinfo.data?.uuid}</Typography>
|
||||
<Typography sx={{ wordBreak: 'break-all' }}>{(userinfo.loading && !userinfo.data) ? <Skeleton /> : userinfo.data?.uuid}</Typography>
|
||||
</CardContent>
|
||||
{/* <CardActions>
|
||||
<Button size="small">更改</Button>
|
||||
@ -80,7 +80,7 @@ const Profile = memo(function Profile() {
|
||||
<CardHeader title="皮肤" />
|
||||
<CardContent sx={{ display: "flex", justifyContent: 'center' }}>
|
||||
{
|
||||
SkinInfo.loading ? <Skeleton variant="rectangular" width={250} height={250} />
|
||||
(SkinInfo.loading && !SkinInfo.data) ? <Skeleton variant="rectangular" width={250} height={250} />
|
||||
: (textures.skin != "" || textures.cape != "") && (
|
||||
<MySkin
|
||||
skinUrl={textures.skin}
|
||||
|
@ -0,0 +1,9 @@
|
||||
import { memo } from "react";
|
||||
|
||||
const Textures = memo(function Textures() {
|
||||
return (<>
|
||||
<p>修改皮肤</p>
|
||||
</>)
|
||||
})
|
||||
|
||||
export default Textures
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user