1
1
package net.lz1998.mirai.controller
2
2
3
+ import dto.HttpDto
3
4
import kotlinx.coroutines.GlobalScope
4
5
import kotlinx.coroutines.launch
5
- import kotlinx.coroutines.runBlocking
6
6
import net.lz1998.mirai.service.BotService
7
- import net.lz1998.mirai.service.LoginDataType
8
- import net.lz1998.mirai.service.myLoginSolver
9
7
import org.springframework.beans.factory.annotation.Autowired
10
- import org.springframework.http.MediaType
11
- import org.springframework.web.bind.annotation.RequestMapping
12
- import org.springframework.web.bind.annotation.RestController
8
+ import org.springframework.web.bind.annotation.*
13
9
14
10
@RestController
11
+ @RequestMapping(" /bot" )
15
12
class BotController {
16
13
17
14
@Autowired
18
15
lateinit var botService: BotService
19
16
20
17
21
18
// 创建一个机器人并登陆
22
- @RequestMapping(" /createBot" )
23
- fun createBot (botId : Long , password : String ): String {
24
- GlobalScope .launch { // TODO 是否可以优化? suspend报错怎么解决?
25
- val bot = botService.botMap[botId]
26
- if (bot != null ) { // 机器人已存在,直接登陆
27
- bot.bot.login()
28
- return @launch
29
- } else { // 机器人不存在,创建
30
- botService.createBot(botId, password)
31
- }
32
- }
33
- return " ok"
34
- }
35
-
36
- // 获取机器人状态
37
- @RequestMapping(" /getStatus" )
38
- fun getStatus (botId : Long ): String {
39
- // 机器人不存在
40
- val bot = botService.botMap[botId] ? : return " NOT_CREATED"
41
-
42
- // 机器人在线
43
- if (bot.bot.isOnline) {
44
- return " ONLINE"
45
- }
46
-
47
- // 机器人需要登陆
48
- val loginData = myLoginSolver.getLoginData(botId)
49
- if (loginData != null ) {
50
- return loginData.type.name // 登陆类型
51
- }
52
-
53
- // 其他状态
54
- return " UNKNOWN"
19
+ @RequestMapping(" /create/v1" , produces = [" application/x-protobuf" ], consumes = [" application/x-protobuf" ])
20
+ fun createBot (@RequestBody param : HttpDto .CreateBotReq ): HttpDto .CreateBotResp {
21
+ botService.createBot(param.botId, param.password)
22
+ return HttpDto .CreateBotResp .newBuilder().build()
55
23
}
56
24
57
- // 通过轮询获取登陆验证url
58
- @RequestMapping(" /getLoginUrl" )
59
- fun getLoginUrl (botId : Long ): String? {
60
- val loginData = myLoginSolver.getLoginData(botId) ? : return null
61
- return if (loginData.type != LoginDataType .PIC_CAPTCHA ) {
62
- loginData.url
63
- } else {
64
- null
65
- }
25
+ @RequestMapping(" /list/v1" , produces = [" application/x-protobuf" ], consumes = [" application/x-protobuf" ])
26
+ fun listBot (@RequestBody param : HttpDto .ListBotReq ): HttpDto .ListBotResp {
27
+ val botList = botService.listBot()
28
+ return HttpDto .ListBotResp .newBuilder().addAllBotList(botList).build()
66
29
}
67
30
68
- // 通过轮询获取登陆图片验证码
69
- @RequestMapping(" /getLoginImage" , produces = [MediaType .IMAGE_JPEG_VALUE ])
70
- fun getLoginData (botId : Long ): ByteArray? {
71
- val loginData = myLoginSolver.getLoginData(botId) ? : return null
72
- return if (loginData.type == LoginDataType .PIC_CAPTCHA ) {
73
- loginData.data
74
- } else {
75
- null
31
+ @RequestMapping(" /login/v1" , produces = [" application/x-protobuf" ], consumes = [" application/x-protobuf" ])
32
+ fun botLoginAsync (@RequestBody param : HttpDto .BotLoginAsyncReq ): HttpDto .BotLoginAsyncResp {
33
+ GlobalScope .launch {
34
+ botService.botLogin(param.botId)
76
35
}
36
+ return HttpDto .BotLoginAsyncResp .newBuilder().build()
77
37
}
78
38
79
- // 处理登陆
80
- @RequestMapping(" /solveLogin" )
81
- fun solveLogin (botId : Long , result : String ): String {
82
- myLoginSolver.solveLogin(botId, result)
83
- return " ok"
84
- }
85
39
}
0 commit comments