微信SDK接通
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { BadRequestException, Body, Controller, Get, Inject, Post, Query } from '@nestjs/common';
|
||||
import { IsIn, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { WECHAT_PROVIDER } from '../../integrations/integrations.constants';
|
||||
import type { IWechatProvider } from '../../integrations/wechat/wechat.interface';
|
||||
|
||||
class PhoneNumberDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
|
||||
@IsString()
|
||||
@IsIn(['mini', 'h5'])
|
||||
@IsOptional()
|
||||
platform?: 'mini' | 'h5';
|
||||
}
|
||||
|
||||
@Controller('common/wechat')
|
||||
export class WechatController {
|
||||
constructor(@Inject(WECHAT_PROVIDER) private readonly wechat: IWechatProvider) {}
|
||||
|
||||
@Get('jssdk-config')
|
||||
async jssdkConfig(@Query('url') url: string) {
|
||||
if (!url) throw new BadRequestException('url 参数必填');
|
||||
return this.wechat.createJssdkConfig(url);
|
||||
}
|
||||
|
||||
@Get('oauth-url')
|
||||
oauthUrl(
|
||||
@Query('redirectUri') redirectUri: string,
|
||||
@Query('state') state: string,
|
||||
@Query('scope') scope?: string,
|
||||
) {
|
||||
if (!redirectUri || !state) throw new BadRequestException('redirectUri 与 state 必填');
|
||||
return { url: this.wechat.buildOAuthUrl(redirectUri, state, scope) };
|
||||
}
|
||||
|
||||
@Post('phone-number')
|
||||
phoneNumber(@Body() dto: PhoneNumberDto) {
|
||||
return this.wechat
|
||||
.getPhoneNumberByCode(dto.code, dto.platform ?? 'mini')
|
||||
.then((phone) => ({ phone }));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user