auth.controller.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // apps/box-app-api/src/feature/auth/auth.controller.ts
  2. import { Body, Controller, Post, Req } from '@nestjs/common';
  3. import { AuthService } from './auth.service';
  4. import { Request } from 'express';
  5. import { LoginDto } from './login.dto';
  6. import {
  7. ApiTags,
  8. ApiOperation,
  9. ApiResponse,
  10. ApiOkResponse,
  11. } from '@nestjs/swagger';
  12. @ApiTags('授权')
  13. @Controller('auth')
  14. export class AuthController {
  15. constructor(private readonly authService: AuthService) {}
  16. @Post('login')
  17. @ApiOperation({
  18. summary: '用户登录/落地注册',
  19. description: '首次登录会自动注册并绑定渠道',
  20. })
  21. @ApiOkResponse({
  22. description:
  23. '登录成功,返回 uid、绑定后的 channelId,并在可用时返回启动广告 startupAds。',
  24. })
  25. @ApiResponse({ status: 400, description: '请求参数错误' })
  26. async login(@Body() body: LoginDto, @Req() req: Request) {
  27. const ip =
  28. (req.headers['x-forwarded-for'] as string)?.split(',')[0]?.trim() ||
  29. (req.headers['x-real-ip'] as string) ||
  30. req.ip;
  31. const userAgent = req.headers['user-agent'];
  32. return this.authService.login({
  33. uid: body.uid,
  34. ip,
  35. userAgent,
  36. appVersion: body.appVersion,
  37. os: body.os,
  38. channelId: body.channelId,
  39. machine: body.machine,
  40. });
  41. }
  42. }