+
@@ -39,6 +39,10 @@ const recentRate = computed(() => {
diff --git a/packages/plugin-status/server/adapter.ts b/packages/plugin-status/server/adapter.ts
index 3527f5c04d..a52706dddb 100644
--- a/packages/plugin-status/server/adapter.ts
+++ b/packages/plugin-status/server/adapter.ts
@@ -1,4 +1,5 @@
-import { Adapter, Bot, BotOptions, Context, Logger, Random } from 'koishi-core'
+import { Adapter, Bot, BotOptions, Context, Logger, omit, Random } from 'koishi-core'
+import { createHash } from 'crypto'
import Profile from './profile'
import WebSocket from 'ws'
@@ -17,12 +18,13 @@ class WebBot extends Bot<'sandbox'> {
}
async sendMessage(channelId: string, content: string) {
- this.socket.send({
- type: 'message',
- body: content,
- })
+ this._sendSocket('message', content)
return Random.uuid()
}
+
+ _sendSocket(type: string, body: any) {
+ this.socket.send(JSON.stringify({ type, body }))
+ }
}
const logger = new Logger('status')
@@ -75,7 +77,7 @@ export class WebAdapter extends Adapter<'sandbox'> {
if (type === 'token') {
const { platform, userId } = body
const user = await this.app.database.getUser(platform, userId, ['name'])
- if (!user) return socket.send(JSON.stringify({ type: 'token', body: { message: '没有此账户。' } }))
+ if (!user) return bot._sendSocket('login', { message: '没有此账户。' })
const id = `${platform}:${userId}`
const token = Random.uuid()
const expire = Date.now() + this.config.expiration
@@ -83,7 +85,18 @@ export class WebAdapter extends Adapter<'sandbox'> {
setTimeout(() => {
if (states[id]?.[1] > Date.now()) delete states[id]
}, this.config.expiration)
- socket.send(JSON.stringify({ type: 'token', body: { token, name: user.name } }))
+ bot._sendSocket('token', { token, name: user.name })
+ } else if (type === 'password') {
+ const { id, password } = body
+ await this.app.database.setUser('id', id, { password })
+ } else if (type === 'login') {
+ const { username, password } = body
+ const user = await this.app.database.getUser('name', username, ['password', 'authority', 'id'])
+ if (!user) return bot._sendSocket('login', { message: '没有此账户。' })
+ if (user.password !== createHash('sha256').update(password).digest('hex')) {
+ if (!user) return bot._sendSocket('login', { message: '用户名或密码错误。' })
+ }
+ bot._sendSocket('user', omit(user, ['password']))
} else {
logger.info(type, body)
}
diff --git a/packages/plugin-status/server/index.ts b/packages/plugin-status/server/index.ts
index c5b20f2e7a..41ccc87e6b 100644
--- a/packages/plugin-status/server/index.ts
+++ b/packages/plugin-status/server/index.ts
@@ -16,6 +16,10 @@ declare module 'koishi-core' {
interface EventMap {
'status/tick'(): void
}
+
+ interface User {
+ password: string
+ }
}
export interface Config extends WebUI.Config {
diff --git a/packages/plugin-status/server/mysql.ts b/packages/plugin-status/server/mysql.ts
index 03006c892a..e0d57caf22 100644
--- a/packages/plugin-status/server/mysql.ts
+++ b/packages/plugin-status/server/mysql.ts
@@ -173,6 +173,7 @@ Database.extend('koishi-plugin-mysql', {
Database.extend('koishi-plugin-mysql', ({ tables, Domain }) => {
tables.user.lastCall = 'timestamp'
+ tables.user.password = 'varchar(64)'
tables.channel.name = 'varchar(50)'
tables.channel.activity = new Domain.Json()
})