From 4e080a0b93de66e831e1922663515f22971d700d Mon Sep 17 00:00:00 2001 From: xion Date: Sun, 23 Feb 2025 10:22:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9D=E5=A7=8B=E5=8C=96=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/user.ts | 51 ++++++++++++++++++++++++++-------------- src/modules/domain.ts | 8 +++++++ src/routes/user/index.ts | 2 ++ src/routes/user/init.ts | 34 +++++++++++++++++++++++++++ src/routes/user/me.ts | 11 +++------ 5 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 src/modules/domain.ts create mode 100644 src/routes/user/init.ts diff --git a/src/models/user.ts b/src/models/user.ts index d46351d..d943295 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -3,7 +3,7 @@ import { sequelize } from '@/modules/sequelize.ts'; import { DataTypes, Model, Op } from 'sequelize'; import { createToken, checkToken } from '@kevisual/auth'; import { cryptPwd } from '@kevisual/auth'; -import { nanoid } from 'nanoid'; +import { customRandom, nanoid, customAlphabet } from 'nanoid'; import { CustomError } from '@kevisual/router'; import { Org } from './org.ts'; import { redis } from '@/app.ts'; @@ -17,6 +17,7 @@ export class User extends Model { declare id: string; declare username: string; declare nickname: string; // 昵称 + declare alias: string; // 别名 declare password: string; declare salt: string; declare needChangePassword: boolean; @@ -36,7 +37,7 @@ export class User extends Model { * @param uid * @returns */ - async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year' ) { + async createToken(uid?: string, loginType?: 'default' | 'plugin' | 'month' | 'season' | 'year') { const { id, username, type } = this; let expireTime = 60 * 60 * 24 * 7; // 7 days switch (loginType) { @@ -169,6 +170,10 @@ User.init( type: DataTypes.TEXT, allowNull: true, }, + alias: { + type: DataTypes.TEXT, + allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复 + }, password: { type: DataTypes.STRING, allowNull: true, @@ -221,38 +226,48 @@ User.sync({ alter: true, logging: false }) console.error('Sync User error', err); }); -export const initializeUser = async () => { +const letter = 'abcdefghijklmnopqrstuvwxyz'; +const custom = customAlphabet(letter, 6); +export const initializeUser = async (pwd = custom()) => { const w = await User.findAndCountAll(); console.info('[User count]', w.count); - const password = '2e8a305521bba54f49638ed25e46adf3'; //123456 - const salt = '123'; if (w.count < 1) { - const root = await User.create({ - username: 'root', - password: password, - needChangePassword: true, - type: 'user', - description: '系统管理员', - salt, - }); + const root = await User.createUser('root', pwd, '系统管理员'); const org = await User.createOrg('admin', root.id, '管理员'); console.info(' new Users name', root.username, org.username); + console.info('new Users root password', pwd); console.info('new Users id', root.id, org.id); - CreateDemoUser(); + const demo = await createDemoUser(); + return { + code: 200, + data: { root, org, pwd: pwd, demo }, + }; + } else { + return { + code: 500, + message: 'Users has been created', + }; } }; -export const CreateDemoUser = async () => { +export const createDemoUser = async (username = 'demo', pwd = custom()) => { const w = await User.findAndCountAll({ logging: false, }); console.info('[User count]', w.count); - const username = 'demo'; const u = await User.findOne({ where: { username }, logging: false }); if (!u) { - const user = await User.createUser(username, '', 'demo'); + const user = await User.createUser(username, pwd, 'demo'); console.info('new Users name', user.username); + return { + code: 200, + data: { user, pwd: pwd }, + }; } else { console.info('Users has been created', u.username); + return { + code: 500, + message: 'Users has been created', + }; } }; // initializeUser(); @@ -268,4 +283,6 @@ export class UserServices extends User { const token = await user.createToken(null, 'season'); return { ...token, isNew }; } + static initializeUser = initializeUser; + static createDemoUser = createDemoUser; } diff --git a/src/modules/domain.ts b/src/modules/domain.ts new file mode 100644 index 0000000..d56a22d --- /dev/null +++ b/src/modules/domain.ts @@ -0,0 +1,8 @@ +import { useConfig } from '@kevisual/use-config'; + +type MinioConfig = { + domain: string; +}; +const config = useConfig(); + +export const domain = config.domain || 'xiongxiao.me'; diff --git a/src/routes/user/index.ts b/src/routes/user/index.ts index dc57b21..2657df8 100644 --- a/src/routes/user/index.ts +++ b/src/routes/user/index.ts @@ -4,3 +4,5 @@ import './org.ts'; import './me.ts'; import './update.ts' + +import './init.ts' \ No newline at end of file diff --git a/src/routes/user/init.ts b/src/routes/user/init.ts new file mode 100644 index 0000000..63bac1b --- /dev/null +++ b/src/routes/user/init.ts @@ -0,0 +1,34 @@ +import { UserServices } from '@/models/user.ts'; + +import { app } from '@/app.ts'; + +app + .route({ + path: 'user', + key: 'init', + }) + .define(async (ctx) => { + const { password } = ctx.query.data || {}; + if (!password) { + ctx.throw(500, 'root password are required'); + } + const res = await UserServices.initializeUser(password); + ctx.body = res; + }) + .addTo(app); + +app + .route({ + path: 'user', + key: 'createDemo', + middleware: ['auth'], + }) + .define(async (ctx) => { + const { username, password } = ctx.query.data || {}; + if (!username && username.startsWith('demo')) { + ctx.throw(500, 'username are required, and must start with demo'); + } + const res = await UserServices.createDemoUser(username, password); + ctx.body = res; + }) + .addTo(app); diff --git a/src/routes/user/me.ts b/src/routes/user/me.ts index 14b025b..bd127ea 100644 --- a/src/routes/user/me.ts +++ b/src/routes/user/me.ts @@ -1,6 +1,7 @@ import { app } from '@/app.ts'; import { Org } from '@/models/org.ts'; import { User } from '@/models/user.ts'; +import { domain } from '@/modules/domain.ts'; app .route({ @@ -38,12 +39,6 @@ app if (!user && email) { user = await User.findOne({ where: { email } }); } - console.log('user logiin', ctx.query); - console.log('user logiin', user); - console.log( - 'users', - (await User.findAll()).map((u) => u.username), - ); if (!user) { ctx.throw(500, 'Login Failed'); } @@ -53,7 +48,7 @@ app const token = await user.createToken(null, loginType); ctx.res.cookie('token', token.token, { maxAge: token.expireTime, - domain: 'xiongxiao.me', + domain: { domain }, sameSite: 'lax', httpOnly: true, }); @@ -69,7 +64,7 @@ app .define(async (ctx) => { ctx.res.cookie('token', '', { maxAge: 0, - domain: 'xiongxiao.me', + domain: `${domain}`, sameSite: 'lax', httpOnly: true, });