up
This commit is contained in:
@@ -13,5 +13,5 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"lastUpdated": 1776136244169
|
"lastUpdated": 1776140518763
|
||||||
}
|
}
|
||||||
@@ -4,23 +4,37 @@ import { defineStore } from 'pinia'
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { usersApi } from '@/api/users';
|
import { usersApi } from '@/api/users';
|
||||||
|
|
||||||
export const useUsersStore = defineStore('users', () => {
|
|
||||||
const usersInfo = ref([]);
|
const usersInfo = ref([]);
|
||||||
|
// 正在请求中的 userID 集合,避免重复发请求
|
||||||
|
const pendingFetch = new Set();
|
||||||
|
|
||||||
|
export const useUsersStore = defineStore('users', () => {
|
||||||
|
|
||||||
function getUserFromUserID(userID) {
|
function getUserFromUserID(userID) {
|
||||||
return usersInfo.value?.find(item => item.UserID === userID) ?? null
|
return usersInfo.value?.find(item => item.UserID === userID) ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fetchUser(userID) {
|
||||||
|
if (pendingFetch.has(userID)) return
|
||||||
|
pendingFetch.add(userID)
|
||||||
|
usersApi.getUserInfoFromUserID(userID).then((r) => {
|
||||||
|
if (r.errCode == 0 && r.raw.err_code == 0 && r.raw.return?.userinfo) {
|
||||||
|
// 防止并发写入重复数据
|
||||||
|
if (!usersInfo.value.find(item => item.UserID === userID)) {
|
||||||
|
usersInfo.value.push(r.raw.return.userinfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).finally(() => {
|
||||||
|
pendingFetch.delete(userID)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function getUsernameFromUserID(userID) {
|
function getUsernameFromUserID(userID) {
|
||||||
const target = getUserFromUserID(userID)
|
const target = getUserFromUserID(userID)
|
||||||
if (target) {
|
if (target) {
|
||||||
return target.Username
|
return target.Username
|
||||||
}
|
}
|
||||||
usersApi.getUserInfoFromUserID(userID).then((r) => {
|
fetchUser(userID)
|
||||||
if (r.errCode == 0 && r.raw.err_code == 0 && r.raw.return.userinfo) {
|
|
||||||
usersInfo.value.push(r.raw.return.userinfo)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return "..."
|
return "..."
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,6 +43,8 @@ export const useUsersStore = defineStore('users', () => {
|
|||||||
if (target?.AvatarPath) {
|
if (target?.AvatarPath) {
|
||||||
return `/api/static/avatar/${target.AvatarPath}`
|
return `/api/static/avatar/${target.AvatarPath}`
|
||||||
}
|
}
|
||||||
|
// 触发加载(如果还没加载过)
|
||||||
|
fetchUser(userID)
|
||||||
return `/ava.svg`
|
return `/ava.svg`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user