返回 AiToEarn
wxGzh.controller.ts
根目录 / project / aitoearn-electron / server / src / user / wxGzh.controller.ts
1 /*
2 * @Author: nevin
3 * @Date: 2024-06-17 19:19:20
4 * @LastEditTime: 2025-02-26 09:58:29
5 * @LastEditors: nevin
6 * @Description: 微信公众号事件接受
7 */
8 import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
9 import { Public } from 'src/auth/auth.guard';
10 import { OrgGuard } from 'src/interceptor/transform.interceptor';
11 import { WxGzhService } from 'src/lib/wx/wxGzh.service';
12 import { RedisService } from 'src/lib/redis/redis.service';
13 import { User, UserStatus } from 'src/db/schema/user.schema';
14 import { UserService } from './user.service';
15 import { NewUserByWx } from './class/user.class';
16 import { AuthService } from 'src/auth/auth.service';
17 import { getGzhLoginKey } from './user.comment';
18
19 @Controller('wxGzh')
20 export class WxGzhController {
21 constructor(
22 private readonly wxGzhService: WxGzhService,
23 private readonly userService: UserService,
24 private readonly authService: AuthService,
25 private readonly redisService: RedisService,
26 ) {}
27
28 @Public()
29 @UseGuards(OrgGuard)
30 @Get('msg')
31 async checkMsg(
32 @Query()
33 query: {
34 signature: string;
35 echostr: string;
36 timestamp: string;
37 nonce: string;
38 },
39 ) {
40 const res = await this.wxGzhService.checkCallback(query);
41 return res;
42 }
43
44 // 接收微信公众号的消息
45 // 消息(暂不处理)
46 // {
47 // ToUserName: 'gh_6fd6e64e6e8f',
48 // FromUserName: 'oPgBP7GGjJIlm3d2xxoIuUOG1iEs',
49 // CreateTime: '1740498823',
50 // MsgType: 'text',
51 // Content: 'Nih',
52 // MsgId: '24917948940934036'
53 // }
54
55 // 进行关注
56 // {
57 // ToUserName: 'gh_6fd6e64e6e8f',
58 // FromUserName: 'oPgBP7GGjJIlm3d2xxoIuUOG1iEs',
59 // CreateTime: '1740498747',
60 // MsgType: 'event',
61 // Event: 'subscribe',
62 // EventKey: 'qrscene_1',
63 // Ticket: 'gQG18TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydUhSdTFsLTBkZkkxLTJ5NmhEMTYAAgQCqL1nAwSAOgkA'
64 // }
65
66 // 扫码
67 // {
68 // ToUserName: 'gh_6fd6e64e6e8f',
69 // FromUserName: 'oPgBP7GGjJIlm3d2xxoIuUOG1iEs',
70 // CreateTime: '1740498973',
71 // MsgType: 'event',
72 // Event: 'SCAN',
73 // EventKey: '1',
74 // Ticket: 'gQG18TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydUhSdTFsLTBkZkkxLTJ5NmhEMTYAAgQCqL1nAwSAOgkA'
75 // }
76 @Public()
77 @UseGuards(OrgGuard)
78 @Post('msg')
79 async acceptMsg(
80 @Query()
81 query: {
82 signature: string;
83 timestamp: string;
84 nonce: string;
85 },
86 @Body()
87 body: {
88 FromUserName: string;
89 MsgType: 'text' | 'event';
90 Event?: 'subscribe' | 'SCAN';
91 EventKey?: string;
92 Ticket?: string;
93 },
94 ) {
95 console.log('------------ body', body);
96
97 const { FromUserName, MsgType, Ticket, EventKey, Event } = body;
98 if (MsgType !== 'event') return '';
99 if (!Ticket) return '';
100
101 let key = '';
102 if (Event === 'subscribe') key = EventKey.replace('qrscene_', '');
103 if (Event === 'SCAN') key = EventKey;
104
105 let userInfo: User =
106 await this.userService.getUserInfoByWxOpenId(FromUserName);
107
108 if (!userInfo) {
109 const newUser = new NewUserByWx({
110 openid: FromUserName,
111 });
112 userInfo = await this.userService.createUser(newUser);
113 }
114
115 if (userInfo.status === UserStatus.STOP) return '';
116
117 const token = await this.authService.generateToken(userInfo);
118 const TokenInfo = await this.authService.decodeToken(token);
119 const loginData = {
120 token,
121 exp: TokenInfo.exp,
122 userInfo: await this.userService.getUserInfoById(TokenInfo.id),
123 };
124
125 this.redisService.setKey(getGzhLoginKey(Ticket, key), loginData, 60 * 5);
126 return '';
127 }
128 }
129
129 lines TYPESCRIPT