头图

vue3-osx一款原创自研的vite5.x+vue3+pinia2+arcodesign+sortablejs等技术构建的webOS网页版桌面OS管理系统模板新解决方案。

vite-macos项目支持windows/macos两种桌面、支持自定义桌面栅格布局、可拖拽图标、多屏分页管理、自定义桌面壁纸主题、毛玻璃虚化背景等功能。

技术框架

  • 编码工具:vsCode
  • 技术框架:vite5.3+vue3.4+vue-router4.4+pinia2
  • UI组件库:arco-design^2.55.3 (字节vue3组件库)
  • 状态管理:pinia^2.1.7
  • 图表插件:echarts^5.5.1
  • 拖拽组件:sortablejs^1.15.2
  • 富文本编辑器:wangeditor^4.7.15
  • 模拟数据:mockjs^1.1.0
  • 样式编译:sass^1.77.8
  • 构建工具:vite^5.3.3

之前有分享过一篇uniapp+vue3跨三端手机os管理系统,感兴趣的可以去瞧瞧。
https://segmentfault.com/a/1190000044899693

项目结构

使用vite5.js搭建项目框架,采用vue3 setup语法糖语法。

目前vite-macos网页os系统项目已经发布到我的原创作品集。希望给大家有所帮助!
https://gf.bilibili.com/item/detail/1106413011

import { createApp } from 'vue'
import './style.scss'
import App from './App.vue'

// 引入arco.design组件库
import ArcoDesign from '@arco-design/web-vue'
import '@arco-design/web-vue/dist/arco.css'
// 额外引入图标库
import ArcoIcon from '@arco-design/web-vue/es/icon'
import VEPlus from 've-plus'
import 've-plus/dist/ve-plus.css'

// 引入路由及状态管理
import Router from './router'
import Pinia from './pinia'

const app = createApp(App)

app
.use(ArcoDesign)
.use(ArcoIcon)
.use(VEPlus)
.use(Router)
.use(Pinia)
.mount('#app')

vue3-router路由管理

image.png

vite批量导入路由配置。

/**
 * 路由管理Router
 * @author andy
 */

import { createRouter, createWebHashHistory } from 'vue-router'
import { authState } from '@/pinia/modules/auth'

import Layout from '@/layouts/index.vue'

// 批量导入路由
const modules = import.meta.glob('./modules/*.js', { eager: true })
console.log(modules)
const patchRouters = Object.keys(modules).map(key => modules[key].default).flat()
console.log(patchRouters)

/**
 * meta配置
 * @param meta.requireAuth 需登录验证页面
 */
const routes = [
  {
    path: '/',
    redirect: '/desktop',
  },
  ...patchRouters,
  // 错误模块
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    component: Layout,
    meta: {
      title: '404error',
    },
    children: [
      {
        path: '404',
        component: () => import('@/views/error/404.vue'),
      }
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 全局路由钩子拦截
router.beforeEach((to, from) => {
  const authstate = authState()
  // 登录验证
  if(to?.meta?.requireAuth && !authstate.authorization) {
    console.log('你还未登录!')
    return {
      path: '/login'
    }
  }
})

router.afterEach(() => {
  // ...
})

router.onError(error => {
  console.warn('[Router Error]', error)
})

桌面desk模板

<script setup>
  import { appState } from '@/pinia/modules/app'

  // 引入布局模板
  import MacosLayout from './template/macos.vue'
  import WindowsLayout from './template/windows.vue'

  const appstate = appState()

  const DeskLayout = {
    macos: MacosLayout,
    windows: WindowsLayout
  }
</script>

<template>
  <div
    class="vu__container desktop flexbox flex-alignc flex-justifyc"
    :style="{'--themeSkin': appstate.config.skin}"
    @contextmenu.prevent
  >
    <component :is="DeskLayout[appstate.config.layout]" />
  </div>
</template>

abda5613bf32c454fb83b260dca649d9_1289798-20240725175448820-51700198.png

<template>
  <div class="vu__layout flexbox flex-col">
    <div class="vu__layout-header">
      <Toolbar />
    </div>
    <div class="vu__layout-body flex1 flexbox">
      <Desk />
    </div>
    <div class="vu__layout-footer">
      <Dock />
    </div>
    <!-- 悬浮球(辅助触控) -->
    <Touch />
  </div>
</template>

桌面栅格引擎

采用全新自研的grid栅格化布局引擎。支持自定义图标、拖拽排序等功能。
9857f29b072e980d01f7e8627e5490d2_1289798-20240725181106968-437275587.png

c8f4572b01eeda41419b340cd2e666fe_1289798-20240725181346440-884748211.png

95fad1823f209408c0004d74afc8ea9d_1289798-20240725181500865-2097606110.png

// 自定义变量(桌面图标)
const deskVariable = ref({
  '--icon-radius': '8px', // 圆角
  '--icon-size': '60px', // 图标尺寸(设置rpx自定义手机设备)
  '--icon-gap-col': '30px', // 水平间距
  '--icon-gap-row': '30px', // 垂直间距
  '--icon-labelSize': '12px', // 标签文字大小
  '--icon-labelColor': '#fff', // 标签颜色
  '--icon-fit': 'contain', // 图标自适应模式
})

c63a51425cb5534805473bfa6a780ee5_1289798-20240725182347078-1320024477.png

5eae802fee0f189da9107222028b2aa2_1289798-20240725182650376-434760207.png

/* 桌面菜单json配置项 Q:282310962 */

const deskMenu = [
  {
    pid: 20240507001,
    list: [
      {imgico: markRaw(Today), size: '2x2'},
      {imgico: markRaw(Weather), size: '2x2'},
      {label: '便签', imgico: markRaw(NoteBook), size: '4x2'},
      ...
    ]
  },
  {
    pid: 20240509002,
    list: [
      {label: 'Appstore', imgico: '/static/mac/appstore.png'},
      {label: '地图', imgico: '/static/mac/maps.png'},
      ...
    ]
  },
  {
    pid: 20240510001,
    list: [
      {label: 'Github', imgico: '/static/svg/github.svg', link: 'https://github.com/', background: '#607d8b',},
      ...
    ]
  },
  {
    uid: 'd141f210-207e-1e8e-9950-9deefac27e48',
    list: [
      {label: 'Vite^5.3.3', imgico: 'https://vitejs.dev/logo.svg', link: 'https://vitejs.dev/'},
      ...
      {
        label: '组件',
        children: [
          {label: '表格', imgico: '/static/svg/table.svg', path: '/components/table/all'},
          {label: '自定义表格', imgico: '/static/svg/table.svg', path: '/components/table/custom'},
          ...
        ]
      },
      {label: 'ChatGPT', imgico: '/static/svg/chatgpt.svg', link: 'https://openai.com/chatgpt/', background: '#15A17F',},
      {label: 'Bilibili', imgico: '/static/svg/bilibili.svg', link: 'https://www.bilibili.com/', background: '#ff6899',},
      {
        label: '个人中心',
        children: [
          {label: '主页', imgico: '/static/svg/my.svg', path: '/setting'},
          ...
        ]
      },
      {
        label: '设置',
        children: [
          {label: '网站设置', imgico: '/static/svg/settings.svg', path: '/setting/system/website'},
          {label: '邮件服务', imgico: '/static/mac/mail.png', path: '/setting/system/mail'},
        ]
      },
      {
        label: '公众号', imgico: markRaw(IconWechat), color: '#07c160',
        onClick: () => {
          ...
        }
      },
    ]
  }
]

vite-macos项目涉及到的知识点还是非常多的,今天就先分享到这里。

https://segmentfault.com/a/1190000045042968

https://segmentfault.com/a/1190000044984067

https://segmentfault.com/a/1190000044741696


xiaoyan2017
765 声望314 粉丝

web前端开发爱好者,专注于前端h5、jquery、vue、react、angular等技术研发实战项目案例。