common.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // 配置参数
  2. var config = {
  3. base_path : 'http://127.0.0.1:8080/cs_core/', // 项目默认路径
  4. ws_path : '127.0.0.1:8080/cs_core/',
  5. dlg : { // 默认对话框属性
  6. width : '800px',
  7. height : '480px',
  8. shade : 0.7,
  9. },
  10. thumbnail : { // 缩略图属性
  11. width : "100px",
  12. height: "80px",
  13. },
  14. pic_path : 'http://127.0.0.1:8080/cs_core/',
  15. window :{
  16. height: 960,
  17. width : 1280
  18. }
  19. };
  20. // 验证字符串是否以指定的str开头
  21. String.prototype.startWith=function(str){
  22. var reg=new RegExp("^" + str);
  23. return reg.test(this);
  24. }
  25. // 验证字符串是否以指定的str结尾
  26. String.prototype.endWith=function(str){
  27. var reg = new RegExp(str + "$");
  28. reg.IgnoreCase;
  29. return reg.test(this);
  30. }
  31. // 验证是否是图片url
  32. function isPicUrl(url){
  33. if('undefined' == url) return false;
  34. if(url.endWith('.BMP')
  35. || url.endWith('.bmp')
  36. || url.endWith('.png')
  37. || url.endWith('.PNG')
  38. || url.endWith('.png')
  39. || url.endWith('.GIF')
  40. || url.endWith('.jpg')
  41. || url.endWith('.JPG')
  42. || url.endWith('.jpeg')
  43. || url.endWith('.JPEG')){
  44. return true;
  45. }
  46. return false;
  47. }
  48. function toJSON(_obj){
  49. return JSON.parse(JSON.stringify(_obj));
  50. }
  51. $.fn.stringify = function() {
  52. return JSON.stringify(this);
  53. }
  54. //以下为修改jQuery Validation插件兼容Bootstrap的方法,没有直接写在插件中是为了便于插件升级
  55. $.validator.setDefaults({
  56. highlight: function (element) {
  57. $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
  58. },
  59. success: function (element) {
  60. element.closest('.form-group').removeClass('has-error').addClass('has-success');
  61. },
  62. errorElement: "span",
  63. errorPlacement: function (error, element) {
  64. if (element.is(":radio") || element.is(":checkbox")) {
  65. //error.appendTo(element.parent().parent().parent());
  66. error.appendTo(element.parent().parent());
  67. } else {
  68. error.appendTo(element.parent().parent());
  69. }
  70. },
  71. errorClass: "help-block m-b-none",
  72. validClass: "help-block m-b-none"
  73. });
  74. $.fn.extend({
  75. form_validate:function(){
  76. var _obj = $(this);
  77. if(!_obj.is('form')){
  78. return;
  79. }
  80. var v_rules = {}, v_messages = {};
  81. $('input,select,textarea').each(function(){
  82. var v = null, vm = null;
  83. eval("v = " + $(this).attr("validate"));
  84. if(v)
  85. {
  86. v_rules[$(this).attr('name')] = v;
  87. }
  88. eval("vm = " + $(this).attr("validateMessage"));
  89. if(vm)
  90. {
  91. v_messages[$(this).attr('name')] = vm;
  92. }
  93. });
  94. _obj.validate({
  95. rules: v_rules,
  96. messages: v_messages,
  97. submitHandler:function(form){
  98. // form.submit();
  99. $(form).ajaxSubmit({
  100. type:"post",
  101. dataType:"json",
  102. success:function(json){
  103. if(json.code == "200"){
  104. var index = parent.layer.getFrameIndex(window.name);
  105. // parent.location.reload();
  106. $(parent.document.getElementsByTagName('table')).each(function(){
  107. _id = $(this).attr('id');
  108. if(_id != '' && _id != undefined){
  109. parent.reflush(_id);
  110. }
  111. });
  112. parent.layer.close(index);
  113. // parent.layer.close(index);
  114. }else{
  115. layer.msg(json.data);
  116. }
  117. },
  118. error:function(){
  119. alert('error');
  120. }
  121. });
  122. }
  123. });
  124. }
  125. });
  126. $.fn.onlyNum = function() {
  127. $(this).keypress(function(event) {
  128. var eventObj = event || e;
  129. var keyCode = eventObj.keyCode || eventObj.which;
  130. if ((keyCode >= 48 && keyCode <= 57))
  131. return true;
  132. else
  133. return false;
  134. }).focus(function() {
  135. // 禁用输入法
  136. this.style.imeMode = 'disabled';
  137. }).bind("paste", function() {
  138. // 获取剪切板的内容
  139. var clipboard = window.clipboardData.getData("Text");
  140. if (/^\d+$/.test(clipboard))
  141. return true;
  142. else
  143. return false;
  144. });
  145. };
  146. // <summary>
  147. // 限制只能输入字母
  148. // </summary>
  149. // ----------------------------------------------------------------------
  150. $.fn.onlyAlpha = function() {
  151. $(this).keypress(
  152. function(event) {
  153. var eventObj = event || e;
  154. var keyCode = eventObj.keyCode || eventObj.which;
  155. if ((keyCode >= 65 && keyCode <= 90)
  156. || (keyCode >= 97 && keyCode <= 122))
  157. return true;
  158. else
  159. return false;
  160. }).focus(function() {
  161. this.style.imeMode = 'disabled';
  162. }).bind("paste", function() {
  163. var clipboard = window.clipboardData.getData("Text");
  164. if (/^[a-zA-Z]+$/.test(clipboard))
  165. return true;
  166. else
  167. return false;
  168. });
  169. };
  170. // ----------------------------------------------------------------------
  171. // <summary>
  172. // 限制只能输入数字和字母
  173. // </summary>
  174. // ----------------------------------------------------------------------
  175. $.fn.onlyNumAlpha = function() {
  176. $(this).keypress(
  177. function(event) {
  178. var eventObj = event || e;
  179. var keyCode = eventObj.keyCode || eventObj.which;
  180. if ((keyCode >= 48 && keyCode <= 57)
  181. || (keyCode >= 65 && keyCode <= 90)
  182. || (keyCode >= 97 && keyCode <= 122))
  183. return true;
  184. else
  185. return false;
  186. }).focus(function() {
  187. this.style.imeMode = 'disabled';
  188. }).bind("paste", function() {
  189. var clipboard = window.clipboardData.getData("Text");
  190. if (/^(\d|[a-zA-Z])+$/.test(clipboard))
  191. return true;
  192. else
  193. return false;
  194. });
  195. };
  196. $.extend({
  197. // 再次确认
  198. confirm:function(_tips, _submit, _cancle){
  199. layer.confirm(_tips, {
  200. btn: ['确定','取消'] //按钮
  201. },
  202. function(index){
  203. if(typeof(_submit) != 'undefined'){
  204. _submit();
  205. }
  206. layer.close(index);
  207. },
  208. function(index){
  209. if(typeof(_cancle) != 'undefined'){
  210. _cancle();
  211. }
  212. layer.close(index);
  213. }
  214. )
  215. },
  216. tips:function(_tips){ // 提示信息
  217. layer.msg(_tips);
  218. },
  219. loading:function(){
  220. layer.msg('加载中', {icon: 4});
  221. },
  222. closeAll:function(){ // 关闭所有layer弹出框
  223. layer.closeAll();
  224. },
  225. closeDlg:function(obj){ // 关闭当前打开的dlg
  226. var index = parent.layer.getFrameIndex(window.name);
  227. parent.layer.close(index);
  228. },
  229. showPic:function(url){ // 图片预览
  230. var img = new Image();
  231. img.src = url; // 预加载图片,计算弹出框的width、height
  232. if(img.width > config.window.width){
  233. img.width = config.window.width;
  234. }
  235. if(img.height > config.window.height){
  236. img.height = config.window.height;
  237. }
  238. img.onload = function(){
  239. var index = layer.open({
  240. type: 2,
  241. title: false, // 去掉标题
  242. shadeClose: true,
  243. fixed: true, //不固定
  244. skin: 'layui-layer-nobg', //没有背景色
  245. shade: 0.7,
  246. area: [img.width + 'px', img.height + 'px'],
  247. content: url //pic 路径
  248. });
  249. }
  250. img.onerror = function(){
  251. $.tips('图片不存在');
  252. }
  253. },
  254. openDlg:function(options){
  255. if('undefined' == typeof(options.width)){
  256. options.width = config.dlg.width;
  257. }
  258. if('undefined' == typeof(options.height)){
  259. options.height = config.dlg.height;
  260. }
  261. if('undefined' == typeof(options.title)){
  262. options.title = false;
  263. }
  264. if(isPicUrl(options.url)){
  265. $.showPic(options.url);
  266. return;
  267. }
  268. var index = layer.open({
  269. type: 2,
  270. area: [options.width, options.height],
  271. fixed: false, //不固定
  272. resize: true,
  273. maxmin: true,
  274. shade: 0.6,
  275. title:options.title,
  276. content: options.url,
  277. });
  278. // layer.iframeAuto(index);
  279. }
  280. });
  281. function queryParams(params){
  282. var page = params.offset / params.limit + 1;
  283. return { page:page, pageSize:params.limit, search:params.search, sort:params.sort,order:params.order};
  284. }
  285. function getSelectedRow(table_id){
  286. var data = $('#' + table_id).bootstrapTable('getSelections');
  287. if(typeof(data) == 'undefined' || data.length < 1){
  288. $.tips('未选中行');
  289. return null;
  290. }
  291. return data[0];
  292. }
  293. /**
  294. * 刷新table數據
  295. * @param table_id
  296. */
  297. function reflush(table_id){
  298. $('#' + table_id).bootstrapTable('refresh');
  299. }
  300. /**
  301. * 图片路径 - 暂定
  302. * @param value
  303. * @param row
  304. * @param index
  305. * @returns {String}
  306. */
  307. function defParsePic(value,row,index){
  308. return '<img src="'+ config.base_path + value + '" width="'+ config.thumbnail.width +'" height="'+ config.thumbnail.height +'" style="cursor:pointer" onclick="javascript:$.showPic(this.src)" />';
  309. }
  310. /**
  311. * 关闭当前窗口
  312. * @param value
  313. * @param row
  314. * @param index
  315. * @returns {String}
  316. */
  317. function closeCurrentDlg(){
  318. var index = parent.layer.getFrameIndex(window.name);
  319. parent.layer.close(index);
  320. }
  321. var NotifyMsg = function(type, data, url){
  322. }
  323. function format_time(value,row,index){
  324. if(value == null || value ==''){
  325. return '-- -- --';
  326. }
  327. var date = new Date();
  328. date.setTime(value);
  329. Y = date.getFullYear() + '-';
  330. M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  331. D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
  332. h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
  333. m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
  334. s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
  335. return Y+M+D+h+m+s;
  336. }
  337. function format_date(value,row,index){
  338. if(value == null || value ==''){
  339. return '-- -- --';
  340. }
  341. var date = new Date();
  342. date.setTime(value);
  343. Y = date.getFullYear() + '-';
  344. M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  345. D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
  346. return Y+M+D;
  347. }