Просмотр исходного кода

更新环境配置,修改端口号和数据库名称,删除不再使用的控制器、服务、路由和实体文件,清理代码库。

wui 7 месяцев назад
Родитель
Сommit
4b8e92089d

+ 3 - 3
.env

@@ -1,4 +1,4 @@
-PORT=3000
+PORT=3010
 HOST=0.0.0.0
 NODE_ENV=development
 
@@ -7,8 +7,8 @@ DB_HOST=rdsave1o67m1ido6gwp6public.mysql.rds.aliyuncs.com
 DB_PORT=3306
 DB_USERNAME=zouma
 DB_PASSWORD='2wsx@WSX#EDC'
-DB_DATABASE=robin_test
+DB_DATABASE=tweb_test
 
 # JWT
-JWT_SECRET='3t1jt1rq^cRJex@Z'
+JWT_SECRET='G5HXsfhW!gKr&4W8'
 JWT_EXPIRES_IN=7d

+ 0 - 6
src/app.ts

@@ -8,9 +8,6 @@ import fastifyEnv, { FastifyEnvOptions } from '@fastify/env'
 import { schema } from './config/env'
 import { createDataSource } from './config/database'
 import userRoutes from './routes/user.routes'
-import walletRoutes from './routes/wallet.routes'
-import tgUserRoutes from './routes/tg-user.routes'
-import deviceRoutes from './routes/device.routes'
 import { config } from 'dotenv'
 
 config()
@@ -68,9 +65,6 @@ const start = async () => {
   })
 
   app.register(userRoutes, { prefix: '/api/users' })
-  app.register(walletRoutes, { prefix: '/api/wallets' })
-  app.register(tgUserRoutes, { prefix: '/api/tg-users' })
-  app.register(deviceRoutes, { prefix: '/api/devices' })
 
   const dataSource = createDataSource(app)
   await dataSource.initialize()

+ 0 - 29
src/controllers/device.controller.ts

@@ -1,29 +0,0 @@
-import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
-import { ListDeviceBody, NoticeDeviceQuery } from '../dto/device.dto'
-import { DeviceService } from '../services/device.service'
-
-export class DeviceController {
-  private deviceService: DeviceService
-
-  constructor(app: FastifyInstance) {
-    this.deviceService = new DeviceService(app)
-  }
-
-  async notice(request: FastifyRequest<{ Body: NoticeDeviceQuery }>, reply: FastifyReply) {
-    try {
-      await this.deviceService.notice(request.body)
-      return reply.code(201).send()
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async findAll(request: FastifyRequest<{ Querystring: ListDeviceBody }>, reply: FastifyReply) {
-    try {
-      const devices = await this.deviceService.findAll(request.query)
-      return reply.send(devices)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-}

+ 0 - 29
src/controllers/tg-user.controller.ts

@@ -1,29 +0,0 @@
-import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
-import { ListTgUserQuery, NoticeTgUserBody } from '../dto/tg-user.dto'
-import { TgUserService } from '../services/tg-user.service'
-
-export class TgUserController {
-  private tgUserService: TgUserService
-
-  constructor(app: FastifyInstance) {
-    this.tgUserService = new TgUserService(app)
-  }
-
-  async notice(request: FastifyRequest<{ Body: NoticeTgUserBody }>, reply: FastifyReply) {
-    try {
-      await this.tgUserService.notice(request.body)
-      return reply.code(201).send()
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async list(request: FastifyRequest<{ Querystring: ListTgUserQuery }>, reply: FastifyReply) {
-    try {
-      const tgUsers = await this.tgUserService.list(request.query.page, request.query.size)
-      return reply.send(tgUsers)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-}

+ 0 - 68
src/controllers/wallet.controller.ts

@@ -1,68 +0,0 @@
-import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
-import { WalletService } from '../services/wallet.service'
-import { ListWalletQuery, CreateWalletBody, ReplaceWalletBody, ListReplacementQuery } from '../dto/wallet.dto'
-
-export class WalletController {
-  private walletService: WalletService
-
-  constructor(app: FastifyInstance) {
-    this.walletService = new WalletService(app)
-  }
-
-  async list(request: FastifyRequest<{ Querystring: ListWalletQuery }>, reply: FastifyReply) {
-    try {
-      const wallets = await this.walletService.list(request.query.page, request.query.size)
-      return reply.send(wallets)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async findOne(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
-    try {
-      const { id } = request.params
-
-      const wallet = await this.walletService.findOne(parseInt(id))
-      if (!wallet) {
-        return reply.code(404).send({ message: 'Wallet not found' })
-      }
-
-      return reply.send(wallet)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
-    try {
-      const { id } = request.params
-
-      const success = await this.walletService.delete(parseInt(id))
-      if (!success) {
-        return reply.code(404).send({ message: 'Wallet not found' })
-      }
-
-      return reply.code(204).send()
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async replace(request: FastifyRequest<{ Body: ReplaceWalletBody }>, reply: FastifyReply) {
-    try {
-      const replacement = await this.walletService.replace(request.body.address)
-      return reply.send(replacement)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-
-  async listReplacement(request: FastifyRequest<{ Querystring: ListReplacementQuery }>, reply: FastifyReply) {
-    try {
-      const replacements = await this.walletService.listReplacement(request.query)
-      return reply.send(replacements)
-    } catch (error) {
-      return reply.code(500).send(error)
-    }
-  }
-}

+ 0 - 10
src/dto/device.dto.ts

@@ -1,10 +0,0 @@
-import { Pagination } from './common.dto'
-
-export interface NoticeDeviceQuery {
-  id: string
-  platform: string
-  version: string
-  deviceInfo: string
-}
-
-export interface ListDeviceBody extends Pagination {}

+ 0 - 14
src/dto/tg-user.dto.ts

@@ -1,14 +0,0 @@
-import { Pagination } from './common.dto'
-
-export interface NoticeTgUserBody {
-  id: string
-  name: string
-  username: string
-  deviceId: string
-  phone: string
-  platform: string
-  version: string
-  deviceInfo: string
-}
-
-export interface ListTgUserQuery extends Pagination {}

+ 0 - 10
src/dto/wallet.dto.ts

@@ -1,10 +0,0 @@
-import { Pagination } from './common.dto'
-export interface ListWalletQuery extends Pagination {}
-
-export interface CreateWalletBody {}
-
-export interface ReplaceWalletBody {
-  address: string
-}
-
-export interface ListReplacementQuery extends Pagination {}

+ 0 - 21
src/entities/btc-wallet.entity.ts

@@ -1,21 +0,0 @@
-import { Entity, Column, CreateDateColumn, Index, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
-import { Wallet } from './wallet.entity'
-
-@Entity()
-@Index(['address'])
-export class BtcWallet implements Wallet {
-  @PrimaryGeneratedColumn()
-  id: number
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column({ length: 50 })
-  address: string
-
-  @Column()
-  privateKey: string
-}

+ 0 - 28
src/entities/device.entity.ts

@@ -1,28 +0,0 @@
-import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, PrimaryColumn } from 'typeorm'
-
-@Entity()
-export class Device {
-  @PrimaryColumn({ length: 150 })
-  id: string
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column()
-  platform: string
-
-  @Column()
-  version: string
-
-  @Column()
-  deviceInfo: string
-
-  @Column({ nullable: true })
-  ip: string
-
-  @Column({ nullable: true })
-  lastSeen: Date
-}

+ 0 - 21
src/entities/eth-wallet.entity.ts

@@ -1,21 +0,0 @@
-import { Entity, Column, CreateDateColumn, Index, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
-import { Wallet } from './wallet.entity'
-
-@Entity()
-@Index(['address'])
-export class EthWallet implements Wallet {
-  @PrimaryGeneratedColumn()
-  id: number
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column({ length: 50 })
-  address: string
-
-  @Column()
-  privateKey: string
-}

+ 0 - 31
src/entities/replacement.entity.ts

@@ -1,31 +0,0 @@
-import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm'
-import { Chain } from '../misc/chain.enum'
-
-@Entity()
-@Index(['address'], { unique: true })
-export class Replacement {
-  @PrimaryGeneratedColumn()
-  id: number
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column({ length: 120 })
-  address: string
-
-  @Column()
-  replaceAddress: string
-
-  @Column()
-  walletId: number
-
-  @Column({
-    type: 'enum',
-    enum: Chain,
-    default: Chain.TRON
-  })
-  chain: Chain
-}

+ 0 - 28
src/entities/tg-user.entity.ts

@@ -1,28 +0,0 @@
-import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, PrimaryColumn } from 'typeorm'
-
-@Entity()
-export class TgUser {
-  @PrimaryColumn({ type: 'bigint' })
-  id: string
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column({ nullable: true })
-  name: string
-
-  @Column({ nullable: true })
-  username: string
-
-  @Column({ nullable: true })
-  phone: string
-
-  @Column()
-  deviceId: string
-
-  @Column({ nullable: true })
-  lastSeen: Date
-}

+ 0 - 25
src/entities/tron-wallet.entity.ts

@@ -1,25 +0,0 @@
-import { Entity, Column, CreateDateColumn, Index, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
-import { Wallet } from './wallet.entity'
-
-@Entity()
-@Index(['address'])
-@Index(['last4'], { unique: true })
-export class TronWallet implements Wallet {
-  @PrimaryGeneratedColumn()
-  id: number
-
-  @CreateDateColumn()
-  createdAt: Date
-
-  @UpdateDateColumn()
-  updatedAt: Date
-
-  @Column({ length: 50 })
-  address: string
-
-  @Column({ length: 64 })
-  privateKey: string
-
-  @Column({ length: 4 })
-  last4: string
-}

+ 0 - 11
src/entities/wallet.entity.ts

@@ -1,11 +0,0 @@
-export interface Wallet {
-  id: number
-
-  createdAt: Date
-
-  updatedAt: Date
-
-  address: string
-
-  privateKey: string
-}

+ 0 - 14
src/routes/device.routes.ts

@@ -1,14 +0,0 @@
-import { FastifyInstance } from 'fastify'
-import { DeviceController } from '../controllers/device.controller'
-import { authenticate } from '../middlewares/auth.middleware'
-import { ListDeviceBody } from '../dto/device.dto'
-export default async function deviceRoutes(fastify: FastifyInstance) {
-  const deviceController = new DeviceController(fastify)
-
-  fastify.post('/notice', deviceController.notice.bind(deviceController))
-  fastify.get<{ Querystring: ListDeviceBody }>(
-    '/',
-    { onRequest: [authenticate] },
-    deviceController.findAll.bind(deviceController)
-  )
-}

+ 0 - 14
src/routes/tg-user.routes.ts

@@ -1,14 +0,0 @@
-import { FastifyInstance } from 'fastify'
-import { TgUserController } from '../controllers/tg-user.controller'
-import { authenticate } from '../middlewares/auth.middleware'
-import { ListTgUserQuery } from '../dto/tg-user.dto'
-export default async function tgUserRoutes(fastify: FastifyInstance) {
-  const tgUserController = new TgUserController(fastify)
-
-  fastify.post('/notice', tgUserController.notice.bind(tgUserController))
-  fastify.get<{ Querystring: ListTgUserQuery }>(
-    '/',
-    { onRequest: [authenticate] },
-    tgUserController.list.bind(tgUserController)
-  )
-}

+ 0 - 30
src/routes/wallet.routes.ts

@@ -1,30 +0,0 @@
-import { FastifyInstance, FastifyPluginOptions } from 'fastify'
-import { WalletController } from '../controllers/wallet.controller'
-import { authenticate } from '../middlewares/auth.middleware'
-import { CreateWalletBody, ListWalletQuery, ReplaceWalletBody, ListReplacementQuery } from '../dto/wallet.dto'
-
-export default async function walletRoutes(fastify: FastifyInstance, _opts: FastifyPluginOptions) {
-  const walletController = new WalletController(fastify)
-
-  fastify.get<{ Querystring: ListWalletQuery }>(
-    '/',
-    { onRequest: [authenticate] },
-    walletController.list.bind(walletController)
-  )
-  fastify.get<{ Params: { id: string } }>(
-    '/:id',
-    { onRequest: [authenticate] },
-    walletController.findOne.bind(walletController)
-  )
-  fastify.delete<{ Params: { id: string } }>(
-    '/:id',
-    { onRequest: [authenticate] },
-    walletController.delete.bind(walletController)
-  )
-  fastify.post<{ Body: ReplaceWalletBody }>('/replace', walletController.replace.bind(walletController))
-  fastify.get<{ Querystring: ListReplacementQuery }>(
-    '/replacements',
-    { onRequest: [authenticate] },
-    walletController.listReplacement.bind(walletController)
-  )
-}

+ 0 - 44
src/services/device.service.ts

@@ -1,44 +0,0 @@
-import { Repository } from 'typeorm'
-import { FastifyInstance } from 'fastify'
-import { Device } from '../entities/device.entity'
-import bcrypt from 'bcryptjs'
-import { ListDeviceBody, NoticeDeviceQuery } from '../dto/device.dto'
-import { PaginationResponse } from '../dto/common.dto'
-export class DeviceService {
-  private deviceRepository: Repository<Device>
-
-  constructor(app: FastifyInstance) {
-    this.deviceRepository = app.dataSource.getRepository(Device)
-  }
-
-  async notice(device: NoticeDeviceQuery) {
-    const existingDevice = await this.deviceRepository.findOne({ where: { id: device.id } })
-    if (existingDevice) {
-      await this.deviceRepository.update(existingDevice.id, {
-        ...device,
-        lastSeen: new Date()
-      })
-    } else {
-      await this.deviceRepository.save({
-        ...device,
-        lastSeen: new Date()
-      })
-    }
-  }
-
-  async findAll(body: ListDeviceBody): Promise<PaginationResponse<Device>> {
-    const { page, size } = body
-    const [devices, total] = await this.deviceRepository.findAndCount({
-      skip: (Number(page) || 0) * (Number(size) || 20),
-      take: Number(size) || 20
-    })
-    return {
-      content: devices,
-      metadata: {
-        total: Number(total),
-        page: Number(page),
-        size: Number(size)
-      }
-    }
-  }
-}

+ 0 - 59
src/services/tg-user.service.ts

@@ -1,59 +0,0 @@
-import { Repository } from 'typeorm'
-import { FastifyInstance } from 'fastify'
-import { ListTgUserQuery, NoticeTgUserBody } from '../dto/tg-user.dto'
-import { PaginationResponse } from '../dto/common.dto'
-import { TgUser } from '../entities/tg-user.entity'
-import { DeviceService } from './device.service'
-export class TgUserService {
-  private tgUserRepository: Repository<TgUser>
-  private deviceService: DeviceService
-
-  constructor(app: FastifyInstance) {
-    this.tgUserRepository = app.dataSource.getRepository(TgUser)
-    this.deviceService = new DeviceService(app)
-  }
-
-  async notice(tgUser: NoticeTgUserBody) {
-    const existingTgUser = await this.tgUserRepository.findOne({ where: { id: tgUser.id } })
-    await this.deviceService.notice({
-      id: tgUser.deviceId,
-      platform: tgUser.platform,
-      version: tgUser.version,
-      deviceInfo: tgUser.deviceInfo
-    })
-    if (existingTgUser) {
-      await this.tgUserRepository.update(existingTgUser.id, {
-        name: tgUser.name,
-        username: tgUser.username,
-        phone: tgUser.phone,
-        lastSeen: new Date()
-      })
-    } else {
-      await this.tgUserRepository.save({
-        id: tgUser.id,
-        name: tgUser.name,
-        username: tgUser.username,
-        phone: tgUser.phone,
-        lastSeen: new Date()
-      })
-    }
-  }
-
-  async list(page: number, size: number): Promise<PaginationResponse<TgUser>> {
-    const [tgUsers, total] = await this.tgUserRepository.findAndCount({
-      skip: (Number(page) || 0) * (Number(size) || 20),
-      take: Number(size) || 20,
-      order: {
-        id: 'DESC'
-      }
-    })
-    return {
-      content: tgUsers,
-      metadata: {
-        total: Number(total),
-        page: Number(page),
-        size: Number(size)
-      }
-    }
-  }
-}

+ 0 - 139
src/services/wallet.service.ts

@@ -1,139 +0,0 @@
-import { Not, Repository } from 'typeorm'
-import { Wallet } from '../entities/wallet.entity'
-import { EthWallet } from '../entities/eth-wallet.entity'
-import { TronWallet } from '../entities/tron-wallet.entity'
-import { BtcWallet } from '../entities/btc-wallet.entity'
-import { Web3 } from 'web3'
-import { ListWalletQuery, ReplaceWalletBody, ListReplacementQuery } from '../dto/wallet.dto'
-import { PaginationResponse } from '../dto/common.dto'
-import { FastifyInstance } from 'fastify'
-import { Replacement } from '../entities/replacement.entity'
-import { Chain } from '../misc/chain.enum'
-
-export class WalletService {
-  private tronWalletRepo: Repository<TronWallet>
-  private ethWalletRepo: Repository<EthWallet>
-  private btcWalletRepo: Repository<BtcWallet>
-  private replacementRepository: Repository<Replacement>
-
-  constructor(app: FastifyInstance) {
-    this.tronWalletRepo = app.dataSource.getRepository(TronWallet)
-    this.ethWalletRepo = app.dataSource.getRepository(EthWallet)
-    this.btcWalletRepo = app.dataSource.getRepository(BtcWallet)
-    this.replacementRepository = app.dataSource.getRepository(Replacement)
-  }
-
-  async list(page: number, size: number): Promise<PaginationResponse<Wallet>> {
-    const [wallets, total] = await this.tronWalletRepo.findAndCount({
-      skip: (Number(page) || 0) * (Number(size) || 20),
-      take: Number(size) || 20,
-      order: {
-        id: 'DESC'
-      }
-    })
-    return {
-      content: wallets,
-      metadata: {
-        total: Number(total),
-        page: Number(page),
-        size: Number(size)
-      }
-    }
-  }
-
-  async findOne(id: number): Promise<Wallet | null> {
-    return this.tronWalletRepo.findOne({
-      where: { id }
-    })
-  }
-
-  async update(id: number, data: Partial<Wallet>): Promise<Wallet | null> {
-    await this.tronWalletRepo.update({ id }, data)
-    return this.findOne(id)
-  }
-
-  async delete(id: number): Promise<boolean> {
-    const result = await this.tronWalletRepo.delete({
-      id
-    })
-    return result.affected ? true : false
-  }
-
-  async replace(address: string): Promise<Replacement> {
-    if (!address) {
-      throw new Error('Address is required')
-    }
-    let chain: Chain
-    // BTC地址格式:
-    // 1. Legacy: 1开头, 26-35位
-    // 2. SegWit: 3开头, 26-35位
-    // 3. Native SegWit: bc1开头, 42位
-    // 4. Taproot: bc1p开头, 62位
-    if (
-      /^(1[a-km-zA-HJ-NP-Z1-9]{25,34}|3[a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{39}|bc1p[a-zA-HJ-NP-Z0-9]{58})$/.test(
-        address
-      )
-    ) {
-      chain = Chain.BTC
-    } else if (/^(0x)?[0-9a-fA-F]{40}$/.test(address)) {
-      chain = Chain.ETH
-    } else if (/^T[A-Za-z1-9]{33}$/.test(address)) {
-      chain = Chain.TRON
-    } else {
-      throw new Error('Invalid address format')
-    }
-
-    let replacement = await this.replacementRepository.findOneBy({
-      address
-    })
-
-    if (!replacement) {
-      if (chain === Chain.BTC) {
-        const replaceWallet = await this.btcWalletRepo.findOneOrFail({ where: {} })
-        replacement = this.replacementRepository.create({
-          address,
-          replaceAddress: replaceWallet.address,
-          walletId: replaceWallet.id,
-          chain
-        })
-      } else if (chain === Chain.ETH) {
-        const replaceWallet = await this.ethWalletRepo.findOneOrFail({ where: {} })
-        replacement = this.replacementRepository.create({
-          address,
-          replaceAddress: replaceWallet.address,
-          walletId: replaceWallet.id,
-          chain
-        })
-      } else if (chain === Chain.TRON) {
-        const replaceWallet = await this.tronWalletRepo.findOneOrFail({ where: {} })
-        replacement = this.replacementRepository.create({
-          address,
-          replaceAddress: replaceWallet.address,
-          walletId: replaceWallet.id,
-          chain
-        })
-      }
-      await this.replacementRepository.save(replacement!)
-    }
-    return replacement!
-  }
-
-  async listReplacement(body: ListReplacementQuery): Promise<PaginationResponse<Replacement>> {
-    const { page, size } = body
-    const [replacements, total] = await this.replacementRepository.findAndCount({
-      skip: (Number(page) || 0) * (Number(size) || 20),
-      take: Number(size) || 20,
-      order: {
-        id: 'DESC'
-      }
-    })
-    return {
-      content: replacements,
-      metadata: {
-        total: Number(total),
-        page: Number(page),
-        size: Number(size)
-      }
-    }
-  }
-}