对未安装axios依赖的情况增加兼容

This commit is contained in:
Kokomi 2023-08-14 04:15:44 +08:00
parent 6637fb5951
commit 070d54cd71
2 changed files with 54 additions and 27 deletions

View File

@ -18,6 +18,7 @@
}, },
"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,6 +1,19 @@
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 axios from 'axios'
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 () {
@ -15,46 +28,47 @@ export class takeBirthdayPhoto extends plugin {
/** 命令正则匹配 */ /** 命令正则匹配 */
reg: '#?留影叙佳期$', reg: '#?留影叙佳期$',
/** 执行方法 */ /** 执行方法 */
fnc: 'birthdaystar', fnc: 'birthdaystar'
} }
] ]
}) })
} }
async birthdaystar (e) { async birthdaystar (e) {
const { user_id } = e; const { user_id } = e
const userInfo = await this.getCookie(user_id) const userInfo = await this.getCookie(user_id)
if (!userInfo) { if (!userInfo) {
e.reply('请先绑定ck再使用本功能哦~', true); e.reply('请先绑定ck再使用本功能哦~', true)
return true; return true
} }
const e_hk4e_token = await this.getEHK4EToken(userInfo.ck, userInfo.uid) const e_hk4e_token = await this.getEHK4EToken(userInfo.ck, userInfo.uid)
if (!e_hk4e_token) { if (!e_hk4e_token) {
e.reply('获取e-hk4e_token失败请刷新ck后再试~', true); e.reply('获取e-hk4e_token失败请刷新ck后再试~', true)
return true; return true
} }
const birthday_star_list = await this.getBirthdayStar(userInfo.uid, e_hk4e_token, userInfo.ck); const birthday_star_list = await this.getBirthdayStar(userInfo.uid, e_hk4e_token, userInfo.ck)
if (!birthday_star_list) { if (!birthday_star_list) {
e.reply('获取生日角色失败,请稍后再试~', true); e.reply('获取生日角色失败,请稍后再试~', true)
return true; return true
} }
if (birthday_star_list.length === 0) { if (birthday_star_list.length === 0) {
e.reply('今天没有生日角色哦~', true); e.reply('今天没有生日角色哦~', true)
return true; return true
} }
for (let role of birthday_star_list) { for (let 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) let 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
} else { } else {
await e.reply(`获取${role.name}的图片成功~`, true); await e.reply(`获取${role.name}的图片成功~`, true)
} }
} }
@ -67,7 +81,7 @@ export class takeBirthdayPhoto extends plugin {
} }
async getEHK4EToken (ck, uid) { async getEHK4EToken (ck, uid) {
const isCN = uid.toString().match(/^[125]/) ? true : false; const isCN = uid.toString().match(/^[125]/) ? true : false
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)
@ -81,8 +95,12 @@ export class takeBirthdayPhoto extends plugin {
uid: Number(uid), uid: Number(uid),
game_biz: game_biz, game_biz: game_biz,
lang: 'zh-cn', lang: 'zh-cn',
region: region, region: region
}) })
let axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.post(url, body, { headers: header }) let res = await axios.post(url, body, { headers: header })
if (res.data.retcode != 0) { if (res.data.retcode != 0) {
return false return false
@ -113,12 +131,16 @@ 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}` let cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid) let badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false; let isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global' let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = { let header = {
'Cookie': cookie, 'Cookie': cookie
} }
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` 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`
let axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.get(url, { headers: header }) let res = await axios.get(url, { headers: header })
return res.data.data.role return res.data.data.role
} }
@ -126,12 +148,16 @@ export class takeBirthdayPhoto extends plugin {
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}` let cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid) let badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false; let isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global' let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = { let header = {
'Cookie': cookie, 'Cookie': cookie
} }
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 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 axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.post(url, { role_id: Number(role_id) }, { headers: header }) let res = await axios.post(url, { role_id: Number(role_id) }, { headers: header })
if (res.data.retcode != 0) { if (res.data.retcode != 0) {
return res.data.message return res.data.message