refactor: 重构日志记录方式并优化部分功能
All checks were successful
Sakura-Miki-build / Explore-Gitea-Actions (push) Successful in 1m30s

- 移除各文件中的 Logger静态实例
- 在 util包下新增 LogUtil.kt 文件,提供统一的日志记录扩展属性
- 优化部分服务类中的逻辑结构
- 调整数据库查询方式,使用 KtQueryWrapper 替代 LambdaQueryWrapper
This commit is contained in:
Grand-cocoa 2025-03-20 18:12:07 +08:00
parent ceaebd100f
commit 4bc526d48c
7 changed files with 43 additions and 45 deletions

View File

@ -1,12 +1,11 @@
package info.alinadace.sakuramiki.configuration package info.alinadace.sakuramiki.configuration
import info.alinadace.sakuramiki.service.Service import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.util.log
import io.github.kloping.qqbot.Starter import io.github.kloping.qqbot.Starter
import io.github.kloping.qqbot.api.event.Event import io.github.kloping.qqbot.api.event.Event
import io.github.kloping.qqbot.impl.ListenerHost import io.github.kloping.qqbot.impl.ListenerHost
import jakarta.annotation.Resource import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.event.EventListener import org.springframework.context.event.EventListener
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -25,8 +24,6 @@ class Initialization {
@Resource @Resource
lateinit var starter: Starter lateinit var starter: Starter
val log: Logger = LoggerFactory.getLogger(this::class.java)
@EventListener(ApplicationReadyEvent::class) @EventListener(ApplicationReadyEvent::class)
suspend fun initFunction() { suspend fun initFunction() {
val eventListener = object : ListenerHost() { val eventListener = object : ListenerHost() {

View File

@ -2,13 +2,12 @@ package info.alinadace.sakuramiki.service
import info.alinadace.sakuramiki.annotation.BotFunction import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.configuration.BotConfig import info.alinadace.sakuramiki.configuration.BotConfig
import info.alinadace.sakuramiki.util.log
import io.github.kloping.qqbot.api.v2.FriendMessageEvent import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.api.v2.GroupMessageEvent import io.github.kloping.qqbot.api.v2.GroupMessageEvent
import io.github.kloping.qqbot.api.v2.MessageV2Event import io.github.kloping.qqbot.api.v2.MessageV2Event
import io.github.kloping.qqbot.entities.ex.PlainText import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/** /**
* @author Kane * @author Kane
@ -17,9 +16,7 @@ import org.slf4j.LoggerFactory
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class) @BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
class ExampleService : Service<MessageV2Event> { class ExampleService : Service<MessageV2Event> {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource @Resource
lateinit var botConfig: BotConfig lateinit var botConfig: BotConfig

View File

@ -6,12 +6,11 @@ import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.service.Service import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.mfa.domain.MFA import info.alinadace.sakuramiki.service.mfa.domain.MFA
import info.alinadace.sakuramiki.service.mfa.mapper.MFAMapper import info.alinadace.sakuramiki.service.mfa.mapper.MFAMapper
import info.alinadace.sakuramiki.util.log
import io.github.kloping.qqbot.api.v2.FriendMessageEvent import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.entities.ex.Image import io.github.kloping.qqbot.entities.ex.Image
import io.github.kloping.qqbot.entities.ex.PlainText import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
/** /**
@ -22,10 +21,6 @@ import java.io.ByteArrayInputStream
@BotFunction(FriendMessageEvent::class) @BotFunction(FriendMessageEvent::class)
class MFABindService : Service<FriendMessageEvent> { class MFABindService : Service<FriendMessageEvent> {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource @Resource
private lateinit var mapper: MFAMapper private lateinit var mapper: MFAMapper
@ -41,7 +36,7 @@ class MFABindService : Service<FriendMessageEvent> {
if (message[0] is PlainText && message[0].toString().startsWith("#mfa")){ if (message[0] is PlainText && message[0].toString().startsWith("#mfa")){
if (message.size == 1){ if (message.size == 1){
val split = message[0].toString().split(" ") val split = message[0].toString().split(" ")
if (split.size != 4) { if (split.size < 4) {
return false return false
} }
if (split[1] != "bind"){ if (split[1] != "bind"){
@ -64,8 +59,14 @@ class MFABindService : Service<FriendMessageEvent> {
if (message[0] is PlainText && message[0].toString().startsWith("#mfa")){ if (message[0] is PlainText && message[0].toString().startsWith("#mfa")){
if (message.size == 1){ if (message.size == 1){
val split = message[0].toString().split(" ") val split = message[0].toString().split(" ")
val mfa = MFA(event.friend.id, split[2], split[3]) var arg = ""
for (argIndex in 3..<split.size){
arg += split[argIndex]
}
arg.trim()
val mfa = MFA(event.friend.id, split[2], arg)
mapper.insert(mfa) mapper.insert(mfa)
log.info("${event.friend.id}绑定成功 - secret: ${mfa.secret}")
event.send("绑定成功") event.send("绑定成功")
return return
} }

View File

@ -1,6 +1,6 @@
package info.alinadace.sakuramiki.service.mfa package info.alinadace.sakuramiki.service.mfa
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import info.alinadace.sakuramiki.annotation.BotFunction import info.alinadace.sakuramiki.annotation.BotFunction
import info.alinadace.sakuramiki.service.Service import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.mfa.domain.MFA import info.alinadace.sakuramiki.service.mfa.domain.MFA
@ -9,8 +9,6 @@ import info.alinadace.sakuramiki.util.MFAUtil
import io.github.kloping.qqbot.api.v2.FriendMessageEvent import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.entities.ex.PlainText import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/** /**
* MFA生成服务 * MFA生成服务
@ -20,9 +18,7 @@ import org.slf4j.LoggerFactory
@BotFunction(FriendMessageEvent::class) @BotFunction(FriendMessageEvent::class)
class MFAGenerateService : Service<FriendMessageEvent> { class MFAGenerateService : Service<FriendMessageEvent> {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource @Resource
private lateinit var mapper: MFAMapper private lateinit var mapper: MFAMapper
@ -50,9 +46,10 @@ class MFAGenerateService : Service<FriendMessageEvent> {
override fun active(event: FriendMessageEvent) { override fun active(event: FriendMessageEvent) {
val message = event.message val message = event.message
if (message[0] is PlainText && message[0].toString().startsWith("mfa")){ if (message[0] is PlainText && message[0].toString().startsWith("mfa")){
if (message.size == 1){ val command = message[0].toString().split(" ")
if (command.size == 1){
val mfa = mapper.selectOne( val mfa = mapper.selectOne(
LambdaQueryWrapper<MFA>() KtQueryWrapper(MFA::class.java)
.eq(MFA::uid, event.friend.id) .eq(MFA::uid, event.friend.id)
.eq(MFA::name, "default") .eq(MFA::name, "default")
) )
@ -63,16 +60,14 @@ class MFAGenerateService : Service<FriendMessageEvent> {
event.send(MFAUtil.generate(mfa.secret, 30)) event.send(MFAUtil.generate(mfa.secret, 30))
return return
} }
if (message.size == 2){ if (command.size == 2){
val split = message[0].toString().split(" ")
if (split.size == 2){
val mfa = mapper.selectOne( val mfa = mapper.selectOne(
LambdaQueryWrapper<MFA>() KtQueryWrapper(MFA::class.java)
.eq(MFA::uid, event.friend.id) .eq(MFA::uid, event.friend.id)
.eq(MFA::name, split[1]) .eq(MFA::name, command[1])
) )
if (mfa == null){ if (mfa == null){
event.send("您还没有绑定${split[1]} MFA使用 #mfa bind ${split[1]} <secret> 绑定${split[1]} MFA") event.send("您还没有绑定${command[1]} MFA使用 #mfa bind ${command[1]} <secret> 绑定${command[1]} MFA")
return return
} }
event.send(MFAUtil.generate(mfa.secret, 30)) event.send(MFAUtil.generate(mfa.secret, 30))
@ -80,5 +75,4 @@ class MFAGenerateService : Service<FriendMessageEvent> {
} }
} }
} }
}
} }

View File

@ -6,14 +6,13 @@ import info.alinadace.sakuramiki.service.Service
import info.alinadace.sakuramiki.service.randomphoto.domain.FileList import info.alinadace.sakuramiki.service.randomphoto.domain.FileList
import info.alinadace.sakuramiki.service.randomphoto.domain.SingleFile import info.alinadace.sakuramiki.service.randomphoto.domain.SingleFile
import info.alinadace.sakuramiki.util.ImageUtil import info.alinadace.sakuramiki.util.ImageUtil
import info.alinadace.sakuramiki.util.log
import io.github.kloping.qqbot.api.v2.FriendMessageEvent import io.github.kloping.qqbot.api.v2.FriendMessageEvent
import io.github.kloping.qqbot.api.v2.GroupMessageEvent import io.github.kloping.qqbot.api.v2.GroupMessageEvent
import io.github.kloping.qqbot.api.v2.MessageV2Event import io.github.kloping.qqbot.api.v2.MessageV2Event
import io.github.kloping.qqbot.entities.ex.Image import io.github.kloping.qqbot.entities.ex.Image
import io.github.kloping.qqbot.entities.ex.PlainText import io.github.kloping.qqbot.entities.ex.PlainText
import jakarta.annotation.Resource import jakarta.annotation.Resource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
/** /**
@ -23,10 +22,6 @@ import java.io.ByteArrayInputStream
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class) @BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
class RandomPhotoService : Service<MessageV2Event> { class RandomPhotoService : Service<MessageV2Event> {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource @Resource
lateinit var alistService: AlistService lateinit var alistService: AlistService

View File

@ -59,7 +59,7 @@ class ImageUtil {
fun download(url: String): ByteArray? { fun download(url: String): ByteArray? {
val request = Request.Builder().get().url(url).build() val request = Request.Builder().get().url(url).build()
return okHttpClient.newCall(request).execute().body?.bytes(); return okHttpClient.newCall(request).execute().body?.bytes()
} }
} }
} }

View File

@ -0,0 +1,14 @@
package info.alinadace.sakuramiki.util
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* Log 扩展
* @author Kane
* @since 2025/3/20 17:50
*/
inline val <reified T : Any> T.log: Logger
get() = LoggerFactory.getLogger(T::class.java)