统一使用 node-fetch,删除 axios (#224)

This commit is contained in:
时雨◎星空 2023-08-14 13:09:35 +08:00 committed by GitHub
parent cd818ed212
commit 233d027b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 85 deletions

View File

@ -18,7 +18,6 @@
}, },
"dependencies": { "dependencies": {
"art-template": "^4.13.2", "art-template": "^4.13.2",
"axios": "^1.4.0",
"chalk": "^5.2.0", "chalk": "^5.2.0",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"https-proxy-agent": "5.0.1", "https-proxy-agent": "5.0.1",

View File

@ -1,19 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js' import plugin from '../../../lib/plugins/plugin.js'
import gsCfg from '../model/gsCfg.js' import gsCfg from '../model/gsCfg.js'
// import axios from 'axios' import fetch from 'node-fetch'
let axios = null
let getAxios = async function () {
if (axios) {
return axios
}
try {
axios = (await import('axios')).default
} catch (e) {
console.log('未安装依赖axios...')
return false
}
return axios
}
export class takeBirthdayPhoto extends plugin { export class takeBirthdayPhoto extends plugin {
constructor () { constructor () {
@ -60,10 +47,10 @@ export class takeBirthdayPhoto extends plugin {
return true return true
} }
for (let role of birthday_star_list) { for (const role of birthday_star_list) {
await e.reply(`正在获取${role.name}的图片,请稍等~`, true) await e.reply(`正在获取${role.name}的图片,请稍等~`, true)
await e.reply(segment.image(role.take_picture)) await e.reply(segment.image(role.take_picture))
let message = await this.getBirthdayStarImg(userInfo.uid, e_hk4e_token, userInfo.ck, role.role_id) const message = await this.getBirthdayStarImg(userInfo.uid, e_hk4e_token, userInfo.ck, role.role_id)
if (message != 'success') { if (message != 'success') {
await e.reply(message) await e.reply(message)
return true return true
@ -85,7 +72,7 @@ export class takeBirthdayPhoto extends plugin {
const url = isCN ? 'https://api-takumi.mihoyo.com/common/badge/v1/login/account' : 'https://api-os-takumi.mihoyo.com/common/badge/v1/login/account' const url = isCN ? 'https://api-takumi.mihoyo.com/common/badge/v1/login/account' : 'https://api-os-takumi.mihoyo.com/common/badge/v1/login/account'
const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global' const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
const region = await this.getServer(uid) const region = await this.getServer(uid)
const header = { const headers = {
'Cookie': ck, 'Cookie': ck,
'Content-Type': 'application/json;charset=UTF-8', 'Content-Type': 'application/json;charset=UTF-8',
'Referer': 'https://webstatic.mihoyo.com/', 'Referer': 'https://webstatic.mihoyo.com/',
@ -97,15 +84,12 @@ export class takeBirthdayPhoto extends plugin {
lang: 'zh-cn', lang: 'zh-cn',
region: region region: region
}) })
let axios = await getAxios() let res = await fetch(url, { method: "POST", body, headers })
if (!axios) { const e_hk4e_token = res.headers.get('set-cookie').match(/e_hk4e_token=(.*?);/)[1]
res = await res.json()
if (res.retcode != 0) {
return false return false
} }
let res = await axios.post(url, body, { headers: header })
if (res.data.retcode != 0) {
return false
}
let e_hk4e_token = res.headers['set-cookie'].toString().match(/e_hk4e_token=(.*?);/)[1]
return e_hk4e_token return e_hk4e_token
} }
@ -129,38 +113,29 @@ export class takeBirthdayPhoto extends plugin {
} }
async getBirthdayStar (uid, e_hk4e_token, ck) { async getBirthdayStar (uid, e_hk4e_token, ck) {
let cookie = `e_hk4e_token=${e_hk4e_token};${ck}` const cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid) const badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false const isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global' const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = { const headers = { 'Cookie': cookie }
'Cookie': cookie const url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/index?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
} let res = await fetch(url, { headers })
let url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/index?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521` res = await res.json()
let axios = await getAxios() return res.data.role
if (!axios) {
return false
}
let res = await axios.get(url, { headers: header })
return res.data.data.role
} }
async getBirthdayStarImg (uid, e_hk4e_token, ck, role_id) { async getBirthdayStarImg (uid, e_hk4e_token, ck, role_id) {
let cookie = `e_hk4e_token=${e_hk4e_token};${ck}` const cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid) const badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false const isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global' const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = { const headers = { 'Cookie': cookie }
'Cookie': cookie const url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/post_my_draw?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
} const body = JSON.stringify({ role_id: Number(role_id) })
let url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/post_my_draw?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521` let res = await fetch(url, { method: "POST", body, headers })
let axios = await getAxios() res = await res.json()
if (!axios) { if (res.retcode != 0) {
return false return res.message
}
let res = await axios.post(url, { role_id: Number(role_id) }, { headers: header })
if (res.data.retcode != 0) {
return res.data.message
} else { } else {
return 'success' return 'success'
} }