util.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var utils = {
  2. getUrlName: function(name) {
  3. // 获取url中特定的参数
  4. var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
  5. if (result == null || result.length < 1) {
  6. return "";
  7. }
  8. return result[1];
  9. },
  10. fullZero: function(num) {
  11. if (num < 10) return '0' + num
  12. else return num
  13. },
  14. //格式化日期 如:2017-10-10 22:33:32
  15. format: function(str) {
  16. if (str == "1900-01-01T00:00:00") {
  17. str = "";
  18. return str;
  19. } else if (str == "0001-01-01T00:00:00") {
  20. ///这种情况是在做滨旅建管系统时用sqlserver数据库时遇到的
  21. str = "";
  22. return str;
  23. } else if (str == "-") {
  24. ///在做滨旅建管时存的是—,然后就让其显示—
  25. str = "-";
  26. return str;
  27. } else if (str != undefined && str != "" && str != null) {
  28. /////如果找到"-",不等于负1
  29. if (str.toString().indexOf('-') != -1) {
  30. str = str.replace(/-/g, "/"); //将-替换为/,因为ios与ie浏览器中不支持-和T
  31. }
  32. /////如果找到T,不等于负1
  33. if (str.toString().indexOf('T') != -1) {
  34. str = str.replace(/T/g, ' '); ///去掉日期中的T,因为ios与ie浏览器中不支持-和T
  35. }
  36. /////如果找到".",不等于负1
  37. if (str.toString().indexOf('.') != -1) {
  38. str = str.slice(0, str.indexOf(".")); ///如果含有毫秒,就将毫秒去掉
  39. }
  40. var formatDate = new Date(Date.parse(str));
  41. var yy = formatDate.getFullYear();
  42. var MM = formatDate.getMonth() + 1;
  43. if (MM < 10) {
  44. MM = '0' + MM;
  45. }
  46. var dd = formatDate.getDate();
  47. if (dd < 10) {
  48. dd = '0' + dd;
  49. }
  50. var setDate = yy + "-" + MM + "-" + dd;
  51. return setDate;
  52. } else {
  53. str = "";
  54. return str;
  55. }
  56. },
  57. ////获取当前年份
  58. currentYear: function() {
  59. var date = new Date();
  60. var yyyy = date.getFullYear().toString();
  61. return yyyy;
  62. },
  63. currentMonth: function() {
  64. var date = new Date();
  65. var mm = date.getMonth() + 1;
  66. if (mm < 10) return '0' + mm
  67. else return mm
  68. },
  69. ////格式化日期获取其年份
  70. formatGetYear: function(str) {
  71. var formatDate = new Date(Date.parse(str));
  72. var yy = formatDate.getFullYear();
  73. return yy;
  74. },
  75. ////格式化日期获取其月份
  76. formatGetMonth: function(str) {
  77. var formatDate = new Date(Date.parse(str));
  78. var MM = formatDate.getMonth() + 1;
  79. return MM;
  80. },
  81. formatDate: function(date, fmt) {
  82. date = date == undefined ? new Date() : date;
  83. // date = typeof date == 'number' ? new Date(date) : date;
  84. date = new Date(date);
  85. fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
  86. var obj = {
  87. 'y': date.getFullYear(), // 年份,注意必须用getFullYear
  88. 'M': date.getMonth() + 1, // 月份,注意是从0-11
  89. 'd': date.getDate(), // 日期
  90. 'q': Math.floor((date.getMonth() + 3) / 3), // 季度
  91. 'w': date.getDay(), // 星期,注意是0-6
  92. 'H': date.getHours(), // 24小时制
  93. 'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
  94. 'm': date.getMinutes(), // 分钟
  95. 's': date.getSeconds(), // 秒
  96. 'S': date.getMilliseconds() // 毫秒
  97. };
  98. var week = ['天', '一', '二', '三', '四', '五', '六'];
  99. for (var i in obj) {
  100. fmt = fmt.replace(new RegExp(i + '+', 'g'), function(m) {
  101. var val = obj[i] + '';
  102. if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
  103. for (var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
  104. return m.length == 1 ? val : val.substring(val.length - m.length);
  105. });
  106. }
  107. return fmt;
  108. },
  109. //判断是否为obj 并排除 null 因为typeof null为object,故先排除null
  110. isObject(obj) {
  111. return obj !== null && typeof obj === 'object'
  112. },
  113. isArray(obj) {
  114. return Object.prototype.toString.call(obj) === '[object Array]';
  115. },
  116. // 序列化参数
  117. param(data) {
  118. // If this is not an object, defer to native stringification.
  119. if (!this.isObject(data)) {
  120. return ((data == null) ? "" : data.toString());
  121. }
  122. var buffer = [];
  123. // Serialize each key in the object.
  124. for (var name in data) {
  125. if (!data.hasOwnProperty(name)) {
  126. continue;
  127. }
  128. var value = data[name];
  129. buffer.push(encodeURIComponent(name) + "=" + encodeURIComponent((value == null) ? "" : value));
  130. }
  131. // Serialize the buffer and clean it up for transportation.
  132. var source = buffer.join("&").replace(/%20/g, "+");
  133. return (source);
  134. },
  135. trim: function(str) {
  136. return str.replace(/(^\s*)|(\s*$)/g, "");
  137. },
  138. ////cookie('set_get_clickID', "");//清空cookie
  139. ////ookie("MyCssSkin", id, { path: '/', expires: 2 });//加上cookie
  140. cookie: function(name, value, options) {
  141. /// <summary>设置cookie</summary>
  142. if (typeof value != 'undefined') { // name and value given, set cookie
  143. options = options || {};
  144. if (value === null) {
  145. value = '';
  146. options.expires = -1;
  147. }
  148. var expires = '';
  149. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  150. var date;
  151. if (typeof options.expires == 'number') {
  152. date = new Date();
  153. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  154. } else {
  155. date = options.expires;
  156. }
  157. expires = '; expires=' + date.toUTCString();
  158. }
  159. var path = options.path ? '; path=' + (options.path) : '';
  160. var domain = options.domain ? '; domain=' + (options.domain) : '';
  161. var secure = options.secure ? '; secure' : '';
  162. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  163. } else {
  164. var cookieValue = null;
  165. if (document.cookie && document.cookie != '') {
  166. var cookies = document.cookie.split(';');
  167. for (var i = 0; i < cookies.length; i++) {
  168. var cookie = this.trim(cookies[i]);
  169. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  170. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  171. break;
  172. }
  173. }
  174. }
  175. return cookieValue;
  176. }
  177. }
  178. }
  179. Vue.prototype.utils = utils
  180. Vue.prototype.$http = axios
  181. window.$http = axios