This commit is contained in:
2025-12-25 16:59:45 +08:00
parent 18a7c15a76
commit 868979a423
4 changed files with 63 additions and 32 deletions

View File

@@ -14,6 +14,7 @@ type UserData = {
orgs?: string[]; orgs?: string[];
wxUnionId?: string; wxUnionId?: string;
phone?: string; phone?: string;
canChangeUsername?: boolean;
}; };
export enum UserTypes { export enum UserTypes {
'user' = 'user', 'user' = 'user',
@@ -182,6 +183,9 @@ export class User extends Model {
avatar: this.avatar, avatar: this.avatar,
orgs, orgs,
}; };
if(this.data?.canChangeUsername) {
info.canChangeUsername = this.data.canChangeUsername;
}
const tokenUser = this.tokenUser; const tokenUser = this.tokenUser;
if (uid) { if (uid) {
info.uid = uid; info.uid = uid;

View File

@@ -8,21 +8,58 @@ import { AppModel } from '@/routes/app-manager/index.ts';
export const checkUsername = (username: string) => { export const checkUsername = (username: string) => {
if (username.length > 30) { if (username.length > 30) {
throw new CustomError(400, 'Username cannot be too long'); throw new CustomError(400, '用户名不能过长');
} }
if (!/^[a-zA-Z0-9_@]+$/.test(username)) { if (!/^[a-zA-Z0-9_@]+$/.test(username)) {
throw new CustomError(400, 'Username cannot contain special characters'); throw new CustomError(400, '用户名包含非法字符');
} }
if (username.includes(' ')) { if (username.includes(' ')) {
throw new CustomError(400, 'Username cannot contain spaces'); throw new CustomError(400, '用户名不能包含空格');
} }
}; };
export const checkUsernameShort = (username: string) => { export const checkUsernameShort = (username: string) => {
if (username.length < 3) { if (username.length <= 3) {
throw new CustomError(400, 'Username cannot be too short'); throw new CustomError(400, '用户名不能过短');
} }
}; };
export const toChangeName = async (opts: { id: number; newName: string; admin?: boolean, ctx: any }) => {
const { id, newName, admin = false, ctx } = opts;
if (!admin) {
checkUsernameShort(newName);
}
const user = await User.findByPk(id);
if (!user) {
ctx.throw(404, 'User not found');
}
const oldName = user.username;
checkUsername(newName);
const findUserByUsername = await User.findOne({ where: { username: newName } });
if (findUserByUsername) {
ctx.throw(400, 'Username already exists');
}
user.username = newName;
const data = user.data || {};
data.canChangeUsername = false;
user.data = data;
try {
await user.save({ fields: ['username', 'data'] });
// 迁移文件数据
await backupUserA(oldName, user.id); // 备份文件数据
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
if (['org', 'user'].includes(user.type)) {
const type = user.type === 'org' ? 'org' : 'user';
await User.clearUserToken(user.id, type); // 清除旧token
}
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据
} catch (error) {
console.error('迁移文件数据失败', error);
ctx.throw(500, 'Failed to change username');
}
return user;
}
app app
.route({ .route({
path: 'user', path: 'user',
@@ -31,34 +68,16 @@ app
}) })
.define(async (ctx) => { .define(async (ctx) => {
const { id, newName } = ctx.query.data || {}; const { id, newName } = ctx.query.data || {};
const user = await User.findByPk(id);
if (!user) {
ctx.throw(404, 'User not found');
}
const oldName = user.username;
checkUsername(newName);
const findUserByUsername = await User.findOne({ where: { username: newName } });
if (findUserByUsername) {
ctx.throw(400, 'Username already exists');
}
user.username = newName;
try { try {
await user.save(); if (!id || !newName) {
// 迁移文件数据 ctx.throw(400, '参数错误');
await backupUserA(oldName, user.id); // 备份文件数据
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
if (['org', 'user'].includes(user.type)) {
const type = user.type === 'org' ? 'org' : 'user';
await User.clearUserToken(user.id, type); // 清除旧token
} }
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据 const user = await toChangeName({ id, newName, admin: true, ctx });
ctx.body = await user?.getInfo?.();
} catch (error) { } catch (error) {
console.error('迁移文件数据失败', error); console.error('changeName error', error);
ctx.throw(500, 'Failed to change username'); ctx.throw(500, 'Failed to change username');
} }
ctx.body = user;
}) })
.addTo(app); .addTo(app);

View File

@@ -209,7 +209,7 @@ app
middleware: ['auth'], middleware: ['auth'],
}) })
.define(async (ctx) => { .define(async (ctx) => {
const { username, password, description, avatar, email } = ctx.query.data || {}; const { username, nickname, password, description, avatar, email } = ctx.query.data || {};
const tokenUser = ctx.state?.tokenUser || {}; const tokenUser = ctx.state?.tokenUser || {};
const { id } = tokenUser; const { id } = tokenUser;
const user = await User.findByPk(id); const user = await User.findByPk(id);
@@ -217,8 +217,16 @@ app
ctx.throw(404, 'user not found'); ctx.throw(404, 'user not found');
} }
user.setTokenUser(tokenUser); user.setTokenUser(tokenUser);
if (username) { if (username && user.data?.canChangeUsername) {
user.username = username; user.username = username;
const data = user.data || {};
data.canChangeUsername = false;
user.data = data;
} else if (username && username !== user.username) {
ctx.throw(400, '请求更改用户名失败,用户名不可再更改,请联系管理员');
}
if (nickname) {
user.nickname = nickname;
} }
if (password && user.type !== 'org') { if (password && user.type !== 'org') {
user.createPassword(password); user.createPassword(password);
@@ -232,7 +240,7 @@ app
if (email) { if (email) {
user.email = email; user.email = email;
} }
await user.save(); await user.save({ fields: ['username', 'nickname', 'data', 'password', 'description', 'avatar', 'email'] });
ctx.body = await user.getInfo(); ctx.body = await user.getInfo();
}) })
.addTo(app); .addTo(app);

View File

@@ -53,7 +53,7 @@ app
.route({ .route({
path: 'wx', path: 'wx',
key: 'mp-get-openid', key: 'mp-get-openid',
description: '微信公众平台获取openid', description: '微信公众平台获取openid, 根据code回调去获取openid',
}) })
.define(async (ctx) => { .define(async (ctx) => {
const code = ctx.query.code; const code = ctx.query.code;