|
|
@@ -1,17 +1,17 @@
|
|
|
import { DataSource, Repository } from 'typeorm';
|
|
|
import { UserEntity } from '@d8d/user-module';
|
|
|
-// import { FileService } from '@d8d/file-module';
|
|
|
+import { FileService } from '@d8d/file-module';
|
|
|
import { JWTUtil } from '@d8d/shared-utils';
|
|
|
import axios from 'axios';
|
|
|
import process from 'node:process'
|
|
|
|
|
|
export class MiniAuthService {
|
|
|
private userRepository: Repository<UserEntity>;
|
|
|
- // private fileService: FileService;
|
|
|
+ private fileService: FileService;
|
|
|
|
|
|
constructor(dataSource: DataSource) {
|
|
|
this.userRepository = dataSource.getRepository(UserEntity);
|
|
|
- // this.fileService = new FileService(dataSource);
|
|
|
+ this.fileService = new FileService(dataSource);
|
|
|
}
|
|
|
|
|
|
async miniLogin(code: string): Promise<{ token: string; user: UserEntity; isNewUser: boolean }> {
|
|
|
@@ -48,18 +48,17 @@ export class MiniAuthService {
|
|
|
if (profile.nickname) user.nickname = profile.nickname;
|
|
|
|
|
|
// 处理头像:如果用户没有头像且提供了小程序头像URL,则下载保存
|
|
|
- // 暂时注释掉文件服务相关功能,因为 file-module 尚未创建
|
|
|
- // if (profile.avatarUrl && !user.avatarFileId) {
|
|
|
- // try {
|
|
|
- // const avatarFileId = await this.downloadAndSaveAvatar(profile.avatarUrl, userId);
|
|
|
- // if (avatarFileId) {
|
|
|
- // user.avatarFileId = avatarFileId;
|
|
|
- // }
|
|
|
- // } catch (error) {
|
|
|
- // // 头像下载失败不影响主要功能
|
|
|
- // console.error('头像下载失败:', error);
|
|
|
- // }
|
|
|
- // }
|
|
|
+ if (profile.avatarUrl && !user.avatarFileId) {
|
|
|
+ try {
|
|
|
+ const avatarFileId = await this.downloadAndSaveAvatar(profile.avatarUrl, userId);
|
|
|
+ if (avatarFileId) {
|
|
|
+ user.avatarFileId = avatarFileId;
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ // 头像下载失败不影响主要功能
|
|
|
+ console.error('头像下载失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return await this.userRepository.save(user);
|
|
|
}
|
|
|
@@ -109,24 +108,24 @@ export class MiniAuthService {
|
|
|
return await this.userRepository.save(user);
|
|
|
}
|
|
|
|
|
|
- // private async downloadAndSaveAvatar(avatarUrl: string, userId: number): Promise<number | null> {
|
|
|
- // try {
|
|
|
- // const result = await this.fileService.downloadAndSaveFromUrl(
|
|
|
- // avatarUrl,
|
|
|
- // {
|
|
|
- // uploadUserId: userId,
|
|
|
- // customPath: `avatars/`,
|
|
|
- // mimeType: 'image/jpeg'
|
|
|
- // },
|
|
|
- // { timeout: 10000 }
|
|
|
- // );
|
|
|
-
|
|
|
- // return result.file.id;
|
|
|
- // } catch (error) {
|
|
|
- // console.error('下载保存头像失败:', error);
|
|
|
- // return null;
|
|
|
- // }
|
|
|
- // }
|
|
|
+ private async downloadAndSaveAvatar(avatarUrl: string, userId: number): Promise<number | null> {
|
|
|
+ try {
|
|
|
+ const result = await this.fileService.downloadAndSaveFromUrl(
|
|
|
+ avatarUrl,
|
|
|
+ {
|
|
|
+ uploadUserId: userId,
|
|
|
+ customPath: `avatars/`,
|
|
|
+ mimeType: 'image/jpeg'
|
|
|
+ },
|
|
|
+ { timeout: 10000 }
|
|
|
+ );
|
|
|
+
|
|
|
+ return result.file.id;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('下载保存头像失败:', error);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private generateToken(user: UserEntity): string {
|
|
|
return JWTUtil.generateToken(user);
|