util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;
  2. //读取cookies
  3. function getCookie(name)
  4. {
  5. var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  6. if(arr=document.cookie.match(reg))
  7. return unescape(arr[2]);
  8. else
  9. return null;
  10. };
  11. //标准时间格式配置
  12. Date.prototype.format =function(format){
  13. var o = {
  14. "M+" : this.getMonth()+1, //month
  15. "d+" : this.getDate(), //day
  16. "h+" : this.getHours(), //hour
  17. "m+" : this.getMinutes(), //minute
  18. "s+" : this.getSeconds(), //second
  19. "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  20. "S" : this.getMilliseconds() //millisecond
  21. }
  22. if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
  23. (this.getFullYear()+"").substr(4- RegExp.$1.length));
  24. for(var k in o)if(new RegExp("("+ k +")").test(format))
  25. format = format.replace(RegExp.$1,
  26. RegExp.$1.length==1? o[k] :
  27. ("00"+ o[k]).substr((""+ o[k]).length));
  28. return format;
  29. };
  30. //根据getIdUrl获取对应的序列值
  31. function getSeqId(getIdUrl) {
  32. var id;
  33. $.ajax({
  34. async: false,
  35. type: 'GET',
  36. url: basePath + getIdUrl,
  37. dataType: 'json',
  38. success: function(data){
  39. id = data.id;
  40. }
  41. });
  42. return id;
  43. };
  44. //处理日期
  45. function parseDate(date){
  46. var y = date.getFullYear(), m = date.getMonth() + 1, d = date.getDate(),
  47. h = date.getHours(), i = date.getMinutes();
  48. var now = new Date(), _y = now.getFullYear(), _m = now.getMonth() + 1, _d = now.getDate();
  49. if(_y != y) {
  50. return y + '-' + m + '-' + d + ' ' + h + ':' + i;
  51. } else {
  52. if(_m != m) {
  53. return m + '月' + d + '号' + h + '点' + i + '分';
  54. } else {
  55. if(_d != d) {
  56. return d + '号' + h + '点' + i + '分';
  57. } else {
  58. return (h < 12 ? '上午' : '下午' ) + h + '点' + i + '分';
  59. }
  60. }
  61. }
  62. };
  63. //读取链接参数
  64. function getUrlParam(name, link){
  65. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  66. var search;
  67. if (link) {search = link.substr(link.indexOf("?"),link.length);}
  68. else { search = window.location.search;}
  69. var r = search.substr(1).match(reg);
  70. if (r != null)
  71. return decodeURI(r[2]);
  72. return null;
  73. };
  74. //返回当前页面宽度
  75. function pageWidth() {
  76. if ($.browser.msie) {
  77. return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
  78. } else {
  79. // - padding
  80. return self.innerWidth - 30;
  81. }
  82. };
  83. // close page
  84. function closePage() {
  85. top.window.opener = top;
  86. top.window.open('','_self','');
  87. top.window.close();
  88. };
  89. //judge excepetion
  90. function judgeException(result, callback) {
  91. var e = result.exceptionInfo;
  92. if(e) {
  93. if(e == 'ERR_NETWORK_SESSIONOUT') {
  94. dialog.show('错误', '请先登录!');
  95. } else {
  96. dialog.show('出现异常', '请稍后再试...');
  97. }
  98. } else {
  99. if(callback) callback.call();
  100. }
  101. };
  102. // modal dialog 需要在页面中添加dialog div及其样式
  103. var dialog = {
  104. show: function(title, content, timeout, callback){
  105. var back = $('.modal-backdrop'), modal = $('#dialog'),
  106. tt = $('.modal-title', modal), tb = $('.modal-body', modal);
  107. back.css('display', 'block');
  108. modal.css('display', 'block');
  109. tt.text(title);
  110. if(timeout && timeout > 0) {
  111. content = '<div><strong class="text-success text-xl" id="timeout">' + timeout + '</strong>&nbsp;秒&nbsp;' + content + '</div>';
  112. }
  113. tb.html(content);
  114. if(timeout) {
  115. var pre = 1000;
  116. if(timeout <=0 ) {// auto close
  117. pre = 1500;
  118. if(!callback)
  119. callback = dialog.hide;
  120. }
  121. var timer = function(t) {
  122. setTimeout(function(){
  123. if(t > -1) {
  124. $('#timeout').text(t--);
  125. timer(t);
  126. } else {
  127. callback && callback.call();
  128. }
  129. }, pre);
  130. };
  131. timer(timeout);
  132. }
  133. },
  134. hide: function() {
  135. var back = $('.modal-backdrop'), modal = $('#dialog');
  136. back.css('display', 'none');
  137. modal.css('display', 'none');
  138. }
  139. };
  140. // loading 需要在页面中添加loading div及其样式
  141. var setLoading = function(isLoading) {
  142. $('.loading-container').css('display', isLoading ? 'block' : 'none');
  143. };
  144. //判断对象是否为数组,Ext3写法----好吧,其实jq已经写好了$.isArray(o);
  145. // var isArray = function(o) {
  146. // return toString.apply(o) === '[object Array]';
  147. // };
  148. /**
  149. * string:原始字符串
  150. * substr:子字符串
  151. * isIgnoreCase:忽略大小写
  152. */
  153. function contains(string,substr,isIgnoreCase){
  154. if(isIgnoreCase){
  155. string=string.toLowerCase();
  156. substr=substr.toLowerCase();
  157. }
  158. var startChar=substr.substring(0,1);
  159. var strLen=substr.length;
  160. for(var j=0;j<string.length-strLen+1;j++){
  161. if(string.charAt(j)==startChar){//如果匹配起始字符,开始查找
  162. if(string.substring(j,j+strLen)==substr){//如果从j开始的字符与str匹配,那ok
  163. return true;
  164. }
  165. }
  166. }
  167. return false;
  168. };