Skip to content

Commit

Permalink
chore: migrate from koishijs/koishi
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 12, 2022
1 parent cb3c7b5 commit a06ca52
Show file tree
Hide file tree
Showing 232 changed files with 12,506 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
insert_final_newline = true
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
lib
dist
temp

todo.md
yarn.lock
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log
package-lock.json
tsconfig.tsbuildinfo
report.*.json

.eslintcache
.DS_Store
.idea
.vscode
.yarn
*.suo
*.ntvs*
*.njsproj
*.sln
3 changes: 1 addition & 2 deletions LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

The MIT License (MIT)

Copyright (c) 2022 Shigma
Copyright (c) 2021-2022 Shigma

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@root/console",
"version": "1.0.0",
"private": true,
"workspaces": [
"packages/*"
],
"license": "MIT",
"scripts": {
"build": "yakumo tsc",
"bump": "yakumo version",
"dep": "yakumo upgrade",
"pub": "yakumo publish",
"test": "yakumo mocha",
"test:text": "rimraf coverage && c8 -r text yarn test",
"test:json": "rimraf coverage && c8 -r json yarn test",
"test:html": "rimraf coverage && c8 -r html yarn test"
},
"devDependencies": {
"@koishijs/plugin-database-memory": "^1.4.1",
"@koishijs/plugin-mock": "^2.0.0",
"@sinonjs/fake-timers": "^6.0.1",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.35",
"@types/sinonjs__fake-timers": "^6.0.4",
"c8": "^7.11.3",
"esbuild": "^0.14.48",
"esbuild-register": "^3.3.3",
"jest-mock": "^28.1.0",
"mocha": "^9.2.2",
"rimraf": "^3.0.2",
"typescript": "^4.7.2",
"yml-register": "^1.0.0",
"yakumo": "^0.2.8",
"yakumo-mocha": "^0.2.5",
"yakumo-publish": "^0.2.5",
"yakumo-tsc": "^0.2.4",
"yakumo-upgrade": "^0.2.3",
"yakumo-version": "^0.2.5"
},
"yakumo": {
"require": [
"esbuild-register",
"yml-register"
]
}
}
5 changes: 5 additions & 0 deletions packages/auth/client/icons/at.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg class="k-icon k-icon-at" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M256 8C118.941 8 8 118.919 8 256c0 137.059 110.919 248 248 248 48.154 0 95.342-14.14 135.408-40.223 12.005-7.815 14.625-24.288 5.552-35.372l-10.177-12.433c-7.671-9.371-21.179-11.667-31.373-5.129C325.92 429.757 291.314 440 256 440c-101.458 0-184-82.542-184-184S154.542 72 256 72c100.139 0 184 57.619 184 160 0 38.786-21.093 79.742-58.17 83.693-17.349-.454-16.91-12.857-13.476-30.024l23.433-121.11C394.653 149.75 383.308 136 368.225 136h-44.981a13.518 13.518 0 0 0-13.432 11.993l-.01.092c-14.697-17.901-40.448-21.775-59.971-21.775-74.58 0-137.831 62.234-137.831 151.46 0 65.303 36.785 105.87 96 105.87 26.984 0 57.369-15.637 74.991-38.333 9.522 34.104 40.613 34.103 70.71 34.103C462.609 379.41 504 307.798 504 232 504 95.653 394.023 8 256 8zm-21.68 304.43c-22.249 0-36.07-15.623-36.07-40.771 0-44.993 30.779-72.729 58.63-72.729 22.292 0 35.601 15.241 35.601 40.77 0 45.061-33.875 72.73-58.161 72.73z"/>
</svg>
</template>
5 changes: 5 additions & 0 deletions packages/auth/client/icons/lock.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg class="k-icon k-icon-lock" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
<path fill="currentColor" d="M400 224h-24v-72C376 68.2 307.8 0 224 0S72 68.2 72 152v72H48c-26.5 0-48 21.5-48 48v192c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V272c0-26.5-21.5-48-48-48zm-104 0H152v-72c0-39.7 32.3-72 72-72s72 32.3 72 72v72z"/>
</svg>
</template>
5 changes: 5 additions & 0 deletions packages/auth/client/icons/sign-in.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg class="k-icon k-icon-sign-in" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M416 448h-84c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h84c17.7 0 32-14.3 32-32V160c0-17.7-14.3-32-32-32h-84c-6.6 0-12-5.4-12-12V76c0-6.6 5.4-12 12-12h84c53 0 96 43 96 96v192c0 53-43 96-96 96zm-47-201L201 79c-15-15-41-4.5-41 17v96H24c-13.3 0-24 10.7-24 24v96c0 13.3 10.7 24 24 24h136v96c0 21.5 26 32 41 17l168-168c9.3-9.4 9.3-24.6 0-34z"/>
</svg>
</template>
5 changes: 5 additions & 0 deletions packages/auth/client/icons/sign-out.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg class="k-icon k-icon-sign-out" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M497 273L329 441c-15 15-41 4.5-41-17v-96H152c-13.3 0-24-10.7-24-24v-96c0-13.3 10.7-24 24-24h136V88c0-21.4 25.9-32 41-17l168 168c9.3 9.4 9.3 24.6 0 34zM192 436v-40c0-6.6-5.4-12-12-12H96c-17.7 0-32-14.3-32-32V160c0-17.7 14.3-32 32-32h84c6.6 0 12-5.4 12-12V76c0-6.6-5.4-12-12-12H96c-53 0-96 43-96 96v192c0 53 43 96 96 96h84c6.6 0 12-5.4 12-12z"/>
</svg>
</template>
5 changes: 5 additions & 0 deletions packages/auth/client/icons/user-full.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg class="k-icon k-icon-user-full" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512">
<path fill="currentColor" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm0 96c48.6 0 88 39.4 88 88s-39.4 88-88 88-88-39.4-88-88 39.4-88 88-88zm0 344c-58.7 0-111.3-26.6-146.5-68.2 18.8-35.4 55.6-59.8 98.5-59.8 2.4 0 4.8.4 7.1 1.1 13 4.2 26.6 6.9 40.9 6.9 14.3 0 28-2.7 40.9-6.9 2.3-.7 4.7-1.1 7.1-1.1 42.9 0 79.7 24.4 98.5 59.8C359.3 421.4 306.7 448 248 448z"/>
</svg>
</template>
50 changes: 50 additions & 0 deletions packages/auth/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Context, icons, message, router, send, store } from '@koishijs/client'
import { config } from './utils'
import Login from './login.vue'
import Profile from './profile.vue'
import At from './icons/at.vue'
import Lock from './icons/lock.vue'
import SignIn from './icons/sign-in.vue'
import SignOut from './icons/sign-out.vue'
import UserFull from './icons/user-full.vue'

icons.register('at', At)
icons.register('lock', Lock)
icons.register('sign-in', SignIn)
icons.register('sign-out', SignOut)
icons.register('user-full', UserFull)

export default (ctx: Context) => {
if (config.token && config.expire > Date.now()) {
send('login/token', config.id, config.token).catch(e => message.error(e.message))
}

ctx.disposables.push(router.beforeEach((route) => {
if ((route.meta.authority || route.meta.fields.includes('user')) && !store.user) {
// handle router.back()
return history.state.forward === '/login' ? '/' : '/login'
}

if (route.meta.authority && route.meta.authority > store.user.authority) {
message.error('权限不足。')
return false
}
}))

ctx.addPage({
path: '/login',
name: '登录',
icon: 'sign-in',
position: () => store.user ? 'hidden' : 'bottom',
component: Login,
})

ctx.addPage({
path: '/profile',
name: '用户资料',
icon: 'user-full',
fields: ['user'],
position: () => store.user ? 'bottom' : 'hidden',
component: Profile,
})
}
137 changes: 137 additions & 0 deletions packages/auth/client/login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<template>
<k-card class="login">
<template v-if="user">
<h1><span>平台账户登录</span></h1>
<p class="hint">欢迎你,{{ user.name || 'Koishi 用户' }}!</p>
<p class="hint">请用上述账号将下面的验证码私聊发送给任意机器人</p>
<p class="token">{{ user.token }}</p>
<div class="control">
<k-button @click="user.token = null">返回上一步</k-button>
</div>
</template>
<template v-else>
<h1 v-if="secure">
<k-choose :data="['平台账户登录', '用户名密码登录']" v-model="config.authType"></k-choose>
</h1>
<h1 v-else><span>平台账户登录</span></h1>
<template v-if="config.authType === 0">
<el-input placeholder="平台名" v-model="config.platform" #prefix>
<k-icon name="at"></k-icon>
</el-input>
<el-input placeholder="账号" v-model="config.userId" @keypress.enter.stop="loginWithAccount" #prefix>
<k-icon name="user"></k-icon>
</el-input>
<p class="error" v-if="message">{{ message }}</p>
<div class="control">
<k-button @click="$router.back()">返回</k-button>
<k-button @click="loginWithAccount">获取验证码</k-button>
</div>
</template>
<template v-else>
<el-input placeholder="用户名" v-model="config.name" #prefix>
<k-icon name="user"></k-icon>
</el-input>
<el-input placeholder="密码" v-model="config.password" @keypress.enter.stop="loginWithPassword"
:type="config.showPass ? 'text' : 'password'">
<template #prefix><k-icon name="lock"></k-icon></template>
<template #suffix>
<k-icon :name="config.showPass ? 'eye' : 'eye-slash'" @click="config.showPass = !config.showPass"></k-icon>
</template>
</el-input>
<p class="error" v-if="message">{{ message }}</p>
<div class="control">
<k-button @click="$router.back()">返回</k-button>
<k-button @click="loginWithPassword">登录</k-button>
</div>
</template>
</template>
</k-card>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { config, sha256 } from './utils'
import { send } from '@koishijs/client'
import { UserLogin } from '@koishijs/plugin-auth'
const secure = isSecureContext
if (!secure) config.authType = 0
const message = ref<string>()
const user = ref<UserLogin>()
let timestamp = 0
async function loginWithAccount() {
const now = Date.now()
if (now < timestamp) return
const { platform, userId } = config
if (!platform || !userId) return
timestamp = now + 1000
try {
user.value = await send('login/platform', platform, userId)
} catch (e) {
message.value = e.message
}
}
async function loginWithPassword() {
const { name, password } = config
try {
await send('login/password', name, await sha256(password))
} catch (e) {
message.value = e.message
}
}
</script>

<style lang="scss">
.route-login {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
section.login {
width: 600px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
margin: 0;
.k-card-body {
padding: 3rem 0 !important;
}
h1 {
font-size: 1.5rem;
margin: 2.5rem auto;
cursor: default;
}
.token {
font-weight: bold;
}
.el-input {
display: block;
max-width: 400px;
margin: 1rem auto;
}
.control {
margin: 2.5rem auto;
}
.k-button {
width: 8rem;
margin: 0 1rem;
}
}
</style>
70 changes: 70 additions & 0 deletions packages/auth/client/profile.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<template>
<k-card class="profile-basic">
<h1>
基本资料
<k-button class="float-right" solid type="error" @click="logout">退出登录</k-button>
<k-button class="float-right" solid :disabled="!Object.keys(diff).length" @click="update">应用更改</k-button>
</h1>
<k-form :schema="schema" v-model="diff" :show-header="false"></k-form>
</k-card>
</template>

<script lang="ts" setup>
import { send, store } from '@koishijs/client'
import { sha256, config } from './utils'
import { computed, ref } from 'vue'
import { message, Schema } from '@koishijs/client'
import { UserUpdate } from '@koishijs/plugin-auth'
const diff = ref<UserUpdate>({})
const schema = computed(() => {
const result: Schema<UserUpdate> = Schema.object({
name: Schema.string().description('用户名').default(config.name),
})
if (isSecureContext) {
result.dict.password = Schema.string().role('secret').description('密码').default(config.password)
}
return result
})
async function logout() {
delete config.id
delete config.token
delete config.expire
return send('user/logout')
}
async function update() {
const update: UserUpdate = { ...diff.value }
if (update.password) {
update.password = await sha256(update.password)
}
try {
await send('user/update', update)
message.success('修改成功!')
Object.assign(config, diff)
Object.assign(store.user, update)
diff.value = {}
} catch (e) {
message.error(e.message)
}
}
</script>

<style lang="scss">
.profile-basic {
max-width: 50rem;
margin: 2rem auto;
h1 {
font-size: 1.375rem;
margin: 1.5rem 0;
line-height: 2rem;
}
}
</style>
9 changes: 9 additions & 0 deletions packages/auth/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": ".",
},
"include": [
".",
],
}
Loading

0 comments on commit a06ca52

Please sign in to comment.