From 0be7627bd1e41deeb548812a810c74693a61d44f Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 7 Feb 2026 02:36:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E9=80=BB=E8=BE=91=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E5=BE=AE=E4=BF=A1=20UnionId=20=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/models/user.ts | 20 +++++++++-- src/db/drizzle/schema.ts | 49 ++++++++++---------------- src/routes/config/list.ts | 27 ++++++++------ src/routes/user/me.ts | 12 +++---- src/routes/user/modules/wx-services.ts | 29 +++++---------- src/routes/user/modules/wx.ts | 7 +++- 6 files changed, 72 insertions(+), 72 deletions(-) diff --git a/src/auth/models/user.ts b/src/auth/models/user.ts index 99ca36f..fec21ca 100644 --- a/src/auth/models/user.ts +++ b/src/auth/models/user.ts @@ -164,21 +164,38 @@ export class User { return users.length > 0 ? new User(users[0]) : null; } + /** + * 根据微信 UnionId 查找用户 + */ + static async findByUnionId(unionId: string): Promise { + 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 { + static async findOne(where: { username?: string; id?: string; email?: string }): Promise { 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; diff --git a/src/db/drizzle/schema.ts b/src/db/drizzle/schema.ts index be48bb7..5ca946b 100644 --- a/src/db/drizzle/schema.ts +++ b/src/db/drizzle/schema.ts @@ -3,19 +3,8 @@ import { sql, sum } from "drizzle-orm" export const enumCfRouterCodeType = pgEnum("enum_cf_router_code_type", ['route', 'middleware']) - -export const testPromptTools = pgTable("TestPromptTools", { - id: serial().primaryKey().notNull(), - template: text().notNull(), - args: jsonb().notNull(), - process: jsonb().notNull(), - type: varchar({ length: 255 }).notNull(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), -}); - export const aiAgent = pgTable("ai_agent", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), type: varchar({ length: 255 }).notNull(), baseUrl: varchar({ length: 255 }).notNull(), apiKey: varchar({ length: 255 }).notNull(), @@ -35,7 +24,7 @@ export const aiAgent = pgTable("ai_agent", { ]); export const appsTrades = pgTable("apps_trades", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), outTradeNo: varchar("out_trade_no", { length: 255 }).notNull(), money: integer().notNull(), subject: text().notNull(), @@ -63,7 +52,7 @@ export const cfOrgs = pgTable("cf_orgs", { ]); export const cfRouterCode = pgTable("cf_router_code", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), path: varchar({ length: 255 }).notNull(), key: varchar({ length: 255 }).notNull(), active: boolean().default(false), @@ -116,7 +105,7 @@ export const cfUserSecrets = pgTable("cf_user_secrets", { }); export const chatHistories = pgTable("chat_histories", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), data: json(), chatId: uuid(), chatPromptId: uuid(), @@ -129,7 +118,7 @@ export const chatHistories = pgTable("chat_histories", { }); export const chatPrompts = pgTable("chat_prompts", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: varchar({ length: 255 }).notNull(), description: text(), data: json(), @@ -141,7 +130,7 @@ export const chatPrompts = pgTable("chat_prompts", { }); export const chatSessions = pgTable("chat_sessions", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), data: json().default({}), chatPromptId: uuid(), type: varchar({ length: 255 }).default('production'), @@ -153,7 +142,7 @@ export const chatSessions = pgTable("chat_sessions", { }); export const fileSync = pgTable("file_sync", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), name: varchar({ length: 255 }), hash: varchar({ length: 255 }), stat: jsonb().default({}), @@ -166,7 +155,7 @@ export const fileSync = pgTable("file_sync", { ]); export const kvAiChatHistory = pgTable("kv_ai_chat_history", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), username: varchar({ length: 255 }).default('').notNull(), model: varchar({ length: 255 }).default('').notNull(), group: varchar({ length: 255 }).default('').notNull(), @@ -184,7 +173,7 @@ export const kvAiChatHistory = pgTable("kv_ai_chat_history", { }); export const kvApp = pgTable("kv_app", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), data: jsonb().default({}), version: varchar({ length: 255 }).default(''), key: varchar({ length: 255 }), @@ -204,7 +193,7 @@ export const kvApp = pgTable("kv_app", { ]); export const kvAppDomain = pgTable("kv_app_domain", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), domain: varchar({ length: 255 }).notNull(), appId: varchar({ length: 255 }), uid: varchar({ length: 255 }), @@ -218,7 +207,7 @@ export const kvAppDomain = pgTable("kv_app_domain", { ]); export const kvAppList = pgTable("kv_app_list", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), data: json().default({}), version: varchar({ length: 255 }).default(''), uid: uuid(), @@ -230,7 +219,7 @@ export const kvAppList = pgTable("kv_app_list", { }); export const kvConfig = pgTable("kv_config", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: text().default(''), key: text().default(''), description: text().default(''), @@ -268,7 +257,7 @@ export const kvGithub = pgTable("kv_github", { }); export const kvPackages = pgTable("kv_packages", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: text().default(''), description: text().default(''), tags: jsonb().default([]), @@ -282,7 +271,7 @@ export const kvPackages = pgTable("kv_packages", { }); export const kvPage = pgTable("kv_page", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: varchar({ length: 255 }).default(''), description: text().default(''), type: varchar({ length: 255 }).default(''), @@ -295,7 +284,7 @@ export const kvPage = pgTable("kv_page", { }); export const kvResource = pgTable("kv_resource", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), name: varchar({ length: 255 }).default(''), description: text().default(''), source: varchar({ length: 255 }).default(''), @@ -309,7 +298,7 @@ export const kvResource = pgTable("kv_resource", { }); export const kvVip = pgTable("kv_vip", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), userId: uuid().notNull(), level: varchar({ length: 255 }).default('free'), category: varchar({ length: 255 }).notNull(), @@ -324,7 +313,7 @@ export const kvVip = pgTable("kv_vip", { }); export const microAppsUpload = pgTable("micro_apps_upload", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: varchar({ length: 255 }).default(''), description: varchar({ length: 255 }).default(''), tags: jsonb().default([]), @@ -339,7 +328,7 @@ export const microAppsUpload = pgTable("micro_apps_upload", { }); export const microMark = pgTable("micro_mark", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: text().default(''), description: text().default(''), tags: jsonb().default([]), @@ -362,7 +351,7 @@ export const microMark = pgTable("micro_mark", { }); export const workShareMark = pgTable("work_share_mark", { - id: uuid().primaryKey().notNull(), + id: uuid().primaryKey().defaultRandom(), title: text().default(''), key: text().default(''), markType: text().default('md'), diff --git a/src/routes/config/list.ts b/src/routes/config/list.ts index 5ecc807..24380cb 100644 --- a/src/routes/config/list.ts +++ b/src/routes/config/list.ts @@ -29,7 +29,7 @@ app path: 'config', key: 'update', middleware: ['auth'], - description: '创建或更新用户配置,参数在data中传入', + description: '创建或更新用户配置,参数在data中传入' }) .define(async (ctx) => { const tokernUser = ctx.state.tokenUser; @@ -90,16 +90,21 @@ app config = updated[0]; ctx.body = config; } else { - // 根据key创建一个配置 - const inserted = await db.insert(schema.kvConfig).values({ - id: nanoid(), - key, - ...rest, - data: data, - uid: tuid, - }).returning(); - config = inserted[0]; - ctx.body = config; + try { + + // 根据key创建一个配置 + const inserted = await db.insert(schema.kvConfig).values({ + key, + ...rest, + data: data || {}, + uid: tuid, + }).returning(); + config = inserted[0]; + ctx.body = config; + } catch (e) { + console.log(e) + throw e; + } } } const key = config?.key; diff --git a/src/routes/user/me.ts b/src/routes/user/me.ts index ffee54e..c8a6618 100644 --- a/src/routes/user/me.ts +++ b/src/routes/user/me.ts @@ -101,9 +101,7 @@ app .define(async (ctx) => { const tokenUser = ctx.state?.tokenUser || {}; const { id } = tokenUser; - const user = await User.findByPk(id, { - logging: false, - }); + const user = await User.findByPk(id); if (!user) { ctx.throw(500, 'user not found'); } @@ -126,10 +124,10 @@ app } let user: User | null = null; if (username) { - user = await User.findOne({ where: { username }, logging: false }); + user = await User.findOne({ username }); } if (!user && email) { - user = await User.findOne({ where: { email } }); + user = await User.findOne({ email }); } if (!user) { ctx.throw(500, 'Login Failed'); @@ -242,7 +240,7 @@ app if (email) { user.email = email; } - await user.save({ fields: ['username', 'nickname', 'data', 'password', 'description', 'avatar', 'email'] }); + await user.save(); ctx.body = await user.getInfo(); }) .addTo(app); @@ -336,7 +334,7 @@ app await User.oauth.delToken(token); ctx.body = result; } else { - const user = await User.findOne({ where: { username } }); + const user = await User.findOne({ username }); const result = await user.createToken(userId, 'default'); createCookie({ token: result.accessToken, diff --git a/src/routes/user/modules/wx-services.ts b/src/routes/user/modules/wx-services.ts index d37fa3c..cc54d8c 100644 --- a/src/routes/user/modules/wx-services.ts +++ b/src/routes/user/modules/wx-services.ts @@ -22,9 +22,7 @@ export class WxServices { } async checkUserExist(username: string) { const user = await User.findOne({ - where: { - username, - }, + username, }); return !!user; } @@ -60,21 +58,16 @@ export class WxServices { } this.wxToken = token; const unionid = token.unionid; - let user = await User.findOne({ - where: { - data: { - wxUnionId: unionid, - }, - }, - }); + let user = await User.findByUnionId(unionid); // @ts-ignore if (type === 'mp' && user && user.data.wxmpOpenid !== token.openid) { - user.data = { + const updatedData = { ...user.data, // @ts-ignore wxmpOpenid: token.openid, }; - user = await user.update({ data: user.data }); + await user.update({ data: updatedData }); + user = await User.findByPk(user.id); } if (!user) { const username = await this.randomUsername(); @@ -92,7 +85,7 @@ export class WxServices { // @ts-ignore data.wxOpenid = token.openid; } - this.user = await user.save({ fields: ['data'] }); + this.user = await user.save(); await this.getUserInfo(); this.isNew = true; @@ -134,13 +127,7 @@ export class WxServices { }; const userInfo = await getUserInfoByMp(access_token, openid); const { unionid } = userInfo; - let user = await User.findOne({ - where: { - data: { - wxUnionId: unionid, - }, - }, - }); + let user = await User.findByUnionId(unionid); if (!user) { const username = await this.randomUsername(); user = await User.createUser(username, nanoid(10)); @@ -164,7 +151,7 @@ export class WxServices { fileds.push('avatar'); } this.isNew = true; - this.user = await user.save({ fields: fileds }); + this.user = await user.save(); } this.user = user; const tokenInfo = await user.createToken(null, 'plugin', { diff --git a/src/routes/user/modules/wx.ts b/src/routes/user/modules/wx.ts index f078258..563e5b8 100644 --- a/src/routes/user/modules/wx.ts +++ b/src/routes/user/modules/wx.ts @@ -3,11 +3,15 @@ import { useConfig } from '@kevisual/use-config'; export const config = useConfig() const wxOpen = { + // @ts-ignore appId: config.WX_OPEN_APP_ID, + // @ts-ignore appSecret: config.WX_OPEN_APP_SECRET, } const wx = { + // @ts-ignore appId: config.WX_MP_APP_ID, + // @ts-ignore appSecret: config.WX_MP_APP_SECRET, } export type WxTokenResponse = { @@ -139,7 +143,8 @@ type RefreshToken = { scope: string; }; export const refreshToken = async (refreshToken: string): Promise => { - const { appId } = config.wx; + // @ts-ignore + const { appId } = config?.wx; const refreshUrl = `https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=${appId}&grant_type=refresh_token&refresh_token=${refreshToken}`; const res = await fetch(refreshUrl); const data = await res.json();