// 头部返回按钮事件 $('.j-header .header-left').on('click', function () { window.history.back() }) window.afterClickBack = function () { console.log('afterClickBack', location.href) } var vTools = { // 公共前缀url baseUrl: '/entniche', // 公共header commonHeader: { 'version': 'v2.9.6' }, errorCodeMap: { 1001: '需要重新登录', 1002: '缺失参数', 1003: '没有权限', }, $env: {}, loadingTransition: 300, redirectURi: [ '/phone/bind', '/phone/sendidentcode', '/ent/myents', '/ent/entinfo', '/ent/selectent', '/ent/association', '/power/persons', '/department/persons', '/department/add', '/department/update', '/department/delete', '/department/detail', '/department/move', '/department/childrens', '/department/setadmin', '/person/manager', '/person/add', '/person/detail', '/person/delete', '/person/update', '/person/move', '/framework/delete', '/framework/move', '/power/set', '/ent/framework', '/ent/add', '/person/pcManager' ], // 公共ajax封装 ajax: function (options) { var that = this // var options = { // url: url, String // data: data, Object // type: type, String: 'GET'/'POST' // contentType: contentType, String: 'application/x-www-form-urlencoded'/'application/json;charset=UTF-8' // success: success, Function // beforeSend: beforeSend, Function // error: error, Function, // loading: { // content: '加载中...', // bgc: 'black' // }, Object // } options.contentType = options.contentType || 'application/json;charset=UTF-8' if (options.contentType === 'application/json;charset=UTF-8') { options.data = JSON.stringify(options.data) || '{}' } else { options.data = options.data || {} } if (options.loading) { var loading = this.jLoading(options.loading) } // 页面baseUrl重写 var inRedirectUriArr = false; var newUrl = options.url; if(options.url.indexOf('?') > -1){ newUrl = options.url.split('?')[0]; } for (var i = 0; i < this.redirectURi.length; i++) { if (newUrl == this.redirectURi[i]) { inRedirectUriArr = true break } } if (inRedirectUriArr) { this.baseUrl = '/entbase' } else { this.baseUrl = '/entnicheNew' } // 执行请求 $.ajax({ url: this.baseUrl + options.url, type: options.type, dataType: 'json', contentType: options.contentType || 'application/json;charset=UTF-8', headers: this.commonHeader, // processData: options.processData || true, data: options.data, beforeSend: function (res) { options.beforeSend && options.beforeSend(res) }, success: function (res) { if (options.loading) { loading.hide(function(){ options.success && options.success(res); }) } else { options.success && options.success(res); } }, error: function (res) { loading.hide(function () { that.jLoading({ iconHide: true, content: '请求失败', duration: 1500, callback: function() { options.error && options.error(res); } }) }) }, complete: function () { if (options.loading) { loading.hide(function() { options.complete && options.complete(); }) } else { options.complete && options.complete(); } } }); }, // 传入你要获取的参数的名字 getParam: function (name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); //获取url中'?'符后的字符串并正则匹配 var context = ''; if (r != null) context = r[2]; // 释放变量 reg = null; r = null; return context == null || context == '' || context == 'undefined' ? '' : context; }, // 数组对象根据某一个值进行冒泡排序 // arr 数组 // value 字符串 bSort: function (arr, value) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (var j = 0; j < len - 1 - i; j++) { // 相邻元素两两对比,元素交换,大的元素交换到后面 if (arr[j][value] > arr[j + 1][value]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }, // 格式化金钱的函数 // s: 金额(number) 必传 // n: 保留小数的位数(int:0-100) formatMoney: function (s, n) { if (n === undefined) { n = -1 } else { n = n > 0 && n <= 20 ? n : 2; } var intS = parseInt(s) var point = '.' var left; var right; s = parseFloat((s + '').replace(/[^\d\.-]/g, '')) // 没传n,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数) if (n === -1) { if (s === intS) { n = 0 right = '' point = '' } else { n = 2 s = s.toFixed(n); right = s.split('.')[1]; } s = s + '' left = s.split('.')[0].split('').reverse(); } else { s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''; left = s.split('.')[0].split('').reverse(); right = s.split('.')[1]; } t = ''; for (i = 0; i < left.length; i++) { t += left[i] + ((i + 1) % 3 == 0 && (i + 1) != left.length ? ',' : ''); } var money = t.split('').reverse().join('') + point + right; return money; }, // 解决ios返回不刷新页面的问题 iosBackRefresh: function () { var isPageHide = false; window.addEventListener('pageshow', function () { if (isPageHide) { location.reload(); } }); window.addEventListener('pagehide', function () { isPageHide = true; }); }, // 解决移动端软键盘弹起遮挡输入框及ios下软键盘关闭后底部留白问题 // 解决键盘弹起'.j-footer'往上顶的问题 // 原始代码在:jianyuMobile:vip_subscribe分支/trial_info.html initScrollView: function (options) { options.inputClassName = options.inputClassName || 'input' options.bottom = options.bottom || '.j-footer' options.exclude = options.exclude || 'exclude' /* options = { // 输入框公共类名 String(为css选择器): 如'.j-input' inputClassName: inputClassName, // 底部按钮 String(为css选择器): 如'.j-footer' bottom: bottomContainer, // 排除的底部区域的button String(为字符串): 如'exclude',默认为'exclude' // 如果底部区域是隐藏的,此时键盘弹起在收起,就会显示,加上一个排除的类名,就不会进行隐藏了 exclude: exclude // =========废弃=========> // view属性废弃,view属性是在底部元素使用定位时候用的 // 滚动区域 String(为css选择器): 如'.container' // view: scrollView // ======================> } */ var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // var clientHeight = $(options.view).height() // var clientHeight = document.documentElement.clientHeight || document.body.clientHeight // function change() { // $(options.view || document.body).height(clientHeight) // } var clientHeight = document.documentElement.clientHeight || document.body.clientHeight; // change函数参考: https://blog.csdn.net/andyfine/article/details/82695236 function change() { var footer = $(options.bottom) // 首先判断footer此时有没有options.exclude的类名,有此类名则不会隐藏 // $('.ele').is(':hidden') // $('.ele').is(':visible') if (footer.hasClass(options.exclude)) return if (document.body.scrollHeight < clientHeight) { footer.hide() } else { footer.show() } } // 给所有input绑定一个focus事件,用来判断键盘弹起。执行完成就解绑该事件 function focusOnce() { $(options.inputClassName).on('focus', function (e) { e.target.scrollIntoView({ 'block': 'center' }) $(options.inputClassName).off('focus') }) } if (isAndroid) { // 在安卓中键盘弹起会触发window.onresize事件 // ios中则不会触发window.resize事件 change(); window.addEventListener('resize', change); } else { // 进入页面绑定,触发一次就解绑 focusOnce() } $(options.inputClassName).on('blur', function (e) { setTimeout(function() { var dom = document.activeElement; if (dom.className.includes(options.inputClassName.replace(/\./g))) { if (isAndroid) { dom.scrollIntoView({ 'block': 'center' }) } else { // dom.scrollIntoView({'block': 'bottom'}) } } else { window.scroll(0, 0) // 当页面上没有获得焦点的input时候就说明键盘收起来了。 // 给input绑定focus事件用来监听其弹起 focusOnce() } }, 50) }) }, // 通过userAgent获取用户手机操作系统类型 androidOrIOS: function () { var u = navigator.userAgent.toLowerCase(); var app = navigator.appVersion; var agent = null; if (/iphone|ipod|ipad|ios/.test(u)) { agent = 'ios' } else { agent = 'android' } return agent }, // 带动画的weui picker的显示隐藏 pickerAnimatedShow: function (selector, f) { if (f) { // 添加进场动画,并显示 $(selector).find('.weui-mask').removeClass('weui-animate-fade-out').addClass('weui-animate-fade-in') $(selector).find('.weui-picker').removeClass('weui-animate-slide-down').addClass('weui-animate-slide-up') $(selector).show(300); } else { // 添加离场动画,并隐藏 $(selector).find('.weui-mask').removeClass('weui-animate-fade-in').addClass('weui-animate-fade-out') $(selector).find('.weui-picker').removeClass('weui-animate-slide-up').addClass('weui-animate-slide-down') $(selector).hide(300); } }, // 键盘弹起和收起的事件(只在安卓有效果) keyboardUpAndDown: function (upCallBack, downCallBack) { var clientHeight = document.documentElement.clientHeight || document.body.clientHeight; $(window).on('resize', function () { var nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight; if (clientHeight > nowClientHeight) { // 键盘弹出的事件处理 upCallBack && upCallBack() console.log('键盘弹出') } else { // 键盘收起的事件处理 downCallBack && downCallBack() console.log('键盘收起') } }); }, // 取[m, n]随机数 getRandomNumber: function (min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }, // 获取唯一的uuid // https://www.kancloud.cn/ifeng/js100/622666 getRandomUuid: function () { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }, // 获取随机字符串 // 不传参数则获取长度不固定的字符串 getRandomString: function (len) { var randomString = ''; if (len) { /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; var maxPos = $chars.length; for (i = 0; i < len; i++) { randomString += $chars.charAt(Math.floor(Math.random() * maxPos)); } } else { // Math.random() 生成随机数字, eg: 0.123456 // .toString(36) 转化成36进制 : "0.4fzyo82mvyr" // .substring(2) 去掉前面两位 : "yo82mvyr" // .slice(-8) 截取最后八位 : "yo82mvyr" randomString = Math.random().toString(36).substring(2) } return randomString; }, // FROM: https://www.jianshu.com/p/90ed8b728975 // 比较两个兑现是否相等 // 返回true为相等,返回false为不相等 deepCompare: function (x, y) { var i, l, leftChain, rightChain; function compare2Objects(x, y) { var p; // remember that NaN === NaN returns false // and isNaN(undefined) returns true if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') { return true; } // Compare primitives and functions. // Check if both arguments link to the same object. // Especially useful on the step where we compare prototypes if (x === y) { return true; } // Works in case when functions are created in constructor. // Comparing dates is a common scenario. Another built-ins? // We can even handle functions passed across iframes if ((typeof x === 'function' && typeof y === 'function') || (x instanceof Date && y instanceof Date) || (x instanceof RegExp && y instanceof RegExp) || (x instanceof String && y instanceof String) || (x instanceof Number && y instanceof Number)) { return x.toString() === y.toString(); } // At last checking prototypes as good as we can if (!(x instanceof Object && y instanceof Object)) { return false; } if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) { return false; } if (x.constructor !== y.constructor) { return false; } if (x.prototype !== y.prototype) { return false; } // Check for infinitive linking loops if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) { return false; } // Quick checking of one object being a subset of another. // todo: cache the structure of arguments[0] for performance for (p in y) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } } for (p in x) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } switch (typeof (x[p])) { case 'object': case 'function': leftChain.push(x); rightChain.push(y); if (!compare2Objects(x[p], y[p])) { return false; } leftChain.pop(); rightChain.pop(); break; default: if (x[p] !== y[p]) { return false; } break; } } return true; } if (arguments.length < 1) { return true; //Die silently? Don't know how to handle such case, please help... // throw "Need two or more arguments to compare"; } for (i = 1, l = arguments.length; i < l; i++) { leftChain = []; //Todo: this can be cached rightChain = []; if (!compare2Objects(arguments[0], arguments[i])) { return false; } } return true; }, // 全局loading框 jLoading: function(options) { // 设置options参数的默认值 options.content = options.content || 'loading...' options.bgc = options.bgc || 'transparent' options.icon = options.icon || 'j-loading-icon' options.duration = options.duration || 0 // 将其拼接成width: 5rem;的形式 options.width = options.width ? 'width:' + options.width + ';' : '' // 结构 //
loading
//' + options.content + '