This commit is contained in:
weiyida 2025-02-23 20:34:16 +08:00
commit a152600c30
12 changed files with 133 additions and 89 deletions

5
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"recommendations": [
"marscode.marscode-extension"
]
}

7
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"marscode.chatLanguage": "cn",
"marscode.codeCompletionPro": {
"enableCodeCompletionPro": true
},
"marscode.enableInlineCommand": true
}

View File

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

View File

@ -1,17 +1,20 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Query, Param } from '@nestjs/common';
@Controller('goods')
export class GoodsController {
constructor() {
console.log('goods Controller');
console.log('goods controller')
}
// 示例1基本查询参数
@Get('hello')
getHello(): string {
return 'Hello World!';
getHello(@Query('name') name?: string) {
return {
message: 'Hello World!',
name: name || 'Guest'
};
}
// 示例2路径参数
@Get('detail/:id')
getDetail(@Param('id') id: string) {
@ -36,4 +39,3 @@ export class GoodsController {
};
}
}

View File

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

View File

@ -0,0 +1,6 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class GoodsService {
}

View File

@ -3,7 +3,6 @@ import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { MessageService } from './message.service';
import { AuthGuard } from '@server/auth/auth.guard';
import { db, VisitType } from '@nice/common';
@Controller('message')
export class MessageController {
constructor(private readonly messageService: MessageService) { }

View File

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

View File

@ -0,0 +1,51 @@
import { useEffect } from 'react';
import { api } from '@nice/client';
function People() {
// 使用 useQuery 钩子从 API 获取数据,限制查询结果数量为 10
const { data } = api.staff.findMany.useQuery({
take: 10, // 限制查询结果数量
});
// 当 data 发生变化时,打印数据到控制台
useEffect(() => {
console.log(data);
}, [data]);
return (
<div>
{
// 遍历 data 并渲染每个元素
data?.map((i) => {
return (
<table className='table-auto w-full'>
<thead>
<tr>
<th className='px-4 py-2'>Username</th>
<th className='px-4 py-2'>ID</th>
<th className='px-4 py-2'>Showname</th>
<th className='px-4 py-2'>Password</th>
<th className='px-4 py-2'>Phone Number</th>
<th className='px-4 py-2'>Order</th>
</tr>
</thead>
<tbody>
{data?.map((i) => (
<tr key={i.id} className='hover:bg-gray-100'>
<td className='border px-4 py-2'>{i.username}</td>
<td className='border px-4 py-2'>{i.id}</td>
<td className='border px-4 py-2'>{i.showname}</td>
<td className='border px-4 py-2'>{i.password}</td>
<td className='border px-4 py-2'>{i.phoneNumber}</td>
<td className='border px-4 py-2'>{i.order}</td>
</tr>
))}
</tbody>
</table>
);
})
}
</div>
);
}
export default People;

View File

@ -1,80 +1,45 @@
// import { api, useStaff } from "@nice/client"
// //import { useAuth } from "@web/src/providers/auth-provider";
// import { Button, Tag } from "antd";
// import { useEffect, useMemo, useState } from "react";
// import PersonCard from './personcard';
// function HomePage(){
// // function HomePage(){
// // const {data} = api.staff.findMany.useQuery({
// // take: 5
// // })
// // //const {user,}=useAuth()
// // //const {update,create}=useStaff()
// // const [counter, setCounter] = useState(0)
// // const counterText=useMemo(()=>{
// // return `当前计数为:${counter}`
// // },[counter])
// // const test=async ()=>{
// // await update.mutateAsync({
// // where: {
// // username: user?.username
// // },
// // data: {
// // username: "test"
// // }
// // })
// // }
// // useEffect(()=>{
// // console.log(data)
// // },[data])
// return (
// <div>
// <h1>主页</h1>
// {/* 调用 PersonCard 组件 */}
// <PersonCard />
// </div>
// );
// };
// // return <div className="p-2 space-y-2">
// // <Tag>当前计数为:{counter}</Tag>
// // <div className="p-z space-x-2">
// // <Button type="primary" onClick={()=>{setCounter(counter + 1)}}>加1</Button>
// // <Button danger onClick={()=>{setCounter(counter - 1)}}>减1</Button>
// // <div/>
// export default HomePage;
// // export {HomePage};//main-route那里也需要加括号
import React, { useEffect } from 'react';
import { api } from "@nice/client"
import { apiClient } from "@web/src/utils"
import { Button, Tag } from "antd"
import { useEffect, useMemo, useState } from "react"
function PersonCard(){
const {data} = api.staff.findMany.useQuery({
take: 15
function HomePage() {
const { data } = api.staff.findMany.useQuery({
take: 10
})
const [counter, setCounter] = useState<number>(0)
const counterText = useMemo(() => {
return `当前计数为:${counter}`
}, [counter])
useEffect(()=>{
console.log(data)
},[data])
const getData = async () => {
const res = await apiClient.get("/goods/hello")
console.log(res)
}
useEffect(() => {
getData()
}, [])
return <div className="p-2 space-y-2">
<Tag>{counterText}</Tag>
<div className="space-x-2" >
<Button type="primary" onClick={() => {
setCounter(counter + 1)
}}>1</Button>
<Button danger
onClick={() => {
setCounter(counter - 1)
}}
>1</Button>
</div>
return (
<div>
{data?.map((i, index) => (
<div key={index} className="bg-blue rounded-lg shadow-sm p-6 hover:shadow-md transition-shadow duration-300">
<div className="flex items-center">
<h3 className="text-lg font-semibold text-gray-900">{i.username}</h3>
{
data?.map(i => {
return <div className="p-2 rounded border shadow">
<Tag>{i.username}</Tag>
</div>
})
}
</div>
))}
</div>
);
}
export default PersonCard;
// export { HomePage }
export default HomePage

View File

@ -330,6 +330,8 @@ model Resource {
@@map("resource")
}
//商品表
model Goods {
id String @id @default(cuid()) // 商品ID