Skip to content

Commit

Permalink
feat: 新增登录相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed Aug 18, 2024
1 parent 04f92dc commit bb4ea3a
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 62 deletions.
32 changes: 18 additions & 14 deletions src/ntqq/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@ const dispatcher = useNTDispatcher()
const log = useLogger('NTStore')
const wrapperUtil = new NodeQQNTWrapperUtil()
const globalAdapter = new NodeIGlobalAdapter({
onGetOfflineMsg(...args) {
log.info('GlobalAdapter/onGetOfflineMsg', ...args)
dispatcher.emit('GlobalAdapter/onGetOfflineMsg', ...args)
onLog(...args) {
log.info('GlobalAdapter/onLog', ...args)
dispatcher.emit('GlobalAdapter/onLog', ...args)
},
onGetSrvCalTime(...args) {
log.info('GlobalAdapter/onGetSrvCalTime', ...args)
dispatcher.emit('GlobalAdapter/onGetSrvCalTime', ...args)
},
async onInstallFinished(...args) {
log.info('GlobalAdapter/onInstallFinished', ...args)
dispatcher.emit('GlobalAdapter/onInstallFinished', ...args)
},
onLog(...args) {
log.info('GlobalAdapter/onLog', ...args)
dispatcher.emit('GlobalAdapter/onLog', ...args)
},
onShowErrUITips(...args) {
log.info('GlobalAdapter/onShowErrUITips', ...args)
dispatcher.emit('GlobalAdapter/onShowErrUITips', ...args)
},
async fixPicImgType(...args) {
log.info('GlobalAdapter/fixPicImgType', ...args)
dispatcher.emit('GlobalAdapter/fixPicImgType', ...args)
},
getAppSetting(...args) {
log.info('GlobalAdapter/getAppSetting', ...args)
dispatcher.emit('GlobalAdapter/getAppSetting', ...args)
},
async onInstallFinished(...args) {
log.info('GlobalAdapter/onInstallFinished', ...args)
dispatcher.emit('GlobalAdapter/onInstallFinished', ...args)
},
onUpdateGeneralFlag(...args) {
log.info('GlobalAdapter/onUpdateGeneralFlag', ...args)
dispatcher.emit('GlobalAdapter/onUpdateGeneralFlag', ...args)
},
async fixPicImgType(...args) {
log.info('GlobalAdapter/fixPicImgType', ...args)
dispatcher.emit('GlobalAdapter/fixPicImgType', ...args)
onGetOfflineMsg(...args) {
log.info('GlobalAdapter/onGetOfflineMsg', ...args)
dispatcher.emit('GlobalAdapter/onGetOfflineMsg', ...args)
},
})
const wrapperEngine = new NodeIQQNTWrapperEngine()
Expand Down
1 change: 1 addition & 0 deletions src/ntqq/core/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const prepareBaseEnvironment = async() => {
density: 2
}
}
log.info('cfg:', cfg)
const globalAdapter = getGlobalAdapter()
const wrapperEngine = getWrapperEngine()
const initResult = wrapperEngine.initWithDeskTopConfig(cfg, globalAdapter)
Expand Down
2 changes: 1 addition & 1 deletion src/ntqq/core/service/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const initLogin = () => {
const listener = new NodeIKernelLoginListener(p)
const { registerEventListener } = useStore()
log.info('add handle after login success.')
registerEventListener('KernelLoginListener/onQRCodeLoginSucceed', 'once', (info) => {
registerEventListener('KernelLoginListener/onQRCodeLoginSucceed', 'always', (info) => {
const { userInfo } = useNTUserStore()
userInfo.uid = info.uid
userInfo.uin = info.uin
Expand Down
16 changes: 16 additions & 0 deletions src/ntqq/login/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,20 @@ export const NTLoginByAccountInfo = (ntLogin: NTLogin.AccountLoginRequest): Prom
}

})
}

export const NTGetQuickLoginList = async () => {
const { getLoginService } = useNTCore()
await sleep(2000)
const loginService = getLoginService()
const loginList = await loginService.getLoginList()
return loginList.LocalLoginInfoList
}

export const NTQuickLoginByUin = async (uin: `${number}`) => {
const { getLoginService } = useNTCore()
await sleep(2000)
const loginService = getLoginService()
const loginResult = await loginService.quickLoginWithUin(uin)
return loginResult
}
2 changes: 1 addition & 1 deletion src/ntqq/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const getAppInfo = (): StoreAppInfo => {
}
}
const getAppId = () => {
return platform === 'win32' ? '537207183' : '537207207'
return platform === 'win32' ? '537237802' : '537207207'
}
const getNTConfigStoreFolder = (): string => {
const wrapperUtil = new NodeQQNTWrapperUtil()
Expand Down
32 changes: 31 additions & 1 deletion src/onebot/actions/bot/bot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useLogger } from "../../../common/log"
import { NTGetQuickLoginList, NTQuickLoginByUin } from "../../../ntqq/login/account"
import { useNTUserStore } from "../../../ntqq/store/user"
import { CustomError } from "../../../server/error/custom-error"
import { useStore } from "../../../store/store"
import { loginByAccount, loginByQrCode } from "../../../transfer/login/login"
import { getUserInfoByUid } from "../../common/user"
import { UserInfoReq, UserInfoResp } from "../friend/interfaces"
import { BotActionParams } from "../interfaces"
import { QuickLoginItem } from "./types"

const log = useLogger('Bot')
const getBotInfo = async (p: {}): Promise<UserInfoResp> => {
Expand All @@ -24,14 +27,41 @@ const getBotInfo = async (p: {}): Promise<UserInfoResp> => {
log.info('getUserDetailInfo:', ret)
resp.user_uid = ret.uid
resp.user_uin = parseInt(ret.uin)
resp.nick = ret.nick
resp.nick = ret.simpleInfo.coreInfo.nick
resp.avatar_url = `http://q1.qlogo.cn/g?b=qq&nk=${resp.user_uin}&s=640`
return resp
}
const getQuickLoginList = async (p: {}) => {
const list = await NTGetQuickLoginList()
return list.map<QuickLoginItem>(e => ({
uin: e.uin,
uid: e.uid,
nick_name: e.nickName,
face_path: e.facePath,
face_url: e.faceUrl,
login_type: e.loginType,
is_quick_login: e.isQuickLogin,
is_auto_login: e.isAutoLogin,
}))
}

interface QuickLoginReq extends BotActionParams {
uin: `${number}`
}
const QuickLoginByUin = async (p: QuickLoginReq) => {
const result = await NTQuickLoginByUin(p.uin)
if (result.result !== '0')
{
throw new CustomError(parseInt(result.result), result.loginErrorInfo.errMsg)
}
return {}
}
export const initBot = () => {
const { registerActionHandle } = useStore()
// 登录
registerActionHandle('login_by_account', loginByAccount)
registerActionHandle('login_by_qrcode', loginByQrCode)
registerActionHandle('get_self_info', getBotInfo)
registerActionHandle('get_quick_login_list', getQuickLoginList)
registerActionHandle('quick_login_by_uin', QuickLoginByUin)
}
25 changes: 25 additions & 0 deletions src/onebot/actions/bot/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface QuickLoginItem {
/**
* QQ号
*/
uin: `${number}`
/**
* 用户ID
*/
uid: `u_${string}`
/**
* 昵称
*/
nick_name: string
face_url: string
face_path: string
login_type: 1 | 2
/**
* 是否快速登录
*/
is_quick_login: boolean
/**
* 是否自动登录
*/
is_auto_login: boolean
}
2 changes: 1 addition & 1 deletion src/onebot/actions/friend/friend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const getUserInfo = async (p: UserInfoReq): Promise<UserInfoResp> => {
log.info('getUserDetailInfo:', ret)
resp.user_uid = ret.uid
resp.user_uin = parseInt(ret.uin)
resp.nick = ret.nick
resp.nick = ret.simpleInfo.coreInfo.nick
resp.avatar_url = `http://q1.qlogo.cn/g?b=qq&nk=${resp.user_uin}&s=640`
return resp
}
Expand Down
21 changes: 5 additions & 16 deletions src/onebot/common/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,15 @@ export interface UserDetailInfoType {
* 唯一标识
*/
uid: `u_${string}`
qid: string
/**
* QQ号
*/
uin: string
/**
* QQ昵称
*/
nick: string
remark: string
longNick: string
avatarUrl: string
birthday_year: number
birthday_month: number
birthday_day: number
/**
* 2 - 女
*/
sex: number
regTime: number
simpleInfo: {
coreInfo: {
nick: string
}
}
}
export interface GroupDetailInfoType {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/onebot/common/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getUserInfoByUid = (uid: `u_${string}`): Promise<UserDetailInfoType
// 超时拒绝
let time = setTimeout(() => {
if (userInfoListener)
removeEventListener('KernelProfileListener/onProfileDetailInfoChanged', userInfoListener)
removeEventListener('KernelProfileListener/onUserDetailInfoChanged', userInfoListener)
reject('getUserInfoByUid timeout')
}, 30000)
userInfoListener = (payload: UserDetailInfoType) => {
Expand All @@ -23,7 +23,7 @@ export const getUserInfoByUid = (uid: `u_${string}`): Promise<UserDetailInfoType
clearTimeout(time)
resolve(payload)
}
registerEventListener(`KernelProfileListener/onProfileDetailInfoChanged`, 'once', userInfoListener)
registerEventListener(`KernelProfileListener/onUserDetailInfoChanged`, 'once', userInfoListener)
const { getWrapperSession } = useNTCore()
const session = getWrapperSession()
const service = session.getProfileService()
Expand Down
125 changes: 125 additions & 0 deletions src/onebot/event/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { useServer } from "../../server/server"
import { useStore } from "../../store/store"
import { EventDataType } from "./interfaces"

const { registerEventListener } = useStore()
/**
* 获取登录二维码
* @returns 二维码信息
*/
const qrCodeFailed = () => {
const { sendMessage } = useServer()
const getQrCode = registerEventListener('KernelLoginListener/onQRCodeSessionFailed', 'once', (p1: number, p2: number) => {
/**
* 未扫描,超时:1 10
* 扫描了,超时未确认:1 10
*/
const ret: EventDataType<any> = {
self: {
id: 0,
uid: 'u_0'
},
time: new Date().getTime(),
type: "notice",
detail_type: "qrcode_error",
sub_type: "",
data: {
p1,
p2,
}
}
sendMessage(JSON.stringify(ret))
})
}
/**
* 登录二维码被扫描
*
* @returns
*/
const qrCodeScaned = () => {
const { sendMessage } = useServer()
const getQrCode = registerEventListener('KernelLoginListener/onQRCodeSessionUserScaned', 'once', (p1: number, avatarUrl: string) => {

const ret: EventDataType<any> = {
self: {
id: 0,
uid: 'u_0'
},
time: new Date().getTime(),
type: "notice",
detail_type: "qrcode_scaned",
sub_type: "",
data: {
p1,
avatar_url: avatarUrl,
}
}
sendMessage(JSON.stringify(ret))
})
}

/**
* 二维码登录成功
*
* @returns
*/
const qrCodeSuccess = () => {
const { sendMessage } = useServer()
const getQrCode = registerEventListener('KernelLoginListener/onQRCodeLoginSucceed', 'once', (userInfo) => {

const ret: EventDataType<any> = {
self: {
id: parseInt(userInfo.uin),
uid: userInfo.uid
},
time: new Date().getTime(),
type: "notice",
detail_type: "qrcode_success",
sub_type: "",
data: {
account: userInfo.account,
uin: userInfo.uin,
uid: userInfo.uid,
nick_name: userInfo.nickName,
face_url: userInfo.faceUrl,
}
}
sendMessage(JSON.stringify(ret))
})
}

/**
* 二维码登录成功
*
* @returns
*/
const alreadyLogin = () => {
const { sendMessage } = useServer()
const getQrCode = registerEventListener('KernelLoginListener/onUserLoggedIn', 'once', (uin: number) => {

const ret: EventDataType<any> = {
self: {
id: 0,
uid: 'u_'
},
time: new Date().getTime(),
type: "notice",
detail_type: "qrcode_userLogged",
sub_type: "",
data: {
uin: uin,
}
}
sendMessage(JSON.stringify(ret))
})
}

/**
* 请求二维码的时候才会调用
*/
export const listenLoginEvent = () => {
qrCodeFailed();
qrCodeScaned();
qrCodeSuccess();
alreadyLogin();
}
Loading

0 comments on commit bb4ea3a

Please sign in to comment.