// 用于生成uuid const S4 = () => { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) } const guid = () => { return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4()) } /* http 传参方法json2url, json2path, json2formData, */ const json2url = (json) => { if (!json) { return false } var arr = [] for (var name in json) { arr.push(name + '=' + json[name]) } var str = arr.join('&') return str } const json2path = (json) => { if (!json) { return false } var arr = [] for (var name in json) { arr.push(json[name]) } var str = arr.join('/') return str } const json2formData = (json) => { // Content-Type: multipart/form-data;boundary=${bound} //代表分割符号 if (!json) { return false } const _opts = new FormData() for (var name in json) { _opts.append(name, json[name]) } return _opts } const findRouersFn = (rouers, path) => { if (rouers) { for (let iteam of rouers) { if (iteam.url === path) { return iteam } } } return false } /* 获取当前路由信息 */ const thisRouterObjFn = (routesArr) => { if (!routesArr && !Array.isArray(routesArr)) { return false } let pathArr = [] let MenuInfo = sessionStorage.getItem('fSDoorwayManagementMenuInfo') && JSON.parse(sessionStorage.getItem('fSDoorwayManagementMenuInfo')) if (MenuInfo) { for (let i = 0; i < routesArr.length; i++) { const element = routesArr[i] const result = findRouersFn(MenuInfo, element.path) if (result) { pathArr.push(result) if (result.child) { MenuInfo = result.child } } } return pathArr } return false } // 判断图片是否存在 const isHasImg = (pathImg) => { var ImgObj = new Image() ImgObj.src = pathImg if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) { return true } else { return false } } // 递归获取菜单,获取全部权限 /*** * arr 菜单数组 * fn 回调必用 ***/ const findButtonAuthoritys = (arr, fn) => { fn = fn || function () {} if (Array.isArray(arr)) { for (let iteam of arr) { if (iteam.type === 2 && iteam.permission !== '') { fn(iteam.permission) } if (iteam.child) { findButtonAuthoritys(iteam.child, fn) } } } } // 数组去重-1标识未找到 const findInArr = (n, arr) => { for (let i = 0; i < arr.length; i++) { if (arr[i] === n) { return i } } return -1 } const findInArrJson = (n, arr, name) => { for (let i = 0; i < arr.length; i++) { if (arr[i][name] === n) { return i } } return -1 } const removeArrByValue = (val, arr) => { for (let i = 0; i < arr.length; i++) { if (arr[i] === val) { arr.splice(i, 1) } } return arr } const removeArrJsonByValue = (n, arr, name) => { for (let i = 0; i < arr.length; i++) { if (arr[i][name] === n) { arr.splice(i, 1) } } return arr } // json 是否为空 const isJsonNull = (jsonStr) => { if (typeof jsonStr === 'object' && jsonStr != null) { for (let key in jsonStr) { if (jsonStr[key]) { return key } } } return false } const cloneDeep = (node) => { if (!node) return node var newNode = {} Object.keys(node).forEach(function (key) { if (key[0] === '_') return var val = node[key] if (val) { if (val.type) { val = cloneDeep(val) } else if (Array.isArray(val)) { val = val.map(cloneDeep) } } newNode[key] = val }) return newNode } var Week = (function () { var ONE_DAY = 24 * 3600 * 1000 var ONE_WEEK = 7 * ONE_DAY function formatNumber(num) { return (num > 9 ? '' : '0') + num } function formatDate(date, num) { var year = date.getFullYear() var month = formatNumber(date.getMonth() + 1) var day = formatNumber(date.getDate()) var nextWeek = new Date(+date + ONE_WEEK) var nextWeekYear = nextWeek.getFullYear() var nextWeekMonth = formatNumber(nextWeek.getMonth() + 1) var nextWeekday = formatNumber(nextWeek.getDate() - 1) /* 只截取当前年份时间 */ if (year === nextWeekYear) { return year + '-' + month + '-' + day + '~' + nextWeekYear + '-' + nextWeekMonth + '-' + nextWeekday } else { return year + '-' + month + '-' + day + '~' + year + '-' + month + '-31' } /* return year + '年第' + formatNumber(num + 1) + '周' + month + '月' + day + '号-' + nextWeekYear + '年' + nextWeekMonth + '月' + nextWeekday + '号'; */ } function Week(year) { this.year = new Date(year, 0, 1) this.nextYear = new Date(year + 1, 0, 1) this.days = 0 this.weeks = 0 } Week.prototype.getDays = function () { this.days = Math.ceil((this.nextYear - this.year) / ONE_DAY) return Math.ceil((this.nextYear - this.year) / ONE_DAY) } Week.prototype.getWeeks = function () { this.weeks = Math.ceil(this.days || this.getDays() / 7) return Math.ceil(this.days || this.getDays() / 7) } Week.prototype.getSomeWeek = function (num) { return formatDate(new Date(+this.year + ONE_WEEK * num), num) } Week.prototype.showWeek = function (num) { num = parseInt(num - 1) || 0 if (!this.weeks) { this.getWeeks() } if (num > this.weeks) { num = 0 } if (num || num === 0) { return this.getSomeWeek(num) } else { var arr = [] while (num < this.weeks) { arr.push(this.getSomeWeek(num)) num++ } return arr } } return Week })() const getYearWeek = (y, index) => { return new Week(y).showWeek(index) } /** * 判断是否是机构 * @param { string | number } type * @return { boolean } */ function isOrg(type) { return (type - 0) < 600 } export { thisRouterObjFn, json2url, json2path, json2formData, isHasImg, findInArr, findInArrJson, isJsonNull, removeArrByValue, removeArrJsonByValue, findButtonAuthoritys, guid, cloneDeep, getYearWeek, isOrg }