miao-plugin/components/profile-data/enka.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

import lodash from 'lodash'
2022-08-18 10:13:42 +00:00
import { Data } from '../index.js'
import { ProfileServ } from '../../models/index.js'
import EnkaData from './enka-data.js'
let HttpsProxyAgent = ''
2022-08-18 10:13:42 +00:00
export default new ProfileServ({
id: 'enka',
cfgKey: 'enkaApi',
// 处理请求参数
async request (api) {
let params = {
headers: { 'User-Agent': this.getCfg('userAgent') }
}
2022-08-18 10:13:42 +00:00
let proxy = this.getCfg('proxyAgent')
if (proxy) {
if (HttpsProxyAgent === '') {
HttpsProxyAgent = await import('https-proxy-agent').catch((err) => {
logger.error(err)
})
HttpsProxyAgent = HttpsProxyAgent ? HttpsProxyAgent.default : undefined
}
if (HttpsProxyAgent) {
params.agent = new HttpsProxyAgent(proxy)
}
}
2022-08-18 10:13:42 +00:00
return { api, params }
},
// 处理服务返回
async response (data, req) {
2022-06-14 22:07:24 +00:00
if (!data.playerInfo) {
if (data.error) {
console.log(`Enka ReqErr: ${data.error}`)
}
2022-08-22 13:38:34 +00:00
return req.err('error', 60)
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
let details = data.avatarInfoList
2022-06-14 22:07:24 +00:00
if (!details || details.length === 0 || !details[0].propMap) {
return req.err('empty', 5 * 60)
2022-06-14 22:07:24 +00:00
}
return data
},
userData (data) {
return Data.getData(data, 'name:nickname,avatar:profilePicture.avatarId,level,signature')
},
profileData (data) {
let ret = {}
lodash.forEach(data.avatarInfoList, (ds) => {
let profile = EnkaData.getProfile(ds)
if (profile && profile.id) {
ret[profile.id] = profile
}
})
return ret
},
// 获取冷却时间
cdTime (data) {
return data.ttl || 60
2022-06-14 22:07:24 +00:00
}
2022-08-18 10:13:42 +00:00
})