init
This commit is contained in:
198
src/utils/uiTool.js
Normal file
198
src/utils/uiTool.js
Normal file
@@ -0,0 +1,198 @@
|
||||
import http from './http'
|
||||
|
||||
export default class uiTool {
|
||||
static setRem() {
|
||||
let whdef = 100 / 1920
|
||||
let bodyWidth = document.body.clientWidth
|
||||
if (bodyWidth < 1360) {
|
||||
bodyWidth = 1360
|
||||
}
|
||||
|
||||
let rem = bodyWidth * whdef
|
||||
document.documentElement.style.fontSize = rem + 'px'
|
||||
console.log('自适应html字体大小', parseInt(rem))
|
||||
console.log('自适应缩放比列', (rem / 100).toFixed(3))
|
||||
window.$whdef = rem
|
||||
}
|
||||
|
||||
static getImgSrc(src) {
|
||||
if (src) {
|
||||
return http.baseUrl() + src
|
||||
} else {
|
||||
return '/assets/img/noImg.png'
|
||||
}
|
||||
}
|
||||
|
||||
static getBtn(h, options) {
|
||||
let rets = []
|
||||
if (!options) {
|
||||
options = [options]
|
||||
}
|
||||
|
||||
options.forEach(item => {
|
||||
rets.push(
|
||||
h(
|
||||
'Button',
|
||||
{
|
||||
props: {
|
||||
type: 'text',
|
||||
ghost: true,
|
||||
loading: item.loading
|
||||
},
|
||||
style: {
|
||||
margin: '2px',
|
||||
color: '#2d8cf0'
|
||||
},
|
||||
on: {
|
||||
click: () => {
|
||||
item.click && item.click()
|
||||
}
|
||||
}
|
||||
},
|
||||
item.title
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
return h('div', { style: { margin: '5px' } }, rets)
|
||||
}
|
||||
|
||||
static getDropdown(h, items) {
|
||||
let btns = []
|
||||
if (items && items.length > 0) {
|
||||
items.forEach(item => {
|
||||
btns.push(
|
||||
h(
|
||||
'DropdownItem',
|
||||
{
|
||||
on: {
|
||||
click: () => {
|
||||
item.click && item.click()
|
||||
}
|
||||
}
|
||||
},
|
||||
item.title
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return h('Dropdown', {}, [
|
||||
h('a', {}, ['更多', h('Icon', { props: { type: 'ios-arrow-down' } })]),
|
||||
h('DropdownMenu', { slot: 'list' }, btns)
|
||||
])
|
||||
}
|
||||
|
||||
static delConfirm(callback) {
|
||||
if (window.rootVue && window.rootVue.$Modal) {
|
||||
window.rootVue.$Modal.confirm({
|
||||
title: '温馨提示',
|
||||
content: '<p>你确定删除吗?</p>',
|
||||
onOk: () => {
|
||||
callback && callback()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static showConfirm({ title = '温馨提示', content = '内容' }, callback) {
|
||||
if (window.rootVue && window.rootVue.$Modal) {
|
||||
window.rootVue.$Modal.confirm({
|
||||
title,
|
||||
content,
|
||||
onOk: () => {
|
||||
callback && callback()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static subTree(curTree, tree, callback) {
|
||||
if (curTree && curTree.length > 0) {
|
||||
curTree.forEach(p => {
|
||||
let childrenTree = tree.filter(p2 => p.id === p2.parent_id)
|
||||
if (childrenTree) {
|
||||
let subTree = uiTool.subTree(childrenTree, tree, callback)
|
||||
p.children = subTree || []
|
||||
}
|
||||
})
|
||||
|
||||
if (callback) {
|
||||
return curTree.map(p => {
|
||||
return callback(p)
|
||||
})
|
||||
}
|
||||
|
||||
return curTree
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
static transformTree(tree, callback) {
|
||||
let rootTree = tree.filter(p => p.parent_id === 0)
|
||||
let curTrees = uiTool.subTree(rootTree, tree, callback)
|
||||
return curTrees
|
||||
}
|
||||
|
||||
static menuToRoute(menus, ParentView, Page404) {
|
||||
if (menus && menus.length > 0) {
|
||||
menus.forEach(item => {
|
||||
if (item.type === '菜单') {
|
||||
item.component = ParentView
|
||||
} else if (item.type === '页面' || item.type === '功能') {
|
||||
try {
|
||||
let componentName = item.component
|
||||
// 这里需要在使用时动态导入
|
||||
item.componentPath = componentName
|
||||
item.component = Page404
|
||||
} catch (e) {
|
||||
item.component = Page404
|
||||
}
|
||||
} else {
|
||||
item.component = ParentView
|
||||
}
|
||||
|
||||
item.meta = {
|
||||
icon: item.icon,
|
||||
isMenu: item.is_show_menu,
|
||||
type: item.type
|
||||
}
|
||||
|
||||
if (item.children && item.children.length > 0) {
|
||||
item.children = uiTool.menuToRoute(item.children, ParentView, Page404)
|
||||
}
|
||||
})
|
||||
return menus
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
static getRoutes(Main, ParentView, Page404) {
|
||||
let mainRoute = {
|
||||
path: '/',
|
||||
name: '主视图',
|
||||
redirect: '/home',
|
||||
component: Main,
|
||||
meta: { title: '首页', notCache: true },
|
||||
children: []
|
||||
}
|
||||
|
||||
if (
|
||||
localStorage.authorityMenus &&
|
||||
localStorage.authorityMenus !== 'undefined'
|
||||
) {
|
||||
let authorityMenus = JSON.parse(localStorage.authorityMenus) || []
|
||||
|
||||
if (authorityMenus && authorityMenus.length > 0) {
|
||||
let menus = uiTool.transformTree(authorityMenus)
|
||||
let curRoutes = uiTool.menuToRoute(menus, ParentView, Page404)
|
||||
|
||||
mainRoute.children = curRoutes
|
||||
}
|
||||
}
|
||||
|
||||
return mainRoute
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user