前端手册
前端手册
技术栈
| 分类 | 选型 |
|---|---|
| 框架 | Vue 3.5 + Vite 6 + script setup |
| UI | Element Plus 2.14 + @element-plus/icons-vue |
| 状态 | Pinia 2.3 + pinia-plugin-persistedstate |
| 路由 | Vue Router 4.5(动态路由 + 固定路由) |
| 请求 | axios 1.6(统一封装,见请求封装) |
| 国际化 | vue-i18n 9.2 |
| 实时通信 | @microsoft/signalr 8(见实时消息) |
| 图表 / 表格 | ECharts 5.2、vxe-table 4.9 |
| 编辑器 | md-editor-v3(Markdown)、@wangeditor/editor(富文本) |
| 其他 | @vue-office(Word/Excel 预览)、vue-cropper、jsencrypt、crypto-js |
Node 建议 ≥ 18(Vite 6 要求)。常用命令:
npm install
npm run dev # 本地开发
npm run build:prod # 生产构建(.env.production)
npm run build:stage # 预发布构建(.env.staging)
npm run preview # 预览构建产物
提交前会走 husky + commitlint(gitmoji 规范),提交信息形如 ✨ feat: xxx。
项目结构
查看项目目录
├─src
│ ├─api # 接口定义,按后端模块分目录
│ │ ├─ai article business content demo monitor public shopping
│ │ ├─system # 用户/角色/菜单/租户/2FA/三方账号…(dict 子目录)
│ │ └─tool # file / filegroup / gen
│ ├─assets # 静态资源(images / icons / styles / logo / iconfont)
│ ├─components # 全局组件(见「内置组件」)
│ ├─composables # 组合式函数(useTable / useAiDialog / useAiStream)
│ ├─constants # 常量(navigation.js)
│ ├─directive # 自定义指令(permission / module)
│ ├─i18n # 多语言:lang 静态语言包 + pages 页面级翻译
│ ├─layout # 布局骨架 index.vue + components(Sidebar/TagsView/Settings…)
│ ├─plugins # 全局方法:tab / modal / download / cache / auth
│ ├─router # index.js:固定路由;动态路由由后端菜单生成
│ ├─signalr # 实时通信(signalr.js 连接 + analysis.js 消息分发)
│ ├─store # Pinia 模块(见[状态管理](./state.md))
│ ├─utils # 工具(request / auth / dict / validate / webauthn …)
│ └─views # 页面
│ ├─Ai # AI 对话、用量、治理
│ ├─article dashboard dataScreen demo error public redirect
│ ├─monitor # cache / job / logininfor / onlineuser / operlog / server
│ ├─order # 商城订单(列表 / 详情 / 销售看板)
│ ├─shopping # 商城后台(商品/SKU/品牌/分类/售后/会员/支付)与 C 端前台
│ ├─system # config / dept / dict / menu / notice / oauth / post / role
│ │ # tenant / tenantPlan / user(含 profile)/ dailyschedule / commonLang
│ └─tool # build / email / file / gen / swagger
├─vite.config.js # 构建配置(别名、代理、分包、压缩)
├─index.html
└─package.json
约定:一个页面一个文件夹,模块自己的组件/工具放在模块目录下;全局复用的才放 src/components、src/utils。
系统配置
src/settings.js:
export default {
version: '20260725', // 框架版本号
title: import.meta.env.VITE_APP_TITLE,
sideTheme: '', // '' 浅色;theme-black / theme-dark / theme-other 深色
theme: '#409EFF', // 主题色
layoutSkin: 'default', // default 默认;premium 高级皮肤
showSettings: false, // 是否显示右侧布局配置抽屉
topNav: false, // 是否显示顶部导航
tagsView: true, // 是否显示页签
tagsViewStyle: 'card', // card 卡片;chrome 浏览器标签风格
tagsViewPersist: false, // 页签是否持久化(刷新后恢复)
tagsShowIcon: true, // 页签是否显示图标
fixedHeader: false,
sidebarLogo: true,
dynamicTitle: false, // 标题是否跟随页面变化
errorLog: 'production', // 错误日志页在哪些环境显示
copyright: 'Copyright ©2026 ...',
showFooter: true,
showWatermark: false, // 是否显示水印
watermarkText: 'ZRAdmin.NET',
showOtherLogin: true, // 登录页是否显示第三方登录
showQrLogin: true, // 登录页是否显示扫码登录
showPhoneLogin: true, // 登录页是否显示手机号登录
defaultSize: 'default', // large / default / small
defaultLang: 'zh-cn',
menuShowNew: false, // 菜单是否显示 New 标记(7 天内新增)
navType: 1, // 1 侧边栏;2 混合;3 纯顶部;4 侧边混合
noticeUrl: 'https://gitee.com/izory/ZrAdminNetCore', // 通知/公告跳转地址
tokenSaveType: 'localStorage' // localStorage | sessionStorage
}
navType决定整体布局形态,改动后侧边栏/顶栏结构会随之变化。
环境变量
| 文件 | 用途 | 关键变量 |
|---|---|---|
.env.development | 本地开发 | VITE_APP_BASE_API=/dev-api、VITE_APP_API_HOST=http://localhost:8888 |
.env.production | 生产 | VITE_APP_BASE_API=/prod-api、VITE_APP_SOCKET_API=/msghub |
.env.staging | 预发布 | 同上,按需改 |
变量必须以 VITE_APP 开头才会注入前端,代码中用 import.meta.env.VITE_APP_BASE_API 读取;打包后不能改,换环境要重新构建。
代理、分包、压缩等构建细节见构建与部署。
请求流程
所有接口都走 src/utils/request.js 封装的 axios 实例:
import request from '@/utils/request'
export function listUser(query) {
return request({ url: '/system/user/list', method: 'get', params: query })
}
拦截器已统一处理:Bearer token、userid/userName 头、tenantId 租户头、Accept-Language 语言头、x-refresh-token 静默续期、业务码判定、错误提示聚合、文件下载。
- 只有
code === 200视为成功;203原样返回给登录页走双因子第二步;401弹重新登录;其余非 200 统一报错; - 错误提示用
ElNotification聚合(相同文案累加次数,同屏最多 3 条),不会刷屏。
完整机制、错误码表、上传下载封装见请求封装。
全局方法与组件
- 全局方法:
proxy.getDicts、proxy.getConfigKey、proxy.download、proxy.downFile、proxy.parseTime、proxy.resetForm、proxy.handleTree、proxy.addDateRange、proxy.selectDictLabel、proxy.dateOptions、proxy.signalr - 全局组件:
DictTag、Pagination、UploadFile、UploadImage、ImagePreview、ImageSelector、RightToolbar、svg-icon、ZrDialog、ZrPrint、ZrPrintQr
script setup 中用法:
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
路由使用
固定路由在 src/router/index.js,菜单对应的业务路由由后端菜单树动态生成。写法、meta 字段、权限指令见路由使用。
开发规范
新增 view
在 src/views 下按模块建目录,一个路由一个页面文件:
src/views/system/config/index.vue
新增 api
在 src/api 下建同名模块文件,例如 src/api/system/config.js:
import request from '@/utils/request'
export function listConfig(query) {
return request({ url: '/system/config/list', method: 'get', params: query })
}
新增组件
- 全局复用 →
src/components/,并在src/main.js注册(或页面内按需 import); - 模块专用 → 放模块目录下,如
src/views/shopping/components/。
新增样式
- 全局样式 →
src/assets/styles/ - 页面样式 → SFC 内
<style scoped lang="scss">
命名约定
| 对象 | 规则 | 示例 |
|---|---|---|
| 页面组件 | index.vue,<script setup name="xxx"> 小写 | name="config" |
| 组件文件 | PascalCase | ImageUpload.vue |
| 接口方法 | 动词 + 模块 | listUser、addConfig |
| Pinia 模块 | 小写单词 | store/modules/dict.js |
页面缓存(keepAlive)
keep-alive 的 include 按组件 name 匹配,所以必须保证「菜单配置的缓存」与「<script setup name="xxx">」一致:
提示
系统管理 → 菜单管理 → 编辑菜单 → 是否缓存选「是」;Vue 组件 name 要求小写且与路由地址一致。
<script setup name="config">
项目通过 vite-plugin-vue-setup-extend 支持在 script setup 上直接写 name。
使用字典
字典支持静态与动态两种来源,Vue3 下统一走 proxy.getDicts:
import { getCurrentInstance, reactive, toRefs } from 'vue'
const { proxy } = getCurrentInstance()
const state = reactive({
options: {
sys_common_status: [], // 动态:从后端按 dictType 拉取
sys_user_sex: [ // 静态:直接写死
{ dictLabel: '男', dictValue: '1' },
{ dictLabel: '女', dictValue: '0', listClass: 'danger' }
]
}
})
const { options } = toRefs(state)
// 一次取多个字典
const dictParams = [{ dictType: 'sys_common_status' }, { dictType: 'sys_yes_no' }]
proxy.getDicts(dictParams).then((res) => {
res.data.forEach((item) => {
state.options[item.dictType] = item.list
})
})
下拉与回显:
<el-option
v-for="dict in options.sys_common_status"
:key="dict.dictValue"
:label="dict.dictLabel"
:value="dict.dictValue"
/>
<!-- 表格回显 -->
<dict-tag :options="options.sys_common_status" :value="row.status" />
注意
dict-tag组件要求dictValue为 string;业务字段是int时用:value="parseInt(dict.dictValue)";- 多选字段用
:value="row.tags ? row.tags.split(',') : []"; - 字典有缓存与版本号机制,后台改字典后新请求即生效,无需清浏览器缓存(详见状态管理)。
自定义字典(SQL 数据源)
系统管理 → 字典管理 → 新增,字典类型以 sql_ 开头,按页面提示填写 SQL,即可把业务表当字典用。
使用参数
系统参数在 src/main.js 中挂到全局方法:
app.config.globalProperties.getConfigKey = getConfigKey
页面里读取:
const { proxy } = getCurrentInstance()
proxy.getConfigKey('sys.account.registerUser').then((res) => {
// res.data 即参数值
state.enableRegister = res.data === 'true'
})
日期与时间
日期区间快捷选项来自 src/utils/dateOptions.js,已挂到 proxy.dateOptions:
<el-date-picker v-model="dateRange" type="daterange" :shortcuts="dateOptions" />
默认今天、并把区间补成 00:00:00 ~ 23:59:59:
const dateRange = ref([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')])
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
需要精确到时分秒时,
value-format要写成YYYY-MM-DD HH:mm:ss。
格式化输出:{{ proxy.parseTime(row.createTime, 'YYYY-MM-DD HH:mm:ss') }}。
国际化
仅 Vue3 支持。静态语言包在 src/i18n/lang,页面级翻译放 src/i18n/pages,也可在「系统管理 → 多语言设置」动态维护。
- js 中:
const { t } = useI18n(); t('message.hello') - 模板中:
{{ $t('message.hello') }} - 菜单名:菜单管理里的「语言 key」对应
meta.titleKey,留空则直接用菜单名
推荐 VSCode 安装 i18n Ally 插件。详见国际化。
获取用户信息
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
userStore.userId
userStore.userName
userStore.name // 昵称
userStore.roles // 角色 key 数组
userStore.permissions // 权限字符串数组
userStore.tenantId // 当前租户(SaaS)
登录、双因子、锁屏、第三方登录等流程见登录与账号安全,模块职责见状态管理。
异常处理
请求层已经把「网络错误 / 超时 / 403 / 404 / 429 / 业务码」统一转成用户能看懂的提示:
| 场景 | 表现 |
|---|---|
| 业务码非 200 | ElMessage 弹出后端 msg |
| 401 | 弹「登录状态已过期」并清理会话、回首页 |
| 403 | 跳转 /401 |
| 404 | 提示 xxx接口404 |
| 5xx / 网络异常 | ElNotification 聚合提示,开发环境附带详情 |
组件内只需 catch 后按需处理,不必重复弹提示。
引用依赖
npm install md-editor-v3 --save
--save 会写入 package.json。项目已内置的库(ECharts、vxe-table、wangeditor、@vue-office 等)不要重复安装。
其他说明
- 省市区:项目未内置
element-china-area-data,需要时自行安装并按需引入; - 水印:
settings.showWatermark控制,逻辑在utils/wartermark.js; - 锁屏:右上角锁屏后跳
/lock,解锁需要登录密码,状态在store/modules/lock.js; - 多租户:请求统一带
tenantId,登录页可选租户;SaaS 下菜单/权限按套餐交集下发。
