From 197d6415d3872b2b80dc7037d6c1587f398d97c1 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Thu, 3 Apr 2025 20:20:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BE=AE=E4=BF=A1=E7=99=BB=E9=99=86?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/routes/wx/services.ts | 47 ++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/app/src/routes/wx/services.ts b/app/src/routes/wx/services.ts index 28badbb..020e85c 100644 --- a/app/src/routes/wx/services.ts +++ b/app/src/routes/wx/services.ts @@ -3,9 +3,11 @@ import { useContextKey } from '@kevisual/use-config/context'; import { UserModel } from '@kevisual/code-center-module'; import { Buffer } from 'buffer'; import { CustomError } from '@kevisual/router'; +import { customAlphabet } from 'nanoid'; +const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10); const User = useContextKey('UserModel'); export class WxServices { - token: WxTokenResponse; + wxToken: WxTokenResponse; // 创建一个webToken,用户登录 webToken: string; accessToken: string; @@ -16,12 +18,31 @@ export class WxServices { constructor() { // } + async checkUserExist(username: string) { + const user = await User.findOne({ + where: { + username, + }, + }); + return !!user; + } + async randomUsername() { + const a = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10); + const b = customAlphabet('1234567890', 10); + const random = '@' + a(4) + '' + b(4); + const user = await this.checkUserExist(random); + if (user) { + return this.randomUsername(); + } + return random; + } async login(code: string) { const token = await fetchToken(code); console.log('login token', token); if (!token.unionid) { throw new CustomError(400, 'code is invalid, wxdata can not be found'); } + this.wxToken = token; const unionid = token.unionid; let user = await User.findOne({ where: { @@ -41,7 +62,8 @@ export class WxServices { console.log('mp-user login openid update=============', token.openid, token.unionid); } if (!user) { - user = await User.createUser(unionid, unionid.slice(0, 8)); + const username = await this.randomUsername(); + user = await User.createUser(username, nanoid(10)); user.data = { ...user.data, // @ts-ignore @@ -72,16 +94,23 @@ export class WxServices { async checkHasUser() {} async getUserInfo() { - const userInfo = await getUserInfo(this.token.access_token, this.token.openid); - const { nickname, headimgurl } = userInfo; - this.user.nickname = nickname; try { - const downloadImgUrl = await this.downloadImg(headimgurl); - this.user.avatar = downloadImgUrl; + if (!this.wxToken) { + throw new CustomError(400, 'wxToken is not set'); + } + const userInfo = await getUserInfo(this.wxToken.access_token, this.wxToken.openid); + const { nickname, headimgurl } = userInfo; + this.user.nickname = nickname; + try { + const downloadImgUrl = await this.downloadImg(headimgurl); + this.user.avatar = downloadImgUrl; + } catch (error) { + console.error('Error downloading or converting image:', error); + } + await this.user.save(); } catch (error) { - console.error('Error downloading or converting image:', error); + console.error('Error getting user info:', error); } - await this.user.save(); } /** * 转成base64