import router from '../../router' import store from '../../store' import constant from './constant' const vKey = location.host export function uid() { return this.uid() } export function bid() { return this.bid() } export default { // ================================Common Use============================================== transDate: function(val, pattern) { if (val) { const time = new Date() time.setTime(val) if (time instanceof Date) { return this.formatDate((pattern) || 'yyyy-MM-dd', time) } } return '' }, transDateStr: function(val, pattern) { if (val) { const time = new Date(val) return this.transDate(time, pattern) } return '' }, transServDate: function(val) { return this.transDate(val, constant.DATE_PATTERN.DATE_TIME_h) }, transTime(val, pattern) { if (val) { const time = new Date(); time.setTime(val); if (time instanceof Date) { return this.formatDate(pattern || "yyyy-MM-dd hh:mm:ss", time); } } return ""; }, transMinute(val, pattern) { if (val) { const time = new Date(); time.setTime(val); if (time instanceof Date) { return this.formatDate(pattern || "yyyy-MM-dd hh:mm:ss", time); } } return ""; }, isArrayFn: function(value) { if (typeof Array.isArray === 'function') { return Array.isArray(value) } else { return Object.prototype.toString.call(value) === '[object Array]' } }, checkLoginStatus: async function(statusCode) { if (statusCode && statusCode === -401) { this.logout() } }, logout: async function() { await store.dispatch('user/logout') const userType = this.currUserType() if (userType === '2') { router.push('/bizLogin') } else { router.push('/login') } }, transDcMap: function(arr) { const tMap = {} if (arr) { arr.forEach(item => { tMap[item.value] = item.label }) } return tMap }, myBrowser: function() { const userAgent = navigator.userAgent // 取得浏览器的userAgent字符串 // console.log('userAgent:', userAgent) const isOpera = userAgent.indexOf('Opera') > -1 if (isOpera) { return 'Opera' } // 判断是否Opera浏览器 if (userAgent.indexOf('Firefox') > -1) { return 'FF' } // 判断是否Firefox浏览器 if (userAgent.indexOf('Chrome') > -1) { return 'Chrome' } if (userAgent.indexOf('Safari') > -1) { return 'Safari' } // 判断是否Safari浏览器 if (userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera) { return 'IE' } // 判断是否IE浏览器 return '' }, isIE: function() { if (!!window.ActiveXObject || 'ActiveXObject' in window) { return true } else { return false } }, uuid: function(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') var uuid = []; var i radix = radix || chars.length if (len) { // Compact form for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix] } else { // rfc4122, version 4 form var r // rfc4122 requires these characters uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-' uuid[14] = '4' // Fill in random data. At i==19 set the high bits of clock sequence as // per rfc4122, sec. 4.1.5 for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | Math.random() * 16 uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r] } } } return uuid.join('') }, castEval: function(val) { return val ? eval('(' + val + ')') : val }, castString: function(val) { return JSON.stringify(val) }, getRandomNumberByRange: function(start, end) { return Math.floor(Math.random() * (end - start) + start) }, getUrlParam: function(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null }, // 金额格式化,整数部分每3位用逗号分隔,支持带有正负号以及小数部分 formatMoney: function(amt) { if (!amt) return '' if (amt.length <= 3) { return amt } if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(amt)) { return amt } var a = RegExp.$1; var b = RegExp.$2; var c = RegExp.$3 var re = new RegExp() re.compile('(\\d)(\\d{3})(,|$)') while (re.test(b)) { b = b.replace(re, '$1,$2$3') } return a + '' + b + '' + c }, formatDate: function(fmt, date) { // author: meizz var 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 (var 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 }, nvl_money: function(val) { if (val) { return '¥' + this.formatMoney(val) } return '' }, nvl_num: function(val) { return val === 0 ? 0 : val.toFixed(2) }, hashCode: function(str) { let hash = 0 if (str.length === 0) return hash for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i) hash = ((hash << 5) - hash) + char hash = hash & hash // Convert to 32bit integer } return hash }, parseTime: function(time, pattern) { if (arguments.length === 0 || !time) { return null } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } else if (typeof time === 'string') { time = time.replace(new RegExp(/-/gm), '/') } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return time_str }, // ================================System Use============================================== setUser: function(value) { localStorage.setItem(this.UserKey(), JSON.stringify(value)) }, currUser: function() { const userKey = localStorage.getItem(this.UserKey()) if (userKey) { const user = JSON.parse(userKey || '') // //console.log('curr user:', user) if (user) { return user } } // this.logout() return null }, uid: function() { return this.currUser() ? this.currUser().id : null }, isAdmin: function() { return this.uid() === '1' }, isAdminRole: function() { if (this.currUser().dataRoles['DATA']) { return this.currUser().dataRoles['DATA'].includes('ADMIN_DATA') } return false }, setBiz: function(value) { localStorage.setItem(this.BizKey(), JSON.stringify(value)) }, currBiz: function() { return JSON.parse(localStorage.getItem(this.BizKey()) || null) }, bid: function() { return this.currBiz() ? this.currBiz().id : null }, setUserType: function(value) { localStorage.setItem(vKey + '_' + constant.KEY_USER_TYPE, value) }, currUserType: function() { return localStorage.getItem(vKey + '_' + constant.KEY_USER_TYPE) || '1' }, setCode: function(value) { localStorage.setItem(constant.KEY_CODE, value) }, currCode: function() { return localStorage.getItem(constant.KEY_CODE) || null }, setMenu: function(value) { localStorage.setItem(this.MenuKey(), JSON.stringify(value)) }, currMenu: function() { const menuVal = this.getMenuVal() if (menuVal && menuVal !== 'undefined') { return JSON.parse(menuVal) } else { return [] } }, getMenuVal: function() { return localStorage.getItem(this.MenuKey()) }, removeStorage: function(_key) { localStorage.removeItem(_key) }, // ================================ Common Key ============================================== // UserKey: function() { // return window.location.host + '_' + constant.KEY_USER + this.currUserType() // }, // // MenuKey: function() { // return window.location.host + '_' + constant.KEY_USER + this.currUserType() + '_' + constant.KEY_USER_MENU // }, UsrPwdKey: function(_usr) { return this.hashCode(window.location.host + '_' + _usr + '_password') }, UserKey: function() { return this.hashCode(window.location.host + '_' + constant.KEY_USER + this.currUserType()) }, BizKey: function() { return this.hashCode(window.location.host + '_' + constant.KEY_BIZ + this.currUserType()) }, MenuKey: function() { return this.hashCode(window.location.host + '_' + constant.KEY_USER + this.currUserType() + '_' + constant.KEY_USER_MENU) }, replaceFileUrl: function(urlPath) { if (urlPath != null && urlPath !== '') { urlPath = urlPath.replaceAll('/server/FileController/download/', constant.BASE_URI + '/FileController/download/') } return urlPath }, isValidCoordinate(lng, lat) { if (!lng || !lat) { return false } if (lng.toString().substring(0, lng.toString().lastIndexOf('.')).length !== 8) { return false } return lat.toString().substring(0, lat.toString().lastIndexOf('.')).length === 7 }, isValidCoordinateBack(lng, lat) { if (lat === 0 || lng === 0) { return false } // 校验纬度是否在 -90 到 90 之间 if (lat < -90 || lat > 90) { return false } // 校验经度是否在 -180 到 180 之间 return !(lng < -180 || lng > 180) }, getHoursDifference(date1, date2) { const timeDiff = Math.abs(date2 - date1) return Math.floor(timeDiff / (60 * 60 * 1000)) }, transCGCS2000toWGS84(x, y) { // 大地2000,平面坐标转经纬度,3度带 // 3.1415926535898/180.0 const iPI = 0.0174532925199433 // 3度带 // const zoneWide = 40 // 长半轴 const a = 6378137 // 扁率 const f = 1 / 298.257222101 const projNo = parseInt(x / 1000000) let longitude0 = projNo * 3 longitude0 = longitude0 * iPI const X0 = projNo * 1000000 + 500000 const Y0 = 0 const xval = x - X0 const yval = y - Y0 const e2 = 2 * f - f * f const e1 = (1.0 - Math.sqrt(1 - e2)) / (1.0 + Math.sqrt(1 - e2)) const ee = e2 / (1 - e2) const M = yval const u = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256)) const fai = u + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.sin(2 * u) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.sin(4 * u) + (151 * e1 * e1 * e1 / 96) * Math.sin(6 * u) + (1097 * e1 * e1 * e1 * e1 / 512) * Math.sin(8 * u) const C = ee * Math.cos(fai) * Math.cos(fai) const T = Math.tan(fai) * Math.tan(fai) const NN = a / Math.sqrt(1.0 - e2 * Math.sin(fai) * Math.sin(fai)) const R = a * (1 - e2) / Math.sqrt((1 - e2 * Math.sin(fai) * Math.sin(fai)) * (1 - e2 * Math.sin(fai) * Math.sin(fai)) * (1 - e2 * Math.sin(fai) * Math.sin(fai))) const D = xval / NN const longitude1 = longitude0 + (D - (1 + 2 * T + C) * D * D * D / 6 + (5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) * D * D * D * D * D / 120) / Math.cos(fai) const latitude1 = fai - (NN * Math.tan(fai) / R) * (D * D / 2 - (5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D / 24 + (61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D * D * D * D / 720) return { lng: longitude1 / iPI, lat: latitude1 / iPI } }, beforeUploadJustWordExcel(file) { const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1) const whiteList = ['pdf', 'doc', 'docx', 'xls', 'xlsx'] if (whiteList.indexOf(fileSuffix) === -1) { this.$message.error('上传文件只能是 pdf、doc、docx、xls、xlsx格式') return false } const isLt2M = file.size / 1024 / 1024 < 50 if (!isLt2M) { this.$message.error('上传文件大小不能超过 5MB') return false } } }