diff --git a/packages/toolkits/pro/template/server/nestJs/package.json b/packages/toolkits/pro/template/server/nestJs/package.json index d9fda3f4..ce38d2fe 100644 --- a/packages/toolkits/pro/template/server/nestJs/package.json +++ b/packages/toolkits/pro/template/server/nestJs/package.json @@ -32,6 +32,7 @@ "class-validator": "^0.14.1", "dotenv": "^16.4.5", "ioredis": "^5.4.1", + "mockjs": "^1.1.0", "mysql2": "3.4.3", "nestjs-i18n": "^10.4.5", "nestjs-typeorm-paginate": "^4.0.4", @@ -50,6 +51,7 @@ "@nestjs/testing": "10.0.3", "@types/express": "4.17.17", "@types/jest": "29.5.3", + "@types/mockjs": "^1.0.10", "@types/node": "20.3.3", "@types/supertest": "2.0.12", "@typescript-eslint/eslint-plugin": "5.60.1", @@ -57,6 +59,7 @@ "eslint": "8.42.0", "eslint-config-prettier": "8.8.0", "eslint-plugin-import": "2.27.5", + "express": "^4.21.2", "jest": "29.6.1", "prettier": "2.8.8", "source-map-support": "^0.5.20", diff --git a/packages/toolkits/pro/template/server/nestJs/src/app.module.ts b/packages/toolkits/pro/template/server/nestJs/src/app.module.ts index 36979383..81e7425c 100644 --- a/packages/toolkits/pro/template/server/nestJs/src/app.module.ts +++ b/packages/toolkits/pro/template/server/nestJs/src/app.module.ts @@ -32,6 +32,7 @@ import { HeaderResolver, I18nModule, } from 'nestjs-i18n'; +import { MockModule } from './mock/mock.module'; @Module({ imports: [ @@ -54,6 +55,7 @@ import { resolvers: [new HeaderResolver(['x-lang'])], typesOutputPath: join(__dirname, '../src/.generate/i18n.generated.ts'), }), + MockModule, ], providers: [ { diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/board.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/board.ts new file mode 100644 index 00000000..f6c3ac87 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/board.ts @@ -0,0 +1,98 @@ +import { mock } from 'mockjs'; +import { successResponseWrap } from '../utils'; + +const initData = mock({ + options: [ + { + value: '1', + label: 'work.mock.employees', + }, + { + value: '2', + label: 'work.mock.onboard', + }, + { + value: '3', + label: 'work.mock.Test', + }, + ], +}); + +const initData1 = mock({ + options: [ + { + value: '1', + label: 'work.mock.week1', + }, + { + value: '2', + label: 'work.mock.week2', + }, + { + value: '3', + label: 'work.mock.week3', + }, + ], +}); + +const initData2 = mock({ + options: [ + { + value: '1', + label: 'work.mock.network', + }, + { + value: '2', + label: 'work.mock.centralized', + }, + { + value: '3', + label: 'work.mock.hardware', + }, + ], +}); + +const changeDate = mock({ + options1: [101, 212, 122, 232], + options2: [323, 555, 425, 2221], + options3: [23234, 234, 989, 122], +}); + +export default [ + { + url: '/api/user/getdata', + method: 'get', + response: () => { + return successResponseWrap(initData); + }, + }, + { + url: '/api/user/getrpractic', + method: 'get', + response: () => { + return successResponseWrap(initData1); + }, + }, + { + url: '/api/user/getrtrain', + method: 'get', + response: () => { + return successResponseWrap(initData2); + }, + }, + { + url: '/api/user/getselect', + method: 'post', + response: (data: any) => { + let result = null; + if (data.body === 1) { + result = successResponseWrap(changeDate.options1); + } else if (data.body === 2) { + result = successResponseWrap(changeDate.options2); + } else { + result = successResponseWrap(changeDate.options3); + } + return result; + }, + }, +] as const; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/forms.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/forms.ts new file mode 100644 index 00000000..12d8d0ac --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/forms.ts @@ -0,0 +1,118 @@ +import { mock } from 'mockjs'; +import { successResponseWrap } from '../utils'; + +const initBase = mock({ + Project: [ + 'baseForm.form.label.projectone', + 'baseForm.form.label.projecttwo', + 'baseForm.form.label.projectthree', + ], + rank: [ + { + value: '1', + label: '01', + }, + { + value: '2', + label: '02', + }, + { + value: '3', + label: '03', + }, + { + value: '4', + label: '04', + }, + { + value: '5', + label: '05', + }, + ], + person: [ + { + value: 'local', + label: 'baseForm.form.label.personone', + }, + { + value: 'noemployees', + label: 'baseForm.form.label.persontwo', + }, + { + value: 'chineseemployees', + label: 'baseForm.form.label.personthree', + }, + ], + frequency: [ + 'baseForm.form.label.frequencyone', + 'baseForm.form.label.frequencytwo', + 'baseForm.form.label.frequencythree', + 'baseForm.form.label.frequencyfour', + ], +}); + +const initStep = mock({ + position: [ + { + value: '1', + label: 'position1', + }, + { + value: '2', + label: 'position2', + }, + { + value: '3', + label: 'position3', + }, + { + value: '4', + label: 'position4', + }, + ], + HR: [ + { + value: '1', + label: 'test01', + }, + { + value: '2', + label: 'test01', + }, + { + value: '3', + label: 'test03', + }, + ], + mentor: ['Teacher1', 'Teacher2', 'Teacher3', 'Teacher4'], + director: ['Director1', 'Director2', 'Director3', 'Director4'], +}); + +export default [ + // init-base + { + url: '/api/base/getdata', + method: 'get', + response: () => { + return successResponseWrap(initBase); + }, + }, + + // init-step + { + url: '/api/step/getdata', + method: 'get', + response: () => { + return successResponseWrap(initStep); + }, + }, + + // submit + { + url: '/api/channel-form/submit', + method: 'post', + response: () => { + return successResponseWrap('ok'); + }, + }, +] as any; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/index.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/index.ts new file mode 100644 index 00000000..f56ceef1 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/index.ts @@ -0,0 +1,7 @@ +import board from './board'; +import forms from './forms'; +import list from './list'; +import profile from './profile'; +import user from './user'; + +export default [...board, ...list, ...profile, ...user, ...forms] as const; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/list.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/list.ts new file mode 100644 index 00000000..22841c29 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/list.ts @@ -0,0 +1,50 @@ +import { mock } from 'mockjs'; +import { successResponseWrap } from '../utils'; +const taskList = mock({ + 'list|60': [ + { + id: '@id', + name: 'xiaoming', + rank: '初级', + description: '一段描述文字', + createTime: '@datetime', + 'status|1': ['0', '1', '2'], + type: 'Tiny Design', + roles: '前端', + employeeNo: '00022456', + department: '公共服务', + departmentLevel: '中级', + workbenchName: 'work', + project: 'TinyDesign', + address: '西安研究所', + lastUpdateUser: '张三', + }, + ], +}); + +let treeData = []; + +export default [ + // list + { + url: '/api/employee/getEmployee', + method: 'post', + response: (params: { body: any }) => { + const { pageIndex = 1, pageSize = 10 } = JSON.parse( + JSON.stringify(params.body) + ); + const index = pageIndex as number; + const size = pageSize as number; + const offset = (index - 1) * size; + const count = index * size; + treeData = taskList.list.slice(offset, count); + + const data = mock({ + total: 60, + data: treeData, + }); + + return successResponseWrap(data); + }, + }, +] as const; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/profile.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/profile.ts new file mode 100644 index 00000000..4408a787 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/profile.ts @@ -0,0 +1,64 @@ +import { successResponseWrap } from '../utils'; +import { mock } from 'mockjs'; + +const initData = mock({ + Project: [ + 'baseForm.form.label.projectone', + 'baseForm.form.label.projecttwo', + 'baseForm.form.label.projectthree', + ], + tableData: [ + { + id: '1', + version: 'version1', + operation: 'offline', + updated: 'person1', + time: '2022-10-11', + }, + { + id: '2', + version: 'version2', + operation: 'offline', + updated: 'person2', + time: '2022-10-12', + }, + { + id: '3', + version: 'version3', + operation: 'online', + updated: 'person3', + time: '2022-10-13', + }, + { + id: '4', + version: 'version4', + operation: 'online', + updated: 'person4', + time: '2022-10-14', + }, + { + id: '5', + version: 'version5', + operation: 'online', + updated: 'person5', + time: '2022-10-15', + }, + { + id: '6', + version: 'version6', + operation: 'online', + updated: 'person6', + time: '2022-10-16', + }, + ], +}); + +export default [ + { + url: '/api/detail/getdata', + method: 'get', + response: () => { + return successResponseWrap(initData); + }, + }, +] as const; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/setup.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/setup.ts new file mode 100644 index 00000000..f2353af3 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/setup.ts @@ -0,0 +1,227 @@ +import { mock } from 'mockjs'; + +export const initData = mock({ + chartData: [ + { + title: 'userInfo.week.1', + value: 1, + list: [ + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionA', + len: 1, + bid: 'A', + pid: 'A', + }, + { + type: 'userInfo.type.optionC', + status: 'userInfo.status.optionB', + len: 5, + bid: 'c', + pid: 'B', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionC', + len: 3, + bid: 'A', + pid: 'C', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionA', + len: 1, + bid: 'A', + pid: 'A', + }, + { + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionA', + len: 6, + bid: 'B', + pid: 'A', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionC', + len: 1, + bid: 'A', + pid: 'C', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionA', + len: 1, + bid: 'A', + pid: 'A', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionB', + len: 1, + bid: 'A', + pid: 'B', + }, + { + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionA', + len: 1, + bid: 'B', + pid: 'A', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionC', + len: 1, + bid: 'A', + pid: 'C', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionD', + len: 1, + bid: 'A', + pid: 'D', + }, + { + type: 'userInfo.type.optionC', + status: 'userInfo.status.optionD', + len: 1, + bid: 'C', + pid: 'D', + }, + { + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionD', + len: 1, + bid: 'A', + pid: 'D', + }, + ], + }, + { title: 'userInfo.month.1', value: 0 }, + { title: 'userInfo.month.2', value: 0 }, + { title: 'userInfo.month.3', value: 0 }, + { title: 'userInfo.month.4', value: 0 }, + { title: 'userInfo.month.5', value: 0 }, + { title: 'userInfo.month.6', value: 0 }, + { title: 'userInfo.month.7', value: 0 }, + { title: 'userInfo.month.8', value: 0 }, + { title: 'userInfo.month.9', value: 0 }, + { title: 'userInfo.month.10', value: 0 }, + { title: 'userInfo.month.11', value: 0 }, + { title: 'userInfo.month.12', value: 0 }, + { title: 'userInfo.month.13', value: 0 }, + { title: 'userInfo.month.14', value: 0 }, + { title: 'userInfo.month.15', value: 0 }, + { title: 'userInfo.month.16', value: 0 }, + { title: 'userInfo.month.17', value: 0 }, + ], + tableData: [ + { + id: '1', + bid: 'A', + pid: 'D', + name: 'GFD Company', + time: '2021-12-18', + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionD', + }, + { + id: '2', + bid: 'B', + pid: 'A', + name: 'WWWW Company', + time: '2021-11-18', + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionA', + }, + { + id: '3', + bid: 'C', + pid: 'B', + name: 'TGBYX Company', + time: '2021-10-18', + type: 'userInfo.type.optionC', + status: 'userInfo.status.optionB', + }, + { + id: '4', + bid: 'B', + pid: 'D', + name: 'GF Company', + time: '2021-09-18', + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionC', + }, + { + id: '5', + bid: 'C', + pid: 'C', + name: 'Property management company', + time: '2021-07-18', + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionD', + }, + { + id: '6', + bid: 'A', + pid: 'C', + name: 'Property management company', + time: '2020-12-23', + type: 'userInfo.type.optionA', + status: 'userInfo.status.optionC', + }, + { + id: '7', + bid: 'B', + pid: 'C', + name: 'GF Company', + time: '2020-11-08', + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionC', + }, + { + id: '8', + bid: 'B', + pid: 'C', + name: 'WWWW Company', + time: '2020-10-18', + type: 'userInfo.type.optionB', + status: 'userInfo.status.optionC', + }, + { + id: '9', + bid: 'C', + pid: 'D', + name: 'WWWW Company', + time: '2020-10-11', + type: 'userInfo.type.optionC', + status: 'userInfo.status.optionD', + }, + { + id: '10', + bid: 'C', + pid: 'D', + name: 'TGBYX Company', + time: '2020-06-18', + type: 'userInfo.type.optionC', + status: 'userInfo.status.optionD', + }, + ], + userInfo: { + userId: '10000', + username: 'admin', + department: 'Tiny-Vue-Pro', + employeeType: 'social recruitment', + role: 'admin', + job: 'Front end', + probationStart: '2021-04-19', + probationEnd: '2021-10-15', + probationDuration: '180', + protocolStart: '2021-04-19', + protocolEnd: '2024-04-19', + address: 'xian', + status: 'normal', + }, +}); diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/data/user.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/data/user.ts new file mode 100644 index 00000000..6be03b53 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/data/user.ts @@ -0,0 +1,60 @@ +import { successResponseWrap, successResponseWrapper } from '../utils'; +import { initData } from './setup'; + +const positive = JSON.parse(JSON.stringify(initData.tableData)); +const negative = JSON.parse(JSON.stringify(initData.tableData.reverse())); +const initlist = JSON.parse(JSON.stringify(initData.chartData[0].list)); +const userInfo = JSON.parse(JSON.stringify(initData.userInfo)); + +export default [ + { + url: '/api/user/data', + method: 'post', + response: (params: any) => { + const { sort, startTime, endTime, filterStatus, filterType } = JSON.parse( + JSON.stringify(params.body) + ); + initData.tableData = positive; + initData.chartData[0].list = initlist; + if (sort === 1 || sort === 3) { + initData.chartData[0].list.reverse(); + initData.tableData = positive; + return successResponseWrap(initData); + } + if (sort === 2 || sort === 4) { + initData.chartData[0].list.reverse(); + initData.tableData = negative; + return successResponseWrap(initData); + } + if ( + startTime !== '' || + endTime !== '' || + filterStatus.length !== 0 || + (filterType.length !== 0 && sort === undefined) + ) { + const start = new Date(JSON.parse(JSON.stringify(startTime))).getTime(); + const end = new Date(JSON.parse(JSON.stringify(endTime))).getTime(); + // eslint-disable-next-line func-names + const table = initData.tableData.filter(function (item: any) { + return ( + filterType.includes(item.bid) && + filterStatus.includes(item.pid) && + new Date(JSON.parse(JSON.stringify(item.time))).getTime() - start > + 0 && + new Date(JSON.parse(JSON.stringify(item.time))).getTime() - end < 0 + ); + }); + // eslint-disable-next-line func-names + const chart = initData.chartData[0].list.filter(function (item: any) { + return ( + filterType.includes(item.bid) && filterStatus.includes(item.pid) + ); + }); + initData.tableData = table; + initData.chartData[0].list = chart; + return successResponseWrap(initData); + } + return successResponseWrap(initData); + }, + }, +] as const; diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/mock.controller.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.controller.ts new file mode 100644 index 00000000..5c195717 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.controller.ts @@ -0,0 +1,43 @@ +import { + Controller, + Get, + HttpException, + HttpStatus, + Param, + Post, + Req, +} from '@nestjs/common'; +import { MockService } from './mock.service'; +import { Public } from '../public/public.decorator'; +import { MockDto } from './mock.dto'; +import { Request } from 'express'; +import data from './data'; + +@Controller('mock') +export class MockController { + constructor(private readonly mockService: MockService) {} + + @Get('*') + async getMock(@Req() req: Request) { + const path = req.path.replace('/mock', ''); + const item = data.filter( + (dataItem) => dataItem.method === 'get' && dataItem.url === path + ); + if (!item.length) { + throw new HttpException('not found', HttpStatus.NOT_FOUND); + } + return item[0].response({ body: null }); + } + + @Post('*') + async postMock(@Req() req: Request) { + const path = req.path.replace('/mock', ''); + const item = data.filter( + (dataItem) => dataItem.method === 'post' && dataItem.url === path + ); + if (!item.length) { + throw new HttpException('not found', HttpStatus.NOT_FOUND); + } + return item[0].response({ body: req.body }); + } +} diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/mock.dto.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.dto.ts new file mode 100644 index 00000000..3a77262f --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.dto.ts @@ -0,0 +1,6 @@ +import { Matches } from 'class-validator'; + +export class MockDto { + @Matches(/.*/) + path: string; +} diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/mock.module.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.module.ts new file mode 100644 index 00000000..d94c724c --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { MockService } from './mock.service'; +import { MockController } from './mock.controller'; + +@Module({ + controllers: [MockController], + providers: [MockService], +}) +export class MockModule {} diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/mock.service.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.service.ts new file mode 100644 index 00000000..be140f36 --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/mock.service.ts @@ -0,0 +1,4 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class MockService {} diff --git a/packages/toolkits/pro/template/server/nestJs/src/mock/utils.ts b/packages/toolkits/pro/template/server/nestJs/src/mock/utils.ts new file mode 100644 index 00000000..6c72a33a --- /dev/null +++ b/packages/toolkits/pro/template/server/nestJs/src/mock/utils.ts @@ -0,0 +1,41 @@ +export const successResponseWrapper = (data: unknown) => { + return { + data, + status: 'ok', + msg: '请求成功', + code: 20000, + }; +}; + +export const failResponseWrapper = ( + data: unknown, + msg: string, + code = 50000 +) => { + return { + data, + status: 'fail', + msg, + code, + }; +}; + +export const successResponseWrap = (data: unknown) => { + return { + data, + errMsg: '', + code: '0', + }; +}; + +export const failResponseWrap = ( + data: unknown, + errMsg: string, + code = '500' +) => { + return { + data, + errMsg, + code, + }; +}; diff --git a/packages/toolkits/pro/template/tinyvue/config/vite.config.dev.ts b/packages/toolkits/pro/template/tinyvue/config/vite.config.dev.ts index f6b5f207..a3529115 100644 --- a/packages/toolkits/pro/template/tinyvue/config/vite.config.dev.ts +++ b/packages/toolkits/pro/template/tinyvue/config/vite.config.dev.ts @@ -10,14 +10,14 @@ const proxyConfig = { rewrite: (path: string) => path.replace( new RegExp(`${loadEnv('', process.cwd()).VITE_BASE_API}`), - '' + '', ), }, [loadEnv('', process.cwd()).VITE_MOCK_SERVER_HOST]: { - target: loadEnv('', process.cwd()).VITE_MOCK_HOST, + target: loadEnv('', process.cwd()).VITE_SERVER_HOST, changeOrigin: true, - rewrite: (path:string) => path.replace(/^\/mock/, '') - } + rewrite: (path: string) => path.replace(/^\/mock/, '/mock'), + }, }; export default mergeConfig( { @@ -38,5 +38,5 @@ export default mergeConfig( }), ], }, - baseConfig + baseConfig, ); diff --git a/packages/toolkits/pro/template/tinyvue/farm.config.ts b/packages/toolkits/pro/template/tinyvue/farm.config.ts index 39fdf580..dd8b4676 100644 --- a/packages/toolkits/pro/template/tinyvue/farm.config.ts +++ b/packages/toolkits/pro/template/tinyvue/farm.config.ts @@ -45,9 +45,9 @@ export default defineConfig({ return Boolean(pathname.match('^/mock')); }, pathRewrite: { - '^/mock': '', + '^/mock': '/mock', }, - target: 'http://localhost:8848', + target: 'http://localhost:3000', logger: console, }, '/api': { diff --git a/packages/toolkits/pro/template/tinyvue/rspack.config.js b/packages/toolkits/pro/template/tinyvue/rspack.config.js index fc0497a9..2f30d65c 100644 --- a/packages/toolkits/pro/template/tinyvue/rspack.config.js +++ b/packages/toolkits/pro/template/tinyvue/rspack.config.js @@ -53,11 +53,11 @@ const config = { }, }, { - context: [process.env.VITE_MOCK_SERVER_HOST], - target: process.env.VITE_MOCK_HOST, + context: ['/mock'], + target: process.env.VITE_SERVER_HOST, changeOrigin: true, pathRewrite: { - '^/mock': '', + '^/mock': '/mock', }, }, ], diff --git a/packages/toolkits/pro/template/tinyvue/webpack.config.js b/packages/toolkits/pro/template/tinyvue/webpack.config.js index fbdada6f..28cd6516 100644 --- a/packages/toolkits/pro/template/tinyvue/webpack.config.js +++ b/packages/toolkits/pro/template/tinyvue/webpack.config.js @@ -151,11 +151,11 @@ module.exports = { }, }, { - context: [process.env.VITE_MOCK_SERVER_HOST], - target: process.env.VITE_MOCK_HOST, + context: ['/mock'], + target: process.env.VITE_SERVER_HOST, changeOrigin: true, pathRewrite: { - '^/mock': '', + '^/mock': '/mock', }, }, ],