xiongzhu пре 2 година
родитељ
комит
f131c92efc

+ 1 - 1
src/app.module.ts

@@ -17,7 +17,7 @@ import { WeixinModule } from './weixin/weixin.module'
 import { NotifyModule } from './notify/notify.module'
 import { CommissionModule } from './commission/commission.module'
 import { UserBalanceModule } from './user-balance/user-balance.module'
-import { WithdrawModule } from './withdraw/withdraw.module';
+import { WithdrawModule } from './withdraw/withdraw.module'
 
 @Module({
     imports: [

+ 6 - 0
src/auth/auth.controller.ts

@@ -14,4 +14,10 @@ export class AuthController {
     async phoneLogin(@Body() loginDto: PhoneLoginDto) {
         return await this.authService.loginByPhone(loginDto)
     }
+
+    @Public()
+    @Post('/admin/login')
+    async login(@Body() { username, password }) {
+        return await this.authService.loginAdmin(username, password)
+    }
 }

+ 12 - 0
src/auth/auth.service.ts

@@ -18,4 +18,16 @@ export class AuthService {
             access_token: this.jwtService.sign(payload)
         }
     }
+
+    async loginAdmin(username: string, password: string) {
+        let user = await this.usersService.loginAdmin(username, password)
+        const payload = {
+            username: user.username,
+            sub: user.id,
+            roles: user.roles
+        }
+        return {
+            access_token: this.jwtService.sign(payload)
+        }
+    }
 }

+ 10 - 0
src/common/dto/page-request.ts

@@ -0,0 +1,10 @@
+import { IsObject } from 'class-validator'
+import { IPaginationOptions } from 'nestjs-typeorm-paginate'
+import { FindManyOptions, FindOptionsWhere } from 'typeorm'
+
+export class PageRequest<T> {
+    @IsObject()
+    page: IPaginationOptions
+
+    search?: FindOptionsWhere<T> | FindManyOptions<T>
+}

+ 1 - 2
src/main.ts

@@ -1,4 +1,4 @@
-import { NestFactory, PartialGraphHost } from '@nestjs/core'
+import { NestFactory } from '@nestjs/core'
 import { AppModule } from './app.module'
 import { Logger, ValidationPipe } from '@nestjs/common'
 import { ConfigService } from '@nestjs/config'
@@ -43,6 +43,5 @@ async function bootstrap() {
 }
 bootstrap().catch((err) => {
     Logger.error(err, 'Bootstrap')
-    writeFileSync('graph.json', PartialGraphHost.toString() ?? '')
     process.exit(1)
 })

+ 6 - 0
src/users/users.admin.controller.ts

@@ -32,6 +32,12 @@ export class UsersAdminController {
         return await this.usersService.findAll(options)
     }
 
+    @Get('/get')
+    public async get(@Req() req) {
+        const user = await this.usersService.findById(req.user.userId)
+        return user
+    }
+
     @Put('/:userId')
     public async updateUser(@Param('userId') userId: string, @Body() userUpdateDto: UserUpdateDto) {
         try {

+ 5 - 1
src/users/users.controller.ts

@@ -23,8 +23,12 @@ export class UsersController {
 
     @Get('/my')
     public async my(@Req() req) {
+        return await this.get(req)
+    }
+
+    @Get('/get')
+    public async get(@Req() req) {
         const user = await this.usersService.findById(req.user.userId)
-        console.log(user)
         return user
     }
 

+ 18 - 1
src/users/users.service.ts

@@ -4,7 +4,8 @@ import {
     HttpException,
     HttpStatus,
     BadRequestException,
-    InternalServerErrorException
+    InternalServerErrorException,
+    UnauthorizedException
 } from '@nestjs/common'
 import { Repository, UpdateResult } from 'typeorm'
 import { InjectRepository } from '@nestjs/typeorm'
@@ -18,6 +19,7 @@ import { SmsService } from '../sms/sms.service'
 import * as randomstring from 'randomstring'
 import { MembershipService } from '../membership/membership.service'
 import { paginate, Pagination, IPaginationOptions } from 'nestjs-typeorm-paginate'
+import { Role } from 'src/model/role.enum'
 
 @Injectable()
 export class UsersService {
@@ -79,6 +81,21 @@ export class UsersService {
         return user
     }
 
+    public async loginAdmin(username: string, password: string): Promise<Users> {
+        let user = await this.userRepository.findOneBy({ username })
+        if (!user) {
+            throw new UnauthorizedException('用户名或密码错误')
+        }
+        const isMatch = await this.hashingService.compare(password, user.password)
+        if (!isMatch) {
+            throw new UnauthorizedException('用户名或密码错误')
+        }
+        if (!user.roles.includes(Role.Admin)) {
+            throw new UnauthorizedException('用户名或密码错误')
+        }
+        return user
+    }
+
     public async create(userDto: UserCreateDto): Promise<IUsers> {
         try {
             return await this.userRepository.save(userDto)

+ 6 - 1
src/weixin/weixin.service.ts

@@ -24,6 +24,7 @@ import { readFileSync, writeFileSync, statSync } from 'fs'
 import { JsapiTicketCache } from './entities/jsapi-ticket-cache.entity'
 import { Attach } from './dto/attach.dto'
 import { join } from 'path'
+import BigNumber from 'bignumber.js'
 
 @Injectable()
 export class WeixinService {
@@ -147,6 +148,10 @@ export class WeixinService {
     }
 
     async jsapiPay(description: string, amount: number, openid: string, attach: Attach) {
+        let realAmount = new BigNumber(amount)
+        if (realAmount.comparedTo(new BigNumber(100)) >= 0) {
+            realAmount = realAmount.minus(new BigNumber((Math.random() * 2).toFixed(2)))
+        }
         let data = {
             appid: this.weixinConfiguration.appId,
             mchid: this.weixinConfiguration.mchId,
@@ -154,7 +159,7 @@ export class WeixinService {
             out_trade_no: Kits.generateStr(),
             notify_url: this.weixinConfiguration.notifyUrl,
             amount: {
-                total: amount * 100,
+                total: realAmount.times(new BigNumber(100)).toFixed(0),
                 currency: 'CNY'
             },
             payer: {

+ 2 - 1
src/withdraw/entities/withdraw.entity.ts

@@ -1,4 +1,5 @@
 import BigNumber from 'bignumber.js'
+import { DecimalTransformer } from '../../transformers/decimal.transformer'
 import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
 
 export enum WithdrawStatus {
@@ -19,7 +20,7 @@ export class Withdraw {
     @Column()
     userId: number
 
-    @Column({ type: 'decimal', precision: 19, scale: 2 })
+    @Column({ type: 'decimal', precision: 19, scale: 2, transformer: new DecimalTransformer() })
     amount: BigNumber
 
     @Column()

+ 5 - 3
src/withdraw/withdraw.admin.controller.ts

@@ -1,10 +1,12 @@
-import { IPaginationOptions } from 'nestjs-typeorm-paginate';
+import { IPaginationOptions } from 'nestjs-typeorm-paginate'
 import { Body, Controller, Post, Req } from '@nestjs/common'
 import { WithdrawApplyDto } from './dto/withdraw-apply.dto'
 import { WithdrawService } from './withdraw.service'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
 import { HasRoles } from 'src/auth/roles.decorator'
 import { Role } from 'src/model/role.enum'
+import { PageRequest } from 'src/common/dto/page-request'
+import { Withdraw } from './entities/withdraw.entity'
 
 @ApiTags('withdraw.admin')
 @ApiBearerAuth()
@@ -14,8 +16,8 @@ export class WithdrawAdminController {
     constructor(private readonly withdrawService: WithdrawService) {}
 
     @Post()
-    async list(@Body() page:IPaginationOptions) {
-        
+    async list(@Body() page: PageRequest<Withdraw>) {
+        return await this.withdrawService.findAll(page)
     }
 
     @Post('/finish')

+ 4 - 3
src/withdraw/withdraw.service.ts

@@ -6,6 +6,7 @@ import { Repository } from 'typeorm'
 import BigNumber from 'bignumber.js'
 import { BalanceType } from '../user-balance/entities/balance-record.entity'
 import { IPaginationOptions, Pagination, paginate } from 'nestjs-typeorm-paginate'
+import { PageRequest } from '../common/dto/page-request'
 
 @Injectable()
 export class WithdrawService {
@@ -15,8 +16,8 @@ export class WithdrawService {
         private readonly withdrawRepository: Repository<Withdraw>
     ) {}
 
-    async findAll(options: IPaginationOptions): Promise<Pagination<Withdraw>> {
-        return await paginate<Withdraw>(this.withdrawRepository, options)
+    async findAll(req: PageRequest<Withdraw>): Promise<Pagination<Withdraw>> {
+        return await paginate<Withdraw>(this.withdrawRepository, req.page, req.search)
     }
 
     async applyWithdraw(userId: number, amount: string, name: string, account: string, remark?: string) {
@@ -33,7 +34,7 @@ export class WithdrawService {
                 name,
                 account,
                 status: WithdrawStatus.PENDING,
-                remark
+                remark: remark || '123'
             })
         )
     }

Разлика између датотеке није приказан због своје велике величине
+ 733 - 511
yarn.lock


Неке датотеке нису приказане због велике количине промена