var utils = { getUrlName: function(name) { // 获取url中特定的参数 var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i")); if (result == null || result.length < 1) { return ""; } return result[1]; }, fullZero: function(num) { if (num < 10) return '0' + num else return num }, //格式化日期 如:2017-10-10 22:33:32 format: function(str) { if (str == "1900-01-01T00:00:00") { str = ""; return str; } else if (str == "0001-01-01T00:00:00") { ///这种情况是在做滨旅建管系统时用sqlserver数据库时遇到的 str = ""; return str; } else if (str == "-") { ///在做滨旅建管时存的是—,然后就让其显示— str = "-"; return str; } else if (str != undefined && str != "" && str != null) { /////如果找到"-",不等于负1 if (str.toString().indexOf('-') != -1) { str = str.replace(/-/g, "/"); //将-替换为/,因为ios与ie浏览器中不支持-和T } /////如果找到T,不等于负1 if (str.toString().indexOf('T') != -1) { str = str.replace(/T/g, ' '); ///去掉日期中的T,因为ios与ie浏览器中不支持-和T } /////如果找到".",不等于负1 if (str.toString().indexOf('.') != -1) { str = str.slice(0, str.indexOf(".")); ///如果含有毫秒,就将毫秒去掉 } var formatDate = new Date(Date.parse(str)); var yy = formatDate.getFullYear(); var MM = formatDate.getMonth() + 1; if (MM < 10) { MM = '0' + MM; } var dd = formatDate.getDate(); if (dd < 10) { dd = '0' + dd; } var setDate = yy + "-" + MM + "-" + dd; return setDate; } else { str = ""; return str; } }, ////获取当前年份 currentYear: function() { var date = new Date(); var yyyy = date.getFullYear().toString(); return yyyy; }, currentMonth: function() { var date = new Date(); var mm = date.getMonth() + 1; if (mm < 10) return '0' + mm else return mm }, ////格式化日期获取其年份 formatGetYear: function(str) { var formatDate = new Date(Date.parse(str)); var yy = formatDate.getFullYear(); return yy; }, ////格式化日期获取其月份 formatGetMonth: function(str) { var formatDate = new Date(Date.parse(str)); var MM = formatDate.getMonth() + 1; return MM; }, formatDate: function(date, fmt) { date = date == undefined ? new Date() : date; // date = typeof date == 'number' ? new Date(date) : date; date = new Date(date); fmt = fmt || 'yyyy-MM-dd HH:mm:ss'; var obj = { 'y': date.getFullYear(), // 年份,注意必须用getFullYear 'M': date.getMonth() + 1, // 月份,注意是从0-11 'd': date.getDate(), // 日期 'q': Math.floor((date.getMonth() + 3) / 3), // 季度 'w': date.getDay(), // 星期,注意是0-6 'H': date.getHours(), // 24小时制 'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制 'm': date.getMinutes(), // 分钟 's': date.getSeconds(), // 秒 'S': date.getMilliseconds() // 毫秒 }; var week = ['天', '一', '二', '三', '四', '五', '六']; for (var i in obj) { fmt = fmt.replace(new RegExp(i + '+', 'g'), function(m) { var val = obj[i] + ''; if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val]; for (var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val; return m.length == 1 ? val : val.substring(val.length - m.length); }); } return fmt; }, //判断是否为obj 并排除 null 因为typeof null为object,故先排除null isObject(obj) { return obj !== null && typeof obj === 'object' }, isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; }, // 序列化参数 param(data) { // If this is not an object, defer to native stringification. if (!this.isObject(data)) { return ((data == null) ? "" : data.toString()); } var buffer = []; // Serialize each key in the object. for (var name in data) { if (!data.hasOwnProperty(name)) { continue; } var value = data[name]; buffer.push(encodeURIComponent(name) + "=" + encodeURIComponent((value == null) ? "" : value)); } // Serialize the buffer and clean it up for transportation. var source = buffer.join("&").replace(/%20/g, "+"); return (source); }, trim: function(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); }, ////cookie('set_get_clickID', "");//清空cookie ////ookie("MyCssSkin", id, { path: '/', expires: 2 });//加上cookie cookie: function(name, value, options) { /// 设置cookie if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); } var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = this.trim(cookies[i]); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } } } Vue.prototype.utils = utils Vue.prototype.$http = axios window.$http = axios