File size: 3,826 Bytes
db242f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
  Body,
  Controller,
  Get,
  Post,
  Put,
  Query,
  UsePipes,
} from '@nestjs/common';
import { Role } from '@prisma/client';

import { Payload, Public } from '@/common/guards/auth.guard';
import { JoiValidationPipe } from '@/common/pipes/joi';
import { WechatService } from '@/modules/auth/wechat.service';

import {
  bindIdentityDto,
  byPasswordDto,
  forgetPasswordDto,
  identityDto,
  validateCodeDto,
} from 'shared';

import {
  bindIdentitySchema,
  bindPasswordSchema,
  forgetPasswordSchema,
  identitySchema,
  passwordSchema,
  validateCodeSchema,
  withPasswordSchema,
} from './auth.dto';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private wechatService: WechatService,
  ) {}

  /* 方法一:密码登录 */
  @Public()
  @UsePipes(new JoiValidationPipe(withPasswordSchema))
  @Post('password')
  async password(@Body() data: byPasswordDto) {
    return {
      success: true,
      ...(await this.authService.loginPassword(data)),
    };
  }

  /* 方法二:验证码登录/注册 */
  /** 1. 获取验证码 **/
  @Public()
  @Get('validateCode')
  async newValidateCode(
    @Query(new JoiValidationPipe(identitySchema)) query: identityDto,
  ) {
    return await this.authService.newValidateCode(query.identity);
  }

  /** 2.通过验证码登录/注册,自动识别邮箱或手机号 */
  @Public()
  @UsePipes(new JoiValidationPipe(validateCodeSchema))
  @Post('validateCode')
  async loginByCode(@Body() data: validateCodeDto) {
    return {
      success: true,
      ...(await this.authService.WithValidateCode(data.identity, data.code)),
    };
  }

  /* 方法三:微信登录/注册 */
  @Public()
  @Get('wechatCode')
  async wechatOauth(@Query() code: string) {
    const tokenData = await this.wechatService.wechatOauth(code);
    const jwt = this.wechatService.loginByWeChat(tokenData);
    if (!jwt) {
      const userInfo = await this.wechatService.getInfo(
        tokenData.accessToken,
        tokenData.openid,
      );
      return {
        token: this.wechatService.registerByWeChat(tokenData, userInfo),
      };
    } else {
      return {
        token: jwt,
      };
    }
  }

  /* 忘记密码 */
  @Public()
  @Post('forgetPassword')
  @UsePipes(new JoiValidationPipe(forgetPasswordSchema))
  async forgetPassword(@Body() data: forgetPasswordDto) {
    await this.authService.forgetPassword(
      data.identity,
      data.code,
      data.newPassword,
    );
    return {
      success: true,
    };
  }

  /* 修改密码 */
  @Put('changePassword')
  async changePassword(
    @Payload('id') userId: number,
    @Body('password', new JoiValidationPipe(passwordSchema)) password: string,
  ) {
    await this.authService.changePassword(userId, password);
    return {
      success: true,
    };
  }

  /* 设置密码,可首次添加用户名,不可修改用户名 */
  @Put('bindPassword')
  async bindPassword(
    @Payload('id') userId: number,
    @Body(new JoiValidationPipe(bindPasswordSchema))
    body: { username?: string; password: string },
  ) {
    const user = await this.authService.bindPassword(userId, body.password);
    if (!user.name) {
      await this.authService.updateName(userId, body.username);
    }
    return {
      success: true,
    };
  }

  /* 绑定账户 */
  @Put('bindIdentity')
  @UsePipes(new JoiValidationPipe(bindIdentitySchema))
  async bindIdentity(
    @Payload('id') userId: number,
    @Body() data: bindIdentityDto,
  ) {
    await this.authService.bindIdentity(userId, data.identity, data.password);
    return {
      success: true,
    };
  }

  @Get('roles')
  async getRoles(@Payload('role') roles: Role[]) {
    return {
      success: true,
      data: roles,
    };
  }
}