返回 AiToEarn
google.controller.ts
根目录 / project / aitoearn-electron / server / src / modules / plat / google / google.controller.ts
1 /*
2 * @Author: nevin
3 * @Date: 2025-02-15 20:59:55
4 * @LastEditTime: 2025-04-27 18:00:18
5 * @LastEditors: nevin
6 * @Description: signIn SignIn 签到
7 */
8 import {
9 Body,
10 Controller,
11 Get,
12 Param,
13 Post,
14 Query,
15 UploadedFile,
16 UseInterceptors,
17 BadRequestException,
18 Render,
19 Res
20 } from '@nestjs/common';
21 import { ApiOperation, ApiTags } from '@nestjs/swagger';
22 import {
23 AccessBackDto,
24 ArchiveAddByUtokenBodyDto,
25 ArchiveAddByUtokenQueryDto,
26 GoogleLoginDto
27 } from './dto/google.dto';
28 import { GoogleService } from './google.service';
29 import { GetToken, Public } from 'src/auth/auth.guard';
30 import { FileInterceptor } from '@nestjs/platform-express';
31 import { Response } from 'express';
32 import { TokenInfo } from 'src/auth/interfaces/auth.interfaces';
33 import { Account } from 'src/db/schema/account.schema';
34 import { ApiResult } from 'src/common/decorators/api-result.decorator';
35 import { ParamsValidationPipe } from 'src/validation.pipe';
36
37 @ApiTags('plat/google - Google账号登录')
38 @Controller('plat/google')
39 export class GoogleController {
40 constructor(private readonly googleService: GoogleService) {}
41
42 @ApiOperation({ summary: '测试' })
43 @Public()
44 @Get('test')
45 // @Render('google/index')
46 async getTest(@Res() res: Response) {
47 // const res = "success";
48 const result = { message: "hello" };
49 console.log(result); // 在控制台输出,确保数据正确
50 // return result;
51 return res.render('google/index', result);
52 }
53
54
55 @ApiOperation({ summary: '获取用户登录授权URL' })
56 @Public()
57 @Get('auth/code')
58 async getGooleAuthCode(
59 // @GetToken() token: TokenInfo,
60 @Param('type') type: 'h5' | 'pc',
61 @Query('platform') platform: string,
62 @Query('userEmail') userEmail?: string,
63 ) {
64 // const result = await this.googleService.getAuthCode(userEmail, platform, type);
65 const result = await this.googleService.getAuthCode_v2(platform, type);
66 console.log(result.data);
67 return result;
68 }
69
70 @ApiOperation({ summary: 'Google登录' })
71 @Public()
72 // @Get('auth/login')
73 @Post('auth/login')
74 @Public()
75 @ApiResult({ type: Account })
76 async googleLogin(
77 @Body(new ParamsValidationPipe()) body: GoogleLoginDto,
78 ) {
79 // console.log(body.clientId, body.credential)
80 return this.googleService.googleLogin(body.clientId, body.credential);
81 }
82
83 @ApiOperation({ summary: '获取AccessToken' })
84 @Public()
85 @Get('auth/accessToken')
86 async getAccessToken(
87 @Query('code') code: string,
88 @Query('state') state: string,
89 // @Query() query: AccessBackDto,
90 // @Res() res: Response
91 ) {
92
93 try {
94 // 使用授权码 (code) 交换访问令牌
95 const systemToken = await this.googleService.setUserAccessToken_V2({
96 code,
97 state
98 });
99 console.log("+++++++++++++++++++++++");
100 console.log(systemToken );
101 console.log("+++++++++++++++++++++++");
102
103 // 成功获取令牌后,返回成功消息或视图
104 // return res.redirect(301, 'https://www.example.com?name=JohnDoe&age=30');
105 // return res.render('google/index', { message: '授权成功', userInfo: userInfo})
106 return systemToken;
107 } catch (err) {
108 console.log('Error during access token exchange', err);
109 // return res.render('google/index', { message: '授权失败', error: err });
110 return false
111 }
112
113 // const result = await this.googleService.getAccessToken(code)
114 // return result;
115
116 // const result = { message: "hello" };
117 // return result;
118 // return res.render('google/index', { message: result});
119 // const data = {
120 // ...body,
121 // code: body.code,
122 // state: body.state,
123 // };
124 // const { accessToken, uploadToken } = query;
125 // return this.bilibiliService.archiveAddByUtoken(
126 // accessToken,
127 // uploadToken,
128 // data,
129 // );
130 }
131
132
133 @ApiOperation({ summary: '刷新授权Token' })
134 // @Public()
135 @Get('auth/refreshAccessToken')
136 async refreshAccessToken(
137 @GetToken() token: TokenInfo,
138 ) {
139 return this.googleService.refreshAccessToken(token.id);
140 }
141
142 @ApiOperation({ summary: '获取已授权用户信息' })
143 // @Public()
144 // @Get('auth/userinfo/:accessToken')
145 @Get('auth/userinfo')
146 async getUserInfo(
147 // @Param('accessToken') accessToken: string,
148 // @Query('userId') userId: string,
149 @GetToken() token: TokenInfo,
150 ) {
151 // return this.googleService.getUserInfo(accessToken, token);
152 console.log("前端传来的token: ", token);
153 const accessToken = await this.googleService.getUserAccessToken(token.id);
154 return this.googleService.getUserInfo(accessToken, token);
155 }
156
157 @ApiOperation({ summary: '查询用户已授权权限列表' })
158 // @Public()
159 @Get('auth/scopes')
160 async getAccountScopes(
161 // @Param('accessToken') accessToken: string
162 @GetToken() token: TokenInfo,
163 ) {
164 const accessToken = await this.googleService.getUserAccessToken(token.id);
165 return this.googleService.getAccountScopes(accessToken);
166 }
167
168 }
169
169 lines TYPESCRIPT