init: v3 init

This commit is contained in:
陈凯龙
2021-12-07 14:36:07 +08:00
parent 2a7f3d2c46
commit 1ae75500de
206 changed files with 90 additions and 25740 deletions

View File

@@ -1,155 +0,0 @@
import { defineStore } from 'pinia'
import { store } from '../index'
export type LayoutType = 'Classic' | 'LeftTop' | 'Top' | 'Test'
export interface AppState {
collapsed: boolean
showTags: boolean
showLogo: boolean
showNavbar: boolean
fixedHeader: boolean
layout: LayoutType
showBreadcrumb: boolean
showHamburger: boolean
showScreenfull: boolean
showUserInfo: boolean
title: string
logoTitle: string
userInfo: string
greyMode: boolean
showBackTop: boolean
showMenuTab: boolean
requestTime: boolean
}
export const useAppStore = defineStore({
id: 'app',
state: (): AppState => ({
collapsed: false, // 菜单栏是否栏缩收
showLogo: true, // 是否显示logo
showTags: true, // 是否显示标签栏
showNavbar: true, // 是否显示navbar
fixedHeader: true, // 是否固定header
layout: 'Classic', // layout布局
showBreadcrumb: true, // 是否显示面包屑
showHamburger: true, // 是否显示侧边栏缩收按钮
showScreenfull: true, // 是否全屏按钮
showUserInfo: true, // 是否显示用户头像
title: 'vue-element-plus-admin', // 标题
logoTitle: 'vue-ElPlus-admin', // logo标题
userInfo: 'userInfo', // 登录信息存储字段-建议每个项目换一个字段,避免与其他项目冲突
greyMode: false, // 是否开始灰色模式,用于特殊悼念日
showBackTop: true, // 是否显示回到顶部
showMenuTab: false, // 是否固定一级菜单
requestTime: false // 是否在接口调用时添加时间戳避免IE缓存
}),
getters: {
getCollapsed(): boolean {
return this.collapsed
},
getShowLogo(): boolean {
return this.showLogo
},
getShowTags(): boolean {
return this.showTags
},
getShowNavbar(): boolean {
return this.showNavbar
},
getFixedHeader(): boolean {
return this.fixedHeader
},
getLayout(): LayoutType {
return this.layout
},
getShowBreadcrumb(): boolean {
return this.showBreadcrumb
},
getShowHamburger(): boolean {
return this.showHamburger
},
getShowScreenfull(): boolean {
return this.showScreenfull
},
getShowUserInfo(): boolean {
return this.showUserInfo
},
getTitle(): string {
return this.title
},
getLogoTitle(): string {
return this.logoTitle
},
getUserInfo(): string {
return this.userInfo
},
getGreyMode(): boolean {
return this.greyMode
},
getShowBackTop(): boolean {
return this.showBackTop
},
getShowMenuTab(): boolean {
return this.showMenuTab
},
getRequestTime(): boolean {
return this.requestTime
}
},
actions: {
setCollapsed(collapsed: boolean): void {
this.collapsed = collapsed
},
setShowLogo(showLogo: boolean): void {
this.showLogo = showLogo
},
setShowTags(showTags: boolean): void {
this.showTags = showTags
},
setShowNavbar(showNavbar: boolean): void {
this.showNavbar = showNavbar
},
setFixedHeader(fixedHeader: boolean): void {
this.fixedHeader = fixedHeader
},
setLayout(layout: LayoutType): void {
this.layout = layout
},
setBreadcrumb(showBreadcrumb: boolean): void {
this.showBreadcrumb = showBreadcrumb
},
setHamburger(showHamburger: boolean): void {
this.showHamburger = showHamburger
},
setScreenfull(showScreenfull: boolean): void {
this.showScreenfull = showScreenfull
},
setUserInfo(showUserInfo: boolean): void {
this.showUserInfo = showUserInfo
},
setTitle(title: string): void {
this.title = title
},
setLogoTitle(logoTitle: string): void {
this.logoTitle = logoTitle
},
setGreyMode(greyMode: boolean): void {
this.greyMode = greyMode
},
setShowBackTop(showBackTop: boolean): void {
this.showBackTop = showBackTop
},
setShowMenuTab(showMenuTab: boolean): void {
this.showMenuTab = showMenuTab
},
setRequestTime(requestTime: boolean): void {
this.requestTime = requestTime
}
}
})
export function useAppStoreWithOut() {
return useAppStore(store)
}

View File

@@ -1,181 +0,0 @@
import { defineStore } from 'pinia'
import { asyncRouterMap, constantRouterMap } from '@/router'
import { deepClone } from '@/utils'
import { AppRouteRecordRaw } from '@/router/types'
// import { useCache } from '@/hooks/web/useCache'
// const { wsCache } = useCache()
// import { isExternal } from '@/utils/validate'
import path from 'path-browserify'
// import { getParentLayout } from '@/router/utils'
import { store } from '../index'
// import { useAppStoreWithOut } from '@/store/modules/app'
// const appStore = useAppStoreWithOut()
// const modules = import.meta.glob('../../views/**/*.vue')
/* Layout */
// const Layout = () => import('@/layout/index.vue')
export interface PermissionState {
routers: AppRouteRecordRaw[]
addRouters: AppRouteRecordRaw[]
isAddRouters: boolean
activeTab: string
menuTabRouters: AppRouteRecordRaw[]
}
export const usePermissionStore = defineStore({
id: 'permission',
state: (): PermissionState => ({
routers: [],
addRouters: [],
isAddRouters: false,
menuTabRouters: [],
activeTab: ''
}),
getters: {
getRouters(): AppRouteRecordRaw[] {
return this.routers
},
getAddRouters(): AppRouteRecordRaw[] {
return this.addRouters
},
getIsAddRouters(): boolean {
return this.isAddRouters
},
getActiveTab(): string {
return this.activeTab
},
getMenuTabRouters(): AppRouteRecordRaw[] {
return this.menuTabRouters
}
},
actions: {
generateRoutes(): Promise<unknown> {
return new Promise<void>((resolve) => {
// 路由权限控制
// let routerMap: AppRouteRecordRaw[] = []
// if (wsCache.get(appStore.getUserInfo).roleName === 'admin') {
// // 模拟前端控制权限
// routerMap = generateRoutesFn(deepClone(asyncRouterMap, ['component']))
// } else {
// // 模拟后端控制权限
// routerMap = getFilterRoutes(wsCache.get(appStore.getUserInfo).checkedNodes)
// }
const routerMap: AppRouteRecordRaw[] = generateRoutesFn(
deepClone(asyncRouterMap, ['component'])
)
// 动态路由404一定要放到最后面
this.addRouters = routerMap.concat([
{
path: '/:path(.*)*',
redirect: '/404',
name: '404',
meta: {
hidden: true,
breadcrumb: false
}
}
])
// 渲染菜单的所有路由
this.routers = deepClone(constantRouterMap, ['component']).concat(routerMap)
resolve()
})
},
setIsAddRouters(state: boolean): void {
this.isAddRouters = state
},
setMenuTabRouters(routers: AppRouteRecordRaw[]): void {
this.menuTabRouters = routers
},
setAcitveTab(activeTab: string): void {
this.activeTab = activeTab
}
}
})
// 路由过滤,主要用于权限控制
function generateRoutesFn(routes: AppRouteRecordRaw[], basePath = '/'): AppRouteRecordRaw[] {
const res: AppRouteRecordRaw[] = []
for (const route of routes) {
// skip some route
if (route.meta && route.meta.hidden && !route.meta.showMainRoute) {
continue
}
// let onlyOneChild: Nullable<string> = null
// if (route.children && route.children.length === 1 && !route.meta.alwaysShow) {
// onlyOneChild = (
// isExternal(route.children[0].path)
// ? route.children[0].path
// : path.resolve(path.resolve(basePath, route.path), route.children[0].path)
// ) as string
// }
let data: Nullable<IObj> = null
// 如不需要路由权限,可注释以下逻辑
// 权限过滤,通过获取登录信息里面的角色权限,动态的渲染菜单。
// const list = wsCache.get(appStore.getUserInfo).checkedNodes
// // 开发者可以根据实际情况进行扩展
// for (const item of list) {
// // 通过路径去匹配
// if (isExternal(item.path) && (onlyOneChild === item.path || route.path === item.path)) {
// data = Object.assign({}, route)
// } else {
// const routePath = path.resolve(basePath, onlyOneChild || route.path)
// if (routePath === item.path || (route.meta && route.meta.followRoute === item.path)) {
// data = Object.assign({}, route)
// }
// }
// }
// 如不需要路由权限,解注释下面一行
data = Object.assign({}, route)
// recursive child routes
if (route.children && data) {
data.children = generateRoutesFn(route.children, path.resolve(basePath, data.path))
}
if (data) {
res.push(data as AppRouteRecordRaw)
}
}
return res
}
// 模拟后端过滤路由
// function getFilterRoutes(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
// const res: AppRouteRecordRaw[] = []
// for (const route of routes) {
// const data: AppRouteRecordRaw | IObj = {
// path: route.path,
// name: route.name,
// redirect: route.redirect
// }
// data.meta = Object.assign({}, route.meta || {}, { title: route.meta.title })
// if (route.component) {
// // 动态加载路由文件,可根据实际情况进行自定义逻辑
// data.component = (
// (route.component as any) === '#'
// ? Layout
// : (route.component as any).includes('##')
// ? getParentLayout((route.component as any).split('##')[1])
// : modules[`../../${route.component}.vue`]
// ) as any
// }
// // recursive child routes
// if (route.children) {
// data.children = getFilterRoutes(route.children)
// }
// res.push(data as AppRouteRecordRaw)
// }
// return res
// }
export function usePermissionStoreWithOut() {
return usePermissionStore(store)
}

View File

@@ -1,204 +0,0 @@
import router from '@/router'
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { getRoute } from '@/router/utils'
import { defineStore } from 'pinia'
import { store } from '../index'
export const PAGE_LAYOUT_KEY = '__PAGE_LAYOUT__'
export interface TagsViewState {
visitedViews: RouteLocationNormalizedLoaded[]
cachedViews: Map<string, string[]>
}
export const useTagsViewStore = defineStore({
id: 'tagsView',
state: (): TagsViewState => ({
visitedViews: [],
cachedViews: new Map<string, string[]>()
}),
getters: {
getVisitedViews(): any[] {
return this.visitedViews
},
getCachedViews(): Map<string, string[]> {
return this.cachedViews
}
},
actions: {
ADD_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
if (this.visitedViews.some((v: RouteLocationNormalizedLoaded) => v.path === view.path)) return
if (view.meta?.noTagsView) return
this.visitedViews.push(
Object.assign({}, view, {
title: view.meta.title || 'no-name'
})
)
},
SET_CACHED_VIEW(): void {
const cacheMap = new Map<string, string[]>()
const pageCacheSet = new Set<string>()
this.visitedViews.forEach((tab) => {
const item = getRoute(tab)
const needCache = !item.meta?.noCache
if (!needCache) return
if (item.meta?.affix) {
const name = item.name as string
pageCacheSet.add(name)
} else if (item.matched && needCache) {
const matched = item.matched
const len = matched.length
if (len < 2) return
for (let i = 0; i < matched.length; i++) {
const key = matched[i].name as string
if (i < 2) {
pageCacheSet.add(key)
}
if (i < len - 1) {
const { meta, name } = matched[i + 1]
if (meta && (meta.affix || needCache)) {
const mapList = cacheMap.get(key) || []
if (!mapList.includes(name as string)) {
mapList.push(name as string)
}
cacheMap.set(key, mapList)
}
}
}
}
})
cacheMap.set(PAGE_LAYOUT_KEY, Array.from(pageCacheSet))
this.cachedViews = cacheMap
},
DEL_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
for (const [i, v] of this.visitedViews.entries()) {
if (v.path === view.path) {
this.visitedViews.splice(i, 1)
break
}
}
},
DEL_CACHED_VIEW(): void {
const route = router.currentRoute.value
for (const [key, value] of this.cachedViews) {
const index = value.findIndex((item: string) => item === (route.name as string))
if (index === -1) {
continue
}
if (value.length === 1) {
this.cachedViews.delete(key)
continue
}
value.splice(index, 1)
this.cachedViews.set(key, value)
}
},
DEL_OTHERS_VISITED_VIEWS(view: RouteLocationNormalizedLoaded): void {
this.visitedViews = this.visitedViews.filter((v) => {
return v.meta.affix || v.path === view.path
})
},
DEL_ALL_VISITED_VIEWS(): void {
// keep affix tags
const affixTags = this.visitedViews.filter((tag) => tag.meta.affix)
this.visitedViews = affixTags
},
UPDATE_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
for (let v of this.visitedViews) {
if (v.path === view.path) {
v = Object.assign(v, view)
break
}
}
},
addView(view: RouteLocationNormalizedLoaded): void {
this.addVisitedView(view)
this.addCachedView()
},
addVisitedView(view: RouteLocationNormalizedLoaded): void {
this.ADD_VISITED_VIEW(view)
},
addCachedView(): void {
this.SET_CACHED_VIEW()
},
delView(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.delVisitedView(view)
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delVisitedView(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_VISITED_VIEW(view)
resolve([...this.visitedViews])
})
},
delCachedView(): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
delOthersViews(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.delOthersVisitedViews(view)
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delOthersVisitedViews(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_OTHERS_VISITED_VIEWS(view)
resolve([...this.visitedViews])
})
},
delOthersCachedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.SET_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
delAllViews(): Promise<unknown> {
return new Promise((resolve) => {
this.delAllVisitedViews()
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delAllVisitedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_ALL_VISITED_VIEWS()
resolve([...this.visitedViews])
})
},
delAllCachedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.SET_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
updateVisitedView(view: RouteLocationNormalizedLoaded): void {
this.UPDATE_VISITED_VIEW(view)
}
}
})
export function useTagsViewStoreWithOut() {
return useTagsViewStore(store)
}