|
@@ -1,6 +1,7 @@
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
import { UserService } from '../users/user.service';
|
|
import { UserService } from '../users/user.service';
|
|
|
import { AuthService } from '../auth/auth.service';
|
|
import { AuthService } from '../auth/auth.service';
|
|
|
|
|
+import { FileService } from '../files/file.service';
|
|
|
import { UserEntity as User } from '../users/user.entity';
|
|
import { UserEntity as User } from '../users/user.entity';
|
|
|
import debug from 'debug';
|
|
import debug from 'debug';
|
|
|
|
|
|
|
@@ -15,7 +16,8 @@ export class WechatAuthService {
|
|
|
|
|
|
|
|
constructor(
|
|
constructor(
|
|
|
private readonly userService: UserService,
|
|
private readonly userService: UserService,
|
|
|
- private readonly authService: AuthService
|
|
|
|
|
|
|
+ private readonly authService: AuthService,
|
|
|
|
|
+ private readonly fileService: FileService
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
// 获取授权URL
|
|
// 获取授权URL
|
|
@@ -56,7 +58,7 @@ export class WechatAuthService {
|
|
|
// 3. 获取用户信息(首次登录)
|
|
// 3. 获取用户信息(首次登录)
|
|
|
const userInfo = await this.getUserInfo(access_token, openid);
|
|
const userInfo = await this.getUserInfo(access_token, openid);
|
|
|
|
|
|
|
|
- // 4. 创建新用户
|
|
|
|
|
|
|
+ // 4. 先创建用户
|
|
|
user = await this.userService.createUser({
|
|
user = await this.userService.createUser({
|
|
|
username: `wx_${openid.substring(0, 8)}`,
|
|
username: `wx_${openid.substring(0, 8)}`,
|
|
|
password: Math.random().toString(36).substring(2),
|
|
password: Math.random().toString(36).substring(2),
|
|
@@ -70,6 +72,31 @@ export class WechatAuthService {
|
|
|
wechatCountry: userInfo.country,
|
|
wechatCountry: userInfo.country,
|
|
|
nickname: userInfo.nickname
|
|
nickname: userInfo.nickname
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 下载微信头像并保存到MinIO
|
|
|
|
|
+ if (userInfo.headimgurl) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const avatarResult = await this.fileService.downloadAndSaveFromUrl(
|
|
|
|
|
+ userInfo.headimgurl,
|
|
|
|
|
+ {
|
|
|
|
|
+ uploadUserId: user.id, // 使用新创建的用户ID
|
|
|
|
|
+ customPath: 'avatars/wechat/',
|
|
|
|
|
+ mimeType: 'image/jpeg'
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 更新用户的 avatarFileId
|
|
|
|
|
+ await this.userService.updateUser(user.id, {
|
|
|
|
|
+ avatarFileId: avatarResult.file.id
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 更新返回的用户对象
|
|
|
|
|
+ user.avatarFileId = avatarResult.file.id;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ logger.error('下载微信头像失败:', error);
|
|
|
|
|
+ // 头像下载失败不影响主要流程
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 5. 生成JWT token
|
|
// 5. 生成JWT token
|
|
@@ -102,7 +129,26 @@ export class WechatAuthService {
|
|
|
// 3. 获取用户信息
|
|
// 3. 获取用户信息
|
|
|
const userInfo = await this.getUserInfo(tokenData.access_token, openid);
|
|
const userInfo = await this.getUserInfo(tokenData.access_token, openid);
|
|
|
|
|
|
|
|
- // 4. 更新用户信息
|
|
|
|
|
|
|
+ // 4. 下载微信头像并保存到MinIO
|
|
|
|
|
+ let avatarFileId: number | null = null;
|
|
|
|
|
+ if (userInfo.headimgurl) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const avatarResult = await this.fileService.downloadAndSaveFromUrl(
|
|
|
|
|
+ userInfo.headimgurl,
|
|
|
|
|
+ {
|
|
|
|
|
+ uploadUserId: userId,
|
|
|
|
|
+ customPath: 'avatars/wechat/',
|
|
|
|
|
+ mimeType: 'image/jpeg'
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+ avatarFileId = avatarResult.file.id;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ logger.error('下载微信头像失败:', error);
|
|
|
|
|
+ // 头像下载失败不影响主要流程,继续更新用户
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 更新用户信息
|
|
|
const user = await this.userService.updateUser(userId, {
|
|
const user = await this.userService.updateUser(userId, {
|
|
|
wechatOpenid: openid,
|
|
wechatOpenid: openid,
|
|
|
wechatUnionid: unionid,
|
|
wechatUnionid: unionid,
|
|
@@ -111,7 +157,8 @@ export class WechatAuthService {
|
|
|
wechatSex: userInfo.sex,
|
|
wechatSex: userInfo.sex,
|
|
|
wechatProvince: userInfo.province,
|
|
wechatProvince: userInfo.province,
|
|
|
wechatCity: userInfo.city,
|
|
wechatCity: userInfo.city,
|
|
|
- wechatCountry: userInfo.country
|
|
|
|
|
|
|
+ wechatCountry: userInfo.country,
|
|
|
|
|
+ avatarFileId: avatarFileId
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
if (!user) {
|