import constant from './constant' const vKey = location.host export default { 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 }, 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('') }, 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' }, 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 // }, 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 }, 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 } }