This commit is contained in:
Li1304553726 2025-02-23 20:26:05 +08:00
commit ec1e352102
7 changed files with 70 additions and 52 deletions

View File

@ -4,19 +4,27 @@ import { TrpcRouter } from './trpc/trpc.router';
import { WebSocketService } from './socket/websocket.service'; import { WebSocketService } from './socket/websocket.service';
async function bootstrap() { async function bootstrap() {
// 创建NestJS应用实例
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
// 启用 CORS 并允许所有来源 // 启用 CORS 并允许所有来源
app.enableCors({ app.enableCors({
origin: '*', origin: '*',
}); });
// 获取WebSocket服务实例并初始化
const wsService = app.get(WebSocketService); const wsService = app.get(WebSocketService);
await wsService.initialize(app.getHttpServer()); await wsService.initialize(app.getHttpServer());
// 获取tRPC路由实例并应用中间件
const trpc = app.get(TrpcRouter); const trpc = app.get(TrpcRouter);
trpc.applyMiddleware(app); trpc.applyMiddleware(app);
// 设置服务器端口优先使用环境变量中的值默认3000
const port = process.env.SERVER_PORT || 3000; const port = process.env.SERVER_PORT || 3000;
// 启动应用,监听指定端口
await app.listen(port); await app.listen(port);
} }
// 启动应用
bootstrap(); bootstrap();

View File

@ -1,45 +1,41 @@
import { Controller, Get, Query, Param } from '@nestjs/common'; import { Controller, Get, Query, Param } from '@nestjs/common';
// 定义商品相关的控制器,路由前缀为 /goods
@Controller('goods') @Controller('goods')
export class GoodsController { export class GoodsController {
// 构造函数,在控制器实例化时执行
constructor() { constructor() {
console.log('goods controller') // 打印日志,用于调试 console.log('goods controller')
} }
// 示例1基本查询参数 // 示例1基本查询参数
// GET /goods/hello?name=xxx
@Get('hello') @Get('hello')
getHello(@Query('name') name?: string) { getHello(@Query('name') name?: string) {
return { return {
message: 'Hello World!', // 固定返回消息 message: 'Hello World!',
name: name || 'Guest' // 返回传入的name参数如果未传入则返回'Guest' name: name || 'Guest'
}; };
} }
// 示例2路径参数 // 示例2路径参数
// GET /goods/detail/123
@Get('detail/:id') @Get('detail/:id')
getDetail(@Param('id') id: string) { getDetail(@Param('id') id: string) {
return { return {
id: id, // 返回路径参数中的id id: id,
detail: `Detail for product ${id}` // 返回包含id的详细信息 detail: `Detail for product ${id}`
}; };
} }
// 示例3多个查询参数 // 示例3多个查询参数
// GET /goods/search?keyword=xxx&page=2&limit=20
@Get('search') @Get('search')
searchProducts( searchProducts(
@Query('keyword') keyword: string, // 搜索关键词 @Query('keyword') keyword: string,
@Query('page') page: number = 1, // 页码默认值为1 @Query('page') page: number = 1,
@Query('limit') limit: number = 10 // 每页数量默认值为10 @Query('limit') limit: number = 10
) { ) {
return { return {
keyword, // 返回搜索关键词 keyword,
page, // 返回当前页码 page,
limit, // 返回每页数量 limit,
results: [] // 返回搜索结果(示例中为空数组) results: []
}; };
} }
} }

View File

@ -1,8 +1,9 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { GoodsService } from './goods.service'; import { GoodsService } from './goods.service';
import { GoodsController } from './goods.controller'; import { GoodsController } from './goods.controller';
@Module({ @Module({
providers: [GoodsService], providers: [GoodsService],
controllers: [GoodsController] controllers: [GoodsController],
}) })
export class GoodsModule {} export class GoodsModule {}

View File

@ -3,4 +3,4 @@ import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class GoodsService { export class GoodsService {
} }

View File

@ -39,4 +39,4 @@ import { GoodsModule } from '@server/models/goods/goods.module';
controllers: [], controllers: [],
providers: [TrpcService, TrpcRouter, Logger], providers: [TrpcService, TrpcRouter, Logger],
}) })
export class TrpcModule {} export class TrpcModule { }

View File

@ -1,34 +1,45 @@
import { api } from '@nice/client'; import { api } from "@nice/client"
import { useState, useEffect, useMemo } from 'react'; import { apiClient } from "@web/src/utils"
import { Button } from 'antd'; import { Button, Tag } from "antd"
import People from '../component/People'; import { useEffect, useMemo, useState } from "react"
import { apiClient } from '@web/src/utils';
// 主页入口组件,负责渲染首页内容
function HomePage() { function HomePage() {
const { data } = api.staff.findMany.useQuery({
const [counter, setCounter] = useState<number>(0); take: 10
})
const [counter, setCounter] = useState<number>(0)
const counterText = useMemo(() => { const counterText = useMemo(() => {
return `当前值:${counter}` return `当前计数为:${counter}`
}, [counter]); }, [counter])
const getData = async () => {
const res = await apiClient.get('/goods/hello', {
}); const getData = async () => {
const res = await apiClient.get("/goods/hello")
console.log(res)
}
useEffect(() => { useEffect(() => {
getData(); getData()
},[]) }, [])
console.log(res); return <div className="p-2 space-y-2">
return ( <Tag>{counterText}</Tag>
<div className='p-2 space-y-2'> <div className="space-x-2" >
<div className='flex space-x-2'> <Button type="primary" onClick={() => {
<Button type="primary" onClick={() => { setCounter(counter + 1) }}>+1</Button> setCounter(counter + 1)
<div className='flex items-center'>{counterText}</div> }}>1</Button>
<Button danger onClick={() => { setCounter(counter - 1) }}>-1</Button> <Button danger
</div> onClick={() => {
<People></People> setCounter(counter - 1)
}}
>1</Button>
</div> </div>
);
{
data?.map(i => {
return <div className="p-2 rounded border shadow">
<Tag>{i.username}</Tag>
</div>
})
}
</div>
} }
} // export { HomePage }
//export {HomePage}; MainRoute也要+{} export default HomePage
export default HomePage;

View File

@ -330,12 +330,14 @@ model Resource {
@@map("resource") @@map("resource")
} }
//商品表 //商品表
model Goods { model Goods {
id String @id @default(cuid()) // 商品ID id String @id @default(cuid()) // 商品ID
name String @unique // 商品名称 name String @unique // 商品名称
description String? // 商品描述 description String? // 商品描述
price Float @default(0.0)// 商品价格 price Float @default(0.0) // 商品价格
images String[] @default([]) // 商品图片 images String[] @default([]) // 商品图片
tags Tag[] @relation("GoodsTags") // 多对多关系 tags Tag[] @relation("GoodsTags") // 多对多关系
reviews Review[] // 一对多关系 reviews Review[] // 一对多关系