123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- export function timeFix () {
- const time = new Date()
- const hour = time.getHours()
- return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
- }
- export function welcome () {
- const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
- const index = Math.floor(Math.random() * arr.length)
- return arr[index]
- }
- /**
- * 触发 window.resize
- */
- export function triggerWindowResizeEvent () {
- const event = document.createEvent('HTMLEvents')
- event.initEvent('resize', true, true)
- event.eventType = 'message'
- window.dispatchEvent(event)
- }
- export function handleScrollHeader (callback) {
- let timer = 0
- let beforeScrollTop = window.pageYOffset
- callback = callback || function () {}
- window.addEventListener(
- 'scroll',
- event => {
- clearTimeout(timer)
- timer = setTimeout(() => {
- let direction = 'up'
- const afterScrollTop = window.pageYOffset
- const delta = afterScrollTop - beforeScrollTop
- if (delta === 0) {
- return false
- }
- direction = delta > 0 ? 'down' : 'up'
- callback(direction)
- beforeScrollTop = afterScrollTop
- }, 50)
- },
- false
- )
- }
- /**
- * Remove loading animate
- * @param id parent element id or class
- * @param timeout
- */
- export function removeLoadingAnimate (id = '', timeout = 1500) {
- if (id === '') {
- return
- }
- setTimeout(() => {
- document.body.removeChild(document.getElementById(id))
- }, timeout)
- }
- /**
- * @param {String|Number} value 要验证的字符串或数值
- * @param {*} validList 用来验证的列表
- */
- export function oneOf (value, validList) {
- for (let i = 0; i < validList.length; i++) {
- if (value === validList[i]) {
- return true
- }
- }
- return false
- }
- /**
- * @param {Function} fn 防抖函数
- * @param {Number} delay 延迟时间
- */
- export function debounce(fn, delay) {
- var timer;
- return function () {
- var context = this;
- var args = arguments;
- clearTimeout(timer);
- timer = setTimeout(function () {
- fn.apply(context, args);
- }, delay);
- };
- }
- /**
- * @param {date} time 需要转换的时间
- * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
- */
- export function formatTime(time, fmt) {
- if (!time) return '';
- else {
- const date = new Date(time);
- const o = {
- 'M+': date.getMonth() + 1,
- 'd+': date.getDate(),
- 'H+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- S: date.getMilliseconds(),
- };
- if (/(y+)/.test(fmt))
- fmt = fmt.replace(
- RegExp.$1,
- (date.getFullYear() + '').substr(4 - RegExp.$1.length)
- );
- for (const k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1
- ? o[k]
- : ('00' + o[k]).substr(('' + o[k]).length)
- );
- }
- }
- return fmt;
- }
- }
- // 千分位分隔符
- export function formatThousands (number) {
- if(!number){return 0}
- const arr = number.toString().split('.')
- const numbers = arr[0].split('').reverse()
- const segs = []
- while (numbers.length) segs.push(numbers.splice(0, 3).join(''))
- return segs.join(',').split('').reverse().join('') + (arr.length>1 ? ("."+arr[1]) : '')
- }
- /**
- * 数组转树形结构
- * @param {array} list 被转换的数组
- * @param {number|string} root 根节点(最外层节点)的 id
- * @return array
- */
- export function arrayToTree(list, root) {
- const result = [] // 用于存放结果
- const map = {} // 用于存放 list 下的节点
- // 1. 遍历 list,将 list 下的所有节点以 id 作为索引存入 map
- for (const item of list) {
- map[item.id] = { ...item } // 浅拷贝
- }
- // 2. 再次遍历,将根节点放入最外层,子节点放入父节点
- for (const item of list) {
- // 3. 获取节点的 id 和 父 id
- const { id, parentId } = item // ES6 解构赋值
- // 4. 如果是根节点,存入 result
- if (item.parentId == root) {
- result.push(map[id])
- } else {
- // 5. 反之,存入到父节点
- map[parentId].children
- ? map[parentId].children.push(map[id])
- : (map[parentId].children = [map[id]])
- }
- }
- // 将结果返回
- return result
- }
- // 树查找
- export function treeFind (tree, func) {
- for (const data of tree) {
- if (func(data)) return data
- if (data.children) {
- const res = treeFind(data.children, func)
- if (res) return res
- }
- }
- return null
- }
- // 是否有价格权限
- export function hasPriceAuth(authNode, priceOptions, userAuthCode){
- const codes = []
- const ret = []
- // 过滤当前节点下的非价格权限菜单
- authNode.map(item=>{
- const a = item.code.split('_')
- const hasCode = priceOptions.find(k => k.value == a[a.length-1])
- const hasRoles = userAuthCode.includes(item.code)
- // 获取当前用户的角色拥有的价格权限
- if(hasCode && hasRoles){
- codes.push(a[a.length-1])
- }
- })
- // console.log(codes)
- // 根据拥有的价格权限生成数组标记[1,0,1,0,0],每一位对应一个价格权限
- // 有权限标记1,否则标记0
- priceOptions.map(item => {
- ret.push(codes.includes(item.value)?1:0)
- })
- // console.log(ret)
- return ret
- }
- // 获取接口对应的价格权限code
- export function getAuthPriceCode (config, router, store) {
- // 通过路由打开的页面的权限code
- const permission = router.history.current.meta.permission
- // 手动指定的权限code,如打开的弹框页面或导出、打印
- // 手动指定的权限在使用完后需要清空,如在关闭弹框或导出、打印接口调用完成后清空
- const curActionPermission = store.state.app.curActionPermission
- // 价格权限的所有选项,销售价、成本价、省、市、特约价
- const priceOptions = store.state.app.priceAuthOptions
- // 最终获取的权限code,手动指定的优先级高于路由打开的页面权限code
- const authCode = curActionPermission || permission
- // 当前角色的分配的价格权限,这里过滤非价格权限code
- const roles = store.state.user.roles
- const userAuthCode = roles.permissionList.filter(item => {
- const a = item.split('_')
- return priceOptions.find(k => k.value == a[a.length-1])
- })
- // 如果有权限code
- if(authCode){
- // 当前正在调用的接口url
- const url = config.url
- // 所有的权限菜单数据
- const authTree = store.state.app.authMenusList
-
- // 从所有的权限菜单中查找当前权限code对应的权限菜单数据
- const authNode = treeFind(authTree,(item)=>item.code == authCode)
- // console.log(authNode)
- if(!authNode.permission){return []}
- // 从找到的对应权限菜单数据中判断当前调用接口的url是否存在,这里和权限菜单中的后台权限code比较
- const hasReqUrl = authNode.permission.split(',').find(item => url.replace(/\//g,'_').indexOf(item)>=0)
- // 如果存在则返回一个如 [1,0,1,0,0] 的格式的价格权限字符串给后台接口
- if(hasReqUrl&&authNode.children&&authNode.children.length){
- return hasPriceAuth(authNode.children,priceOptions,userAuthCode)
- }
- return []
- }
- return []
- }
|