From 9e7de0451d5e9e393ba72d63c612fe4a69a02cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98chen=2Ehome=E2=80=99?= <1147347984@qq.com> Date: Sun, 7 Aug 2022 22:46:13 +0800 Subject: [PATCH] =?UTF-8?q?chore(projects):=20=E5=A2=9E=E5=8A=A0=E4=BA=86m?= =?UTF-8?q?ock=E6=95=B0=E6=8D=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/plugins/html.ts | 2 +- build/plugins/index.ts | 5 +-- build/plugins/mock.ts | 7 +++++ mock/index.ts | 6 ++++ mock/module/index.ts | 3 ++ mock/module/user.ts | 65 +++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/service/api/login.ts | 5 +++ src/service/http/index.ts | 3 +- src/views/test/test1.vue | 8 ++++- tsconfig.json | 44 ++++++++------------------ visualizer.ts | 6 ++++ 12 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 build/plugins/mock.ts create mode 100644 mock/index.ts create mode 100644 mock/module/index.ts create mode 100644 mock/module/user.ts create mode 100644 visualizer.ts diff --git a/build/plugins/html.ts b/build/plugins/html.ts index db3f5b1..6a50eae 100644 --- a/build/plugins/html.ts +++ b/build/plugins/html.ts @@ -1,6 +1,6 @@ import { createHtmlPlugin } from 'vite-plugin-html'; // https://github.com/vbenjs/vite-plugin-html/blob/main/README.zh_CN.md -export default (env) => { +export default (env: ImportMetaEnv) => { return createHtmlPlugin({ minify: true, // 压缩HTML inject: { diff --git a/build/plugins/index.ts b/build/plugins/index.ts index 92b67db..54ccdd6 100644 --- a/build/plugins/index.ts +++ b/build/plugins/index.ts @@ -2,8 +2,9 @@ import vue from './vue'; import compress from './compress'; import html from './html'; import unocss from './unocss'; -import visualizer from './visualizer'; +import visualizer from '../../visualizer'; import unplugin from './unplugin'; +import mock from './mock'; /** * @description: 设置vite插件配置 @@ -11,7 +12,7 @@ import unplugin from './unplugin'; * @return {*} */ export function setVitePlugins(env: ImportMetaEnv) { - const plugins = [...vue, html(env), unocss, ...unplugin]; + const plugins = [...vue, html(env), unocss, ...unplugin, mock]; // 是否压缩 if (env.VITE_COMPRESS_OPEN === 'Y') { plugins.push(compress(env)); diff --git a/build/plugins/mock.ts b/build/plugins/mock.ts new file mode 100644 index 0000000..c4be5eb --- /dev/null +++ b/build/plugins/mock.ts @@ -0,0 +1,7 @@ +import { viteMockServe } from 'vite-plugin-mock'; + +export default viteMockServe({ + mockPath: 'mock', + watchFiles: true, // 监视文件更改 + // localEnabled: command === 'serve', +}); diff --git a/mock/index.ts b/mock/index.ts new file mode 100644 index 0000000..c64c9d1 --- /dev/null +++ b/mock/index.ts @@ -0,0 +1,6 @@ +import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'; +import api from './module'; + +export function setupMockServer() { + createProdMockServer(api); +} diff --git a/mock/module/index.ts b/mock/module/index.ts new file mode 100644 index 0000000..7ed1734 --- /dev/null +++ b/mock/module/index.ts @@ -0,0 +1,3 @@ +import user from './user'; + +export default [...user]; diff --git a/mock/module/user.ts b/mock/module/user.ts new file mode 100644 index 0000000..47b1c24 --- /dev/null +++ b/mock/module/user.ts @@ -0,0 +1,65 @@ +import Mock from 'mockjs'; +function resultSuccess(data: any, { msg = 'success' } = {}) { + return Mock.mock({ + code: 200, + data, + msg, + }); +} + +const Random = Mock.Random; + +const token = Random.string('upper', 32, 32); + +const adminInfo = { + userId: '1', + username: 'admin', + realName: 'Admin', + avatar: Random.image(), + desc: 'manager', + password: Random.string('upper', 4, 16), + token, + permissions: [ + { + label: '主控台', + value: 'dashboard_console', + }, + { + label: '监控页', + value: 'dashboard_monitor', + }, + { + label: '工作台', + value: 'dashboard_workplace', + }, + { + label: '基础列表', + value: 'basic_list', + }, + { + label: '基础列表删除', + value: 'basic_list_delete', + }, + ], +}; + +export default [ + { + url: '/mock/login', + timeout: 1000, + method: 'post', + response: () => { + return resultSuccess({ token }); + }, + }, + { + url: '/mock/admin_info', + timeout: 1000, + method: 'get', + response: () => { + // const token = getRequestToken(request); + // if (!token) return resultError('Invalid token'); + return resultSuccess(adminInfo); + }, + }, +]; diff --git a/package.json b/package.json index 825eb4d..52516d3 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "eslint-plugin-vue": "^9.3.0", "husky": "^8.0.0", "lint-staged": "^13.0.3", + "mockjs": "^1.1.0", "naive-ui": "^2.32.1", "prettier": "^2.7.1", "rollup-plugin-visualizer": "^5.7.1", @@ -58,6 +59,7 @@ "vite": "^3.0.0", "vite-plugin-compression": "^0.5.1", "vite-plugin-html": "^3.2.0", + "vite-plugin-mock": "^2.9.6", "vue-tsc": "^0.38.4" } } diff --git a/src/service/api/login.ts b/src/service/api/login.ts index 58c2efd..0dff09a 100644 --- a/src/service/api/login.ts +++ b/src/service/api/login.ts @@ -1,4 +1,5 @@ import { request } from '../http'; +import { mockRequest } from '../http'; interface Itest { data: string; @@ -23,3 +24,7 @@ export function fetachPut(params: Itest) { export function fetachPatch(params: Itest) { return request.patch('/patchAPI', params); } +/* mock方法测试 */ +export function fetchMock() { + return mockRequest.post('/login'); +} diff --git a/src/service/http/index.ts b/src/service/http/index.ts index 8cf2169..a50fafb 100644 --- a/src/service/http/index.ts +++ b/src/service/http/index.ts @@ -1,10 +1,11 @@ import { getServiceEnvConfig } from '~/.env-config'; import { createRequest } from './request'; -const { url, urlPattern, secondUrl, secondUrlPattern } = getServiceEnvConfig(import.meta.env); +const { url, urlPattern } = getServiceEnvConfig(import.meta.env); const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y'; export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url }); // export const secondRequest = createRequest({ baseURL: isHttpProxy ? secondUrlPattern : secondUrl }); +export const mockRequest = createRequest({ baseURL: '/mock' }); diff --git a/src/views/test/test1.vue b/src/views/test/test1.vue index b79a171..3decd09 100644 --- a/src/views/test/test1.vue +++ b/src/views/test/test1.vue @@ -7,11 +7,12 @@