import { Controller, Get, Post, Body, Req, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ApiOptionalAuthGuard } from '../../../../core/security/apiOptionalAuth.guard'; import { SiteScopeGuard } from '../../../../core/security/siteScopeGuard'; import { WeappService } from '../../services/weapp.service'; @ApiTags('前台-小程序') @UseGuards(ApiOptionalAuthGuard, SiteScopeGuard) @Controller('api/weapp') export class WeappController { constructor(private readonly weappService: WeappService) {} /** * 授权登录 */ @Post('login') @ApiOperation({ summary: '小程序授权登录' }) @ApiResponse({ status: 200 }) async login( @Body('code') code: string, @Body('nickname') nickname: string, @Body('headimg') headimg: string, @Body('mobile') mobile: string, @Body('mobile_code') mobileCode: string, @Req() req: any ) { const siteId = Number(req.auth?.('site_id') ?? req.siteId ?? 0) || 0; const data = { code, nickname, headimg, mobile, mobileCode, siteId }; const result = await this.weappService.login(data); return { code: 0, data: result, msg: 'success' }; } /** * 注册 */ @Post('register') @ApiOperation({ summary: '小程序用户注册' }) @ApiResponse({ status: 200 }) async register( @Body('openid') openid: string, @Body('unionid') unionid: string, @Body('mobile_code') mobileCode: string, @Body('mobile') mobile: string, @Req() req: any ) { const siteId = Number(req.auth?.('site_id') ?? req.siteId ?? 0) || 0; const data = { openid, unionid, mobileCode, mobile, siteId }; const result = await this.weappService.register(data); return result; } /** * 获取用户信息 */ @Get('getUserInfo') @ApiOperation({ summary: '获取小程序用户信息' }) @ApiResponse({ status: 200 }) async getUserInfo( @Req() req: any ) { const openid = req.auth?.('openid') || ''; const siteId = Number(req.auth?.('site_id') ?? req.siteId ?? 0) || 0; const result = await this.weappService.getUserInfo(openid, siteId); return { code: 0, data: result, msg: 'success' }; } /** * 更新用户信息 */ @Post('updateUserInfo') @ApiOperation({ summary: '更新小程序用户信息' }) @ApiResponse({ status: 200 }) async updateUserInfo( @Body() updateData: any, @Req() req: any ) { const openid = req.auth?.('openid') || ''; const siteId = Number(req.auth?.('site_id') ?? req.siteId ?? 0) || 0; const result = await this.weappService.updateUserInfo(openid, siteId, updateData); return { code: 0, data: result, msg: 'success' }; } }