diff --git a/controller/api/yggdrasil.js b/controller/api/yggdrasil.js index 06daf5c..50b0bc1 100644 --- a/controller/api/yggdrasil.js +++ b/controller/api/yggdrasil.js @@ -26,6 +26,51 @@ module.exports = { } ctx.body = data; }, + api: { + profiles: { + minecraft: async (ctx, next) => { + const data = ctx.request.body; + + // 校验是否传入了数组 + if (!data instanceof Array) { + ctx.set("Content-Type", "application/json"); + ctx.body = []; + return; + } + + // 去除重复及无效数据 + data.filter(function (item, index, arr) { + return arr.indexOf(item, 0) === index && typeof item == "string"; + }); + + // 一次最多查询3个玩家的数据 + if (data.length >= 3) { + ctx.set("Content-Type", "application/json"); + ctx.body = []; + return; + } + + let playerList = []; + + // 遍历玩家列表 + for (let i = 0; i < data.length; i++) { + + // 搜索玩家信息 + let userData = await suser.searchUserInfoByPlayerName(data[i]).then(result => { return result; }); + + // 如果存在则生成玩家Profile,并加入到playerList + // 无需提供详细角色属性,第二个参数设置为false + if (userData) { + playerList.push(suser.genUserProfile(userData, false)); + } + } + + // 返回数据 + ctx.set("Content-Type", "application/json"); + ctx.body = playerList; + } + } + }, authserver: { authenticate: async (ctx, next) => { const data = ctx.request.body; diff --git a/routers/api.js b/routers/api.js index ba5ea76..52926f8 100644 --- a/routers/api.js +++ b/routers/api.js @@ -91,6 +91,10 @@ router.get('/yggdrasil/sessionserver/session/minecraft/hasJoined', yggdrasilCont // Yggdrasil角色查询接口 router.get('/yggdrasil/sessionserver/session/minecraft/profile/:uuid', yggdrasilController.sessionserver.session.minecraft.profile); +// POST ./api/yggdrasil/api/profiles/minecraft +// Yggdrasil按名称批量查询角色接口 +router.post('/yggdrasil/api/profiles/minecraft', yggdrasilController.api.profiles.minecraft); + /* ---------- ROUTES END ---------- */ module.exports = router; \ No newline at end of file diff --git a/service/user.js b/service/user.js index ecd5a85..5c6f72d 100644 --- a/service/user.js +++ b/service/user.js @@ -222,6 +222,17 @@ module.exports = { }); }) }, + searchUserInfoByPlayerName: (playername) => { + return new Promise((resolve, reject) => { + User.findOne({ 'playername': playername }, '', { lean: true }, function (err, user) { + if (!err && user) { + resolve(user); + } else { + resolve(false); + } + }); + }) + }, letUserLoggedIn: (ctx, email) => { return new Promise((resolve, reject) => { User.findOne({ 'email': email }, 'email playername password uuid tokens skin isBanned ip time', function (err, user) { @@ -256,32 +267,43 @@ module.exports = { }); }) }, - genUserProfile: (userData) => { - let textureData = { - timestamp: Date.now(), - profileId: userData.uuid.replace(/-/g, ""), - profileName: userData.playername, - textures: { - SKIN: { - url: `${config.common.url}/textures/${userData.skin.hash}`, - metadata: { - model: userData.skin.type == 0 ? "default" : "slim" + genUserProfile: (userData, isPropertiesContained = true) => { + if (isPropertiesContained) { + let textureData = { + timestamp: Date.now(), + profileId: userData.uuid.replace(/-/g, ""), + profileName: userData.playername, + textures: { + SKIN: { + url: `${config.common.url}/textures/${userData.skin.hash}`, + metadata: { + model: userData.skin.type == 0 ? "default" : "slim" + } } } } + textureData = Buffer.from(JSON.stringify(textureData)).toString("base64"); } - textureData = Buffer.from(JSON.stringify(textureData)).toString("base64"); - let data = { - id: userData.uuid.replace(/-/g, ""), - name: userData.playername, - properties: [ - { - "name": "textures", - "value": textureData, - "signature": utils.genSignedData(textureData) - } - ] + let data; + if (isPropertiesContained) { + data = { + id: userData.uuid.replace(/-/g, ""), + name: userData.playername, + properties: [ + { + "name": "textures", + "value": textureData, + "signature": utils.genSignedData(textureData) + } + ] + } + } else { + data = { + id: userData.uuid.replace(/-/g, ""), + name: userData.playername + } } + return data; }, genUserList: (filter, currectPage, pageSize) => {