util.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. strToJson(str) {
  179. let obj = str.replace(/\u0000|\u0001|\u0002|\u0003|\u0004|\u0005|\u0006|\u0007|\u0008|\u0009|\u000a|\u000b|\u000c|\u000d|\u000e|\u000f|\u0010|\u0011|\u0012|\u0013|\u0014|\u0015|\u0016|\u0017|\u0018|\u0019|\u001a|\u001b|\u001c|\u001d|\u001e|\u001f|\u007F/g, "")
  180. let json = eval('(' + obj + ')');
  181. return json;
  182. },
  183. ///format格式化缩写为fmt,两个参数,前者是日期格式,后者是日期
  184. datefmt:function (fmt, date) {
  185. ///console.log(date);
  186. if (date == "1900-01-01T00:00:00") {
  187. date = "";
  188. return date;
  189. } else if (date == "0001-01-01T00:00:00") {
  190. ///这种情况是在做滨旅建管系统时用sqlserver数据库时遇到的
  191. date = "";
  192. return date;
  193. } else if (date == "-") {
  194. ///在做滨旅建管时存的是—,然后就让其显示—
  195. date = "-";
  196. return date;
  197. } else if (date != undefined && date != "" && date != null) {
  198. /////如果找到"-",不等于负1
  199. if (date.toString().indexOf('-') != -1) {
  200. date = date.replace(/-/g, "/"); //将-替换为/,因为ios与ie浏览器中不支持-和T
  201. }
  202. /////如果找到T,不等于负1
  203. if (date.toString().indexOf('T') != -1) {
  204. date = date.replace(/T/g, ' '); ///去掉日期中的T,因为ios与ie浏览器中不支持-和T
  205. }
  206. /////如果找到".",不等于负1
  207. if (date.toString().indexOf('.') != -1) {
  208. date = date.slice(0, date.indexOf(".")); ///如果含有毫秒,就将毫秒去掉
  209. }
  210. var formatDate = new Date(Date.parse(date));
  211. var o = {
  212. "M+": formatDate.getMonth() + 1, //月份
  213. "d+": formatDate.getDate(), //日
  214. "H+": formatDate.getHours(), //小时
  215. "m+": formatDate.getMinutes(), //分
  216. "s+": formatDate.getSeconds(), //秒
  217. "q+": Math.floor((formatDate.getMonth() + 3) / 3), //季度
  218. "S": formatDate.getMilliseconds() //毫秒
  219. };
  220. if (/(y+)/.test(fmt))
  221. fmt = fmt.replace(RegExp.$1, (formatDate.getFullYear() + "").substr(4 - RegExp.$1.length));
  222. for (var k in o)
  223. if (new RegExp("(" + k + ")").test(fmt))
  224. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  225. return fmt;
  226. } else {
  227. date = "";
  228. return date;
  229. }
  230. },
  231. getLocalDate1:function () {
  232. var objDate = new Date();
  233. var str, date, colorhead, colorfoot;
  234. var yy = objDate.getFullYear();
  235. var MM = objDate.getMonth() + 1;
  236. if (MM < 10) MM = '0' + MM;
  237. var dd = objDate.getDate();
  238. if (dd < 10) dd = '0' + dd;
  239. var hh = objDate.getHours();
  240. if (hh < 10) hh = '0' + hh;
  241. var mm = objDate.getMinutes();
  242. if (mm < 10) mm = '0' + mm;
  243. var ss = objDate.getSeconds();
  244. if (ss < 10) ss = '0' + ss;
  245. date = yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss;
  246. var mins = objDate.getTime() - new Date(date).getTime();
  247. mins < 10 ? '00' + mins : mins < 100 ? '0' + mins : mins;
  248. str = yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + "." + mins;
  249. return str;
  250. }
  251. }
  252. Vue.prototype.utils = utils
  253. Vue.prototype.$http = axios
  254. window.$http = axios