优化用户查找逻辑,新增根据微信 UnionId 查找用户的方法,简化数据库查询

This commit is contained in:
2026-02-07 02:36:29 +08:00
parent 885e04e301
commit 0be7627bd1
6 changed files with 72 additions and 72 deletions

View File

@@ -164,21 +164,38 @@ export class User {
return users.length > 0 ? new User(users[0]) : null;
}
/**
* 根据微信 UnionId 查找用户
*/
static async findByUnionId(unionId: string): Promise<User | null> {
const users = await db
.select()
.from(usersTable)
.where(sql`${usersTable.data}->>'wxUnionId' = ${unionId}`)
.limit(1);
return users.length > 0 ? new User(users[0]) : null;
}
/**
* 根据条件查找一个用户
*/
static async findOne(where: { username?: string; id?: string }): Promise<User | null> {
static async findOne(where: { username?: string; id?: string; email?: string }): Promise<User | null> {
let query = db.select().from(usersTable);
if (where.username) {
query = query.where(eq(usersTable.username, where.username)) as any;
} else if (where.id) {
query = query.where(eq(usersTable.id, where.id)) as any;
} else if (where.email) {
query = query.where(eq(usersTable.email, where.email)) as any;
}
const users = await query.limit(1);
return users.length > 0 ? new User(users[0]) : null;
}
static findByunionid(){
}
static async createUser(username: string, password?: string, description?: string) {
const user = await User.findOne({ username });
@@ -324,7 +341,6 @@ export class User {
*/
async getOrgs() {
let id = this.id;
console.log('type', this.type, this.tokenUser)
if (this.type === 'org') {
if (this.tokenUser && this.tokenUser.uid) {
id = this.tokenUser.uid;