import { Injectable } from '@nestjs/common'; import { RedisService } from '../../common/redis/redis.service'; const TTL_SECONDS = 30 * 60; export type WecomUserVerifySession = { phone: string; /** 验证通过后可查看 */ verified: boolean; pendingUserId?: string; }; @Injectable() export class WecomBotSessionService { constructor(private readonly redis: RedisService) {} private key(botKey: string, wecomUserId: string) { return `dukang:wecom:session:${botKey}:${wecomUserId}`; } async get(botKey: string, wecomUserId: string): Promise { return this.redis.getJson(this.key(botKey, wecomUserId)); } async setPendingPhone(botKey: string, wecomUserId: string, phone: string) { await this.redis.setJson( this.key(botKey, wecomUserId), { phone, verified: false } satisfies WecomUserVerifySession, TTL_SECONDS, ); } async markVerified(botKey: string, wecomUserId: string, phone: string, userId: string) { await this.redis.setJson( this.key(botKey, wecomUserId), { phone, verified: true, pendingUserId: userId } satisfies WecomUserVerifySession, TTL_SECONDS, ); } async clear(botKey: string, wecomUserId: string) { await this.redis.del(this.key(botKey, wecomUserId)); } }