UID=0的默认头像URL改为由前端计算

This commit is contained in:
John Smith 2024-04-08 23:45:12 +08:00
parent 690f6aa35c
commit 6cfe332e78
3 changed files with 21 additions and 2 deletions

View File

@ -434,7 +434,7 @@ class RoomInfoHandler(api.base.ApiHandler):
class AvatarHandler(api.base.ApiHandler): class AvatarHandler(api.base.ApiHandler):
async def get(self): async def get(self):
# uid基本是0了现在这个接口唯一的作用是算用户名MD5其实可以放到前端 # uid基本是0了现在都是前端计算默认头像URL这个接口留着只是为了兼容旧版
uid = int(self.get_query_argument('uid')) uid = int(self.get_query_argument('uid'))
username = self.get_query_argument('username', '') username = self.get_query_argument('username', '')
avatar_url = await services.avatar.get_avatar_url_or_none(uid) avatar_url = await services.avatar.get_avatar_url_or_none(uid)

View File

@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"axios": "^1.4.0", "axios": "^1.4.0",
"core-js": "^3.8.3", "core-js": "^3.8.3",
"crypto-js": "^4.2.0",
"downloadjs": "^1.4.7", "downloadjs": "^1.4.7",
"element-ui": "^2.15.13", "element-ui": "^2.15.13",
"lodash": "^4.17.21", "lodash": "^4.17.21",

View File

@ -1,4 +1,5 @@
import axios from 'axios' import axios from 'axios'
import MD5 from 'crypto-js/md5'
export function getDefaultMsgHandler() { export function getDefaultMsgHandler() {
let dummyFunc = () => {} let dummyFunc = () => {}
@ -27,6 +28,10 @@ export function processAvatarUrl(avatarUrl) {
} }
export async function getAvatarUrl(uid, username) { export async function getAvatarUrl(uid, username) {
if (uid === 0) {
return getDefaultAvatarUrl(uid, username)
}
let res let res
try { try {
res = (await axios.get('/api/avatar_url', { params: { res = (await axios.get('/api/avatar_url', { params: {
@ -34,11 +39,24 @@ export async function getAvatarUrl(uid, username) {
username: username username: username
} })).data } })).data
} catch { } catch {
return DEFAULT_AVATAR_URL return getDefaultAvatarUrl(uid, username)
} }
return res.avatarUrl return res.avatarUrl
} }
export function getDefaultAvatarUrl(uid, username) {
let strToHash
if (uid !== 0) {
strToHash = uid.toString()
} else if (username !== '') {
strToHash = username
} else {
return DEFAULT_AVATAR_URL
}
let idHash = MD5(strToHash).toString()
return `//cravatar.cn/avatar/${idHash}?s=256&d=robohash&f=y`
}
export async function getTextEmoticons() { export async function getTextEmoticons() {
let res let res
try { try {