作为一款基于uni-app的企业级移动端框架,若依(RuoYi)App版的项目结构设计体现了典型的前端工程化思想。初次接触这个项目时,我注意到它的目录组织非常符合现代前端开发规范,特别是对Vue技术栈的深度适配。下面我将从实际开发角度,详细剖析这个项目骨架的设计哲学。
项目根目录下的每个文件夹都有其明确的职责边界,这种模块化设计让代码维护变得轻松:
api目录:所有网络请求集中管理,采用接口封装模式。在实际项目中,我建议按业务模块进一步划分子目录(如system-api、user-api等),避免单个文件过于臃肿。
components:全局公用组件库。值得注意的是,这里的组件都是经过项目定制的,与uni_modules下的通用组件形成互补。开发时应遵循"先查库存,再新建"的原则。
pages:采用约定式路由,目录结构即路由结构。我在实际开发中发现,若依的页面组织特别适合中型项目,每个页面目录可以包含其私有组件和资源。
plugins:存放全局插件和过滤器。这里可以扩展Vue的原型方法,比如我常在这里添加$formatDate等工具方法。
store:Vuex状态管理。若依采用了模块化设计,对于复杂业务,可以按功能拆分store模块。
static目录的处理方式值得关注:
markdown复制static/
├── favicon.ico # 多平台自动适配的网站图标
├── index.html # 模板文件实际很少需要修改
└── logo.png # 建议准备@2x和@3x版本
在移动端开发中,我习惯将静态资源分为三类:
重要提示:uni-app打包时static目录会原样输出,而assets目录会经过编译处理。因此图标字体等需要保持原样的资源必须放在static下。
App.vue作为应用入口,其设计思路值得仔细研究。我们来看增强版的代码解析:
javascript复制onLaunch: function() {
// 设备信息采集(示例扩展)
this.getSystemInfo()
// 主题初始化
this.initTheme()
// 原有初始化逻辑
this.initApp()
},
methods: {
getSystemInfo() {
uni.getSystemInfo({
success: res => {
this.globalData.systemInfo = res
// 根据设备类型设置全局变量
this.globalData.isIOS = res.platform === 'ios'
}
})
},
initTheme() {
// 读取本地存储的主题配置
const theme = uni.getStorageSync('APP_THEME') || 'light'
this.globalData.theme = theme
this.applyTheme(theme)
}
}
关键点解析:
我在实际项目中通常会扩展以下功能:
config.js的默认配置虽然简单,但在企业级应用中需要更完善的方案:
javascript复制// 环境配置自动切换
const env = process.env.NODE_ENV || 'development'
const configs = {
development: {
baseUrl: 'http://localhost:8080',
debug: true
},
production: {
baseUrl: 'https://vue.ruoyi.vip/prod-api',
debug: false
}
}
// 合并应用配置
module.exports = {
...configs[env],
appInfo: {
name: "ruoyi-app",
version: "1.2.0",
// 版本比较方法(扩展)
compareVersion(v) {
return this.version.localeCompare(v, undefined, {numeric: true})
}
}
}
配置管理经验:
pages.json的配置直接影响应用导航体验,分享几个实用技巧:
json复制{
"pages": [
{
"path": "pages/login",
"style": {
"navigationBarTitleText": "登录",
// 禁用原生导航栏(自定义导航时使用)
"navigationStyle": "custom",
// 下拉刷新配置
"enablePullDownRefresh": true,
// 页面背景色
"backgroundColor": "#f8f8f8"
}
}
],
"tabBar": {
// 添加徽标配置
"list": [{
"pagePath": "pages/index",
"iconPath": "static/tabbar/home.png",
"selectedIconPath": "static/tabbar/home_active.png",
"text": "首页",
// 徽标文字
"badgeText": "12"
}]
}
}
路由设计建议:
虽然若依已经集成了Vuex,但在大型项目中可以考虑以下优化:
javascript复制// store/modules/user.js
export default {
namespaced: true,
state: () => ({
token: uni.getStorageSync('token') || '',
userInfo: null
}),
mutations: {
SET_TOKEN(state, token) {
state.token = token
uni.setStorageSync('token', token)
}
},
actions: {
async login({ commit }, payload) {
try {
const { token } = await loginApi(payload)
commit('SET_TOKEN', token)
return Promise.resolve()
} catch (error) {
return Promise.reject(error)
}
}
}
}
状态管理要点:
permission.js的增强实现:
javascript复制import store from '@/store'
import { TOKEN_KEY } from '@/utils/auth'
const whiteList = ['/pages/login', '/pages/register']
export default function() {
uni.addInterceptor('navigateTo', {
invoke(args) {
const url = args.url.split('?')[0]
if (whiteList.includes(url)) return true
if (!store.getters.token) {
uni.redirectTo({
url: '/pages/login?redirect=' + encodeURIComponent(args.url)
})
return false
}
return true
},
fail(err) {
console.error('导航失败:', err)
}
})
}
权限控制技巧:
在package.json中配置跨平台构建命令:
json复制{
"scripts": {
"build:h5": "cross-env NODE_ENV=production UNI_PLATFORM=h5 vue-cli-service uni-build",
"build:mp-weixin": "cross-env NODE_ENV=production UNI_PLATFORM=mp-weixin vue-cli-service uni-build",
"build:app-plus": "cross-env NODE_ENV=production UNI_PLATFORM=app-plus vue-cli-service uni-build"
}
}
构建优化建议:
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| H5页面空白 | 路由base路径错误 | 检查manifest.json中的h5配置 |
| 小程序无法请求接口 | 未配置合法域名 | 在小程序后台配置request合法域名 |
| 图片加载失败 | 路径大小写问题 | 统一使用小写文件名和引用 |
| 样式不生效 | scoped样式冲突 | 使用/deep/或::v-deep穿透 |
在长期使用若依框架的过程中,我总结出几个关键点:
对于想要深度定制的开发者,建议从以下几个方面入手: