Commit 980c4376 authored by nanahira's avatar nanahira

change payment interface to pay-exp

parent 0593163d
Pipeline #17562 passed with stages
in 2 minutes and 28 seconds
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
Query, Query,
UploadedFile, UploadedFile,
UseInterceptors, UseInterceptors,
ValidationPipe,
} from '@nestjs/common'; } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { config } from './config'; import { config } from './config';
...@@ -33,6 +34,7 @@ import { ...@@ -33,6 +34,7 @@ import {
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { FileUploadDto } from './dto/FileUploadDto'; import { FileUploadDto } from './dto/FileUploadDto';
import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto'; import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto';
import { PayExpDto } from './dto/PayExp.dto';
@Controller('api') @Controller('api')
@ApiTags('arena') @ApiTags('arena')
...@@ -274,11 +276,20 @@ export class AppController { ...@@ -274,11 +276,20 @@ export class AppController {
return this.appService.getLastMonthBattleCount(); return this.appService.getLastMonthBattleCount();
} }
@Get('novelai-auth') @Get('pay-exp')
@ApiOperation({ summary: 'novelai 用认证' }) @ApiOperation({ summary: '支付 exp' })
@ApiHeader({ name: 'Authorization' }) @ApiHeader({ name: 'Authorization' })
@ApiOkResponse({ type: CodeResponseDto }) @ApiOkResponse({ type: CodeResponseDto })
async novelaiAuth(@Headers('Authorization') token: string) { async payExp(
return this.appService.novelaiAuth(token); @Headers('Authorization') token: string,
@Query(
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
}),
)
query: PayExpDto,
) {
return this.appService.payExp(token, query);
} }
} }
...@@ -40,6 +40,7 @@ import { AthleticCheckerService } from './athletic-checker/athletic-checker.serv ...@@ -40,6 +40,7 @@ import { AthleticCheckerService } from './athletic-checker/athletic-checker.serv
import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto'; import { HomePageMatchCountDto } from './dto/HomePageMatchCount.dto';
import { AccountService } from './account/account.service'; import { AccountService } from './account/account.service';
import { CodeResponseDto } from './dto/CodeResponse.dto'; import { CodeResponseDto } from './dto/CodeResponse.dto';
import { PayExpDto } from './dto/PayExp.dto';
const attrOffset = 1010; const attrOffset = 1010;
const raceOffset = 1020; const raceOffset = 1020;
...@@ -1781,7 +1782,7 @@ export class AppService { ...@@ -1781,7 +1782,7 @@ export class AppService {
return new HomePageMatchCountDto(count); return new HomePageMatchCountDto(count);
} }
async novelaiAuth(token: string) { async payExp(token: string, dto: PayExpDto) {
const user = await this.accountService.checkToken(token); const user = await this.accountService.checkToken(token);
if (user.admin) { if (user.admin) {
return new CodeResponseDto(200); return new CodeResponseDto(200);
...@@ -1793,22 +1794,22 @@ export class AppService { ...@@ -1793,22 +1794,22 @@ export class AppService {
}); });
if (!userInfo) { if (!userInfo) {
this.log.log( this.log.log(
`${user.username} has no duel records, cannot use novelai.`, `${user.username} has no duel records, cannot use ${dto.getName()}.`,
); );
throw new HttpException(new CodeResponseDto(402), 402); throw new HttpException(new CodeResponseDto(402), 402);
} }
if (userInfo.exp < config.novelaiCost) { if (userInfo.exp < dto.cost) {
this.log.log( this.log.log(
`${user.username} has no enough exp to use novelai: ${userInfo.exp}`, `${user.username} has no enough exp to use novelai: ${userInfo.exp}`,
); );
throw new HttpException(new CodeResponseDto(402), 402); throw new HttpException(new CodeResponseDto(402), 402);
} }
this.log.log( this.log.log(
`${user.username} paid ${config.novelaiCost} exp to use novelai.`, `${user.username} paid ${dto.cost} exp to use ${dto.getName()}.`,
); );
await edb await edb
.getRepository(UserInfo) .getRepository(UserInfo)
.decrement({ username: user.username }, 'exp', config.novelaiCost); .decrement({ username: user.username }, 'exp', dto.cost);
return new CodeResponseDto(200); return new CodeResponseDto(200);
}); });
} }
......
...@@ -9,7 +9,6 @@ export interface Config { ...@@ -9,7 +9,6 @@ export interface Config {
analyzerHost: string; analyzerHost: string;
enableSchedule: boolean; enableSchedule: boolean;
accountsUrl: string; accountsUrl: string;
novelaiCost: number;
} }
export const athleticCheckConfig = { export const athleticCheckConfig = {
...@@ -35,7 +34,4 @@ export const config: Config = { ...@@ -35,7 +34,4 @@ export const config: Config = {
enableSchedule: !process.env.NO_SCHEDULE, enableSchedule: !process.env.NO_SCHEDULE,
accountsUrl: accountsUrl:
process.env.ACCOUNTS_URL || 'https://sapi.moecube.com:444/accounts', process.env.ACCOUNTS_URL || 'https://sapi.moecube.com:444/accounts',
novelaiCost: process.env.NOVELAI_COST
? parseInt(process.env.NOVELAI_COST)
: 3,
}; };
...@@ -5,8 +5,11 @@ export class CodeResponseDto { ...@@ -5,8 +5,11 @@ export class CodeResponseDto {
code: number; code: number;
@ApiProperty({ description: '是否成功' }) @ApiProperty({ description: '是否成功' })
success: boolean; success: boolean;
constructor(code: number) { @ApiProperty({ description: '错误信息' })
message?: string;
constructor(code: number, message?: string) {
this.success = code < 400; this.success = code < 400;
this.code = code; this.code = code;
this.message = message;
} }
} }
import { ApiProperty } from "@nestjs/swagger";
import { IsInt, IsOptional, IsPositive, IsString } from "class-validator";
export class PayExpDto {
@IsInt()
@IsPositive()
@ApiProperty({ description: '支付数量' })
cost: number;
@IsString()
@IsOptional()
@ApiProperty({ description: '支付名称' })
name?: string;
getName() {
return this.name || 'service';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment