fix: 微信登陆随机账号
This commit is contained in:
		| @@ -3,9 +3,11 @@ import { useContextKey } from '@kevisual/use-config/context'; | |||||||
| import { UserModel } from '@kevisual/code-center-module'; | import { UserModel } from '@kevisual/code-center-module'; | ||||||
| import { Buffer } from 'buffer'; | import { Buffer } from 'buffer'; | ||||||
| import { CustomError } from '@kevisual/router'; | import { CustomError } from '@kevisual/router'; | ||||||
|  | import { customAlphabet } from 'nanoid'; | ||||||
|  | const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10); | ||||||
| const User = useContextKey<typeof UserModel>('UserModel'); | const User = useContextKey<typeof UserModel>('UserModel'); | ||||||
| export class WxServices { | export class WxServices { | ||||||
|   token: WxTokenResponse; |   wxToken: WxTokenResponse; | ||||||
|   // 创建一个webToken,用户登录 |   // 创建一个webToken,用户登录 | ||||||
|   webToken: string; |   webToken: string; | ||||||
|   accessToken: string; |   accessToken: string; | ||||||
| @@ -16,12 +18,31 @@ export class WxServices { | |||||||
|   constructor() { |   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) { |   async login(code: string) { | ||||||
|     const token = await fetchToken(code); |     const token = await fetchToken(code); | ||||||
|     console.log('login token', token); |     console.log('login token', token); | ||||||
|     if (!token.unionid) { |     if (!token.unionid) { | ||||||
|       throw new CustomError(400, 'code is invalid, wxdata can not be found'); |       throw new CustomError(400, 'code is invalid, wxdata can not be found'); | ||||||
|     } |     } | ||||||
|  |     this.wxToken = token; | ||||||
|     const unionid = token.unionid; |     const unionid = token.unionid; | ||||||
|     let user = await User.findOne({ |     let user = await User.findOne({ | ||||||
|       where: { |       where: { | ||||||
| @@ -41,7 +62,8 @@ export class WxServices { | |||||||
|       console.log('mp-user login openid update=============', token.openid, token.unionid); |       console.log('mp-user login openid update=============', token.openid, token.unionid); | ||||||
|     } |     } | ||||||
|     if (!user) { |     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 = { | ||||||
|         ...user.data, |         ...user.data, | ||||||
|         // @ts-ignore |         // @ts-ignore | ||||||
| @@ -72,7 +94,11 @@ export class WxServices { | |||||||
|  |  | ||||||
|   async checkHasUser() {} |   async checkHasUser() {} | ||||||
|   async getUserInfo() { |   async getUserInfo() { | ||||||
|     const userInfo = await getUserInfo(this.token.access_token, this.token.openid); |     try { | ||||||
|  |       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; |       const { nickname, headimgurl } = userInfo; | ||||||
|       this.user.nickname = nickname; |       this.user.nickname = nickname; | ||||||
|       try { |       try { | ||||||
| @@ -82,6 +108,9 @@ export class WxServices { | |||||||
|         console.error('Error downloading or converting image:', error); |         console.error('Error downloading or converting image:', error); | ||||||
|       } |       } | ||||||
|       await this.user.save(); |       await this.user.save(); | ||||||
|  |     } catch (error) { | ||||||
|  |       console.error('Error getting user info:', error); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|   /** |   /** | ||||||
|    * 转成base64 |    * 转成base64 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user