common.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // 获取请求参数
  2. // 使用示例
  3. // location.href = http://localhost:8080/index.html?id=123
  4. // T.p('id') --> 123;
  5. var url = function(name) {
  6. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  7. var r = window.location.search.substr(1).match(reg);
  8. if(r!=null)return unescape(r[2]); return null;
  9. }
  10. //登录token
  11. var token = localStorage.getItem("token");
  12. if(token == 'null'){
  13. parent.location.href = 'login.html';
  14. }
  15. //全局配置
  16. $.ajaxSetup({
  17. dataType: "json",
  18. cache: false,
  19. headers: {
  20. "token": token
  21. },
  22. complete: function(xhr) {
  23. if(xhr.responseJSON.code == 401){
  24. toUrl('login.html');
  25. }
  26. }
  27. })
  28. //权限判断
  29. function hasPermission(permission) {
  30. if(isNullOrEmpty(window.parent.perms)) {
  31. return false;
  32. }
  33. if (window.parent.perms.indexOf(permission) > -1) {
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39. toUrl = function(href) {
  40. window.location.href = href;
  41. }
  42. $.fn.bootstrapTableEx = function(opt){
  43. var defaults = {
  44. url: '',
  45. dataField: "rows",
  46. method: 'post',
  47. dataType: 'json',
  48. selectItemName: 'id',
  49. clickToSelect: false,
  50. pagination: true,
  51. smartDisplay: false,
  52. pageSize: 10,
  53. pageList: [10, 20, 30, 40, 50],
  54. paginationLoop: false,
  55. paginationShowPageGo: true,
  56. sidePagination: 'server',
  57. queryParamsType : null,
  58. columns: []
  59. }
  60. var option = $.extend({}, defaults, opt);
  61. if(!option.pagination){
  62. option.responseHandler = function(res) {
  63. return res.rows;
  64. }
  65. }
  66. $(this).bootstrapTable(option);
  67. }
  68. $.fn.bootstrapTreeTableEx = function(opt) {
  69. var $table = $(this);
  70. var defaults = {
  71. url: '',
  72. striped: true,
  73. sidePagination: 'server',
  74. clickToSelect: true,
  75. idField: '',
  76. columns: [],
  77. treeShowField: '',
  78. parentIdField: '',
  79. onLoadSuccess: function(data) {
  80. $table.treegrid({
  81. treeColumn: 1,
  82. onChange: function() {
  83. $table.bootstrapTable('resetWidth');
  84. }
  85. });
  86. }
  87. }
  88. var option = $.extend({}, defaults, opt);
  89. $(this).bootstrapTable(option);
  90. }
  91. formatDate = function (v, format) {
  92. if (!v) return "";
  93. var d = v;
  94. if (typeof v === 'string') {
  95. if (v.indexOf("/Date(") > -1)
  96. d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
  97. else
  98. d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));//.split(".")[0] 用来处理出现毫秒的情况,截取掉.xxx,否则会出错
  99. }
  100. var o = {
  101. "M+": d.getMonth() + 1,
  102. "d+": d.getDate(),
  103. "h+": d.getHours(),
  104. "m+": d.getMinutes(),
  105. "s+": d.getSeconds(),
  106. "q+": Math.floor((d.getMonth() + 3) / 3),
  107. "S": d.getMilliseconds()
  108. };
  109. if (/(y+)/.test(format)) {
  110. format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
  111. }
  112. for (var k in o) {
  113. if (new RegExp("(" + k + ")").test(format)) {
  114. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  115. }
  116. }
  117. return format;
  118. }
  119. function today() {
  120. var dd = new Date();
  121. return formatDate(dd, 'yyyy-MM-dd');
  122. }
  123. function countDay(dayCount) {
  124. var dd = new Date();
  125. if (dayCount < 0) {
  126. dd.setDate(dd.getDate()+dayCount+1);//获取AddDayCount天前的日期
  127. } else {
  128. dd.setDate(dd.getDate()+dayCount-1);//获取AddDayCount天后的日期
  129. }
  130. var y = dd.getFullYear();
  131. var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0
  132. var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0
  133. return y+"-"+m+"-"+d;
  134. }
  135. isNullOrEmpty = function (obj) {
  136. if ((typeof (obj) == "string" && obj == "") || obj == null || obj == undefined) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. isNotNullOrEmpty = function (obj) {
  143. if ((typeof (obj) == "string" && obj == "") || obj == null || obj == undefined) {
  144. return false;
  145. } else {
  146. return true;
  147. }
  148. }
  149. checkedArray = function (id) {
  150. var isOK = true;
  151. if (id == undefined || id == "" || id == 'null' || id == 'undefined') {
  152. isOK = false;
  153. dialogMsg('您没有选中任何数据项!');
  154. }
  155. return isOK;
  156. }
  157. checkedRow = function (id) {
  158. var isOK = true;
  159. if (id == undefined || id == "" || id == 'null' || id == 'undefined') {
  160. isOK = false;
  161. dialogMsg('您没有选中任何数据项!');
  162. } else if (id.length > 1) {
  163. isOK = false;
  164. dialogMsg('您只能选择一条数据项!');
  165. }
  166. return isOK;
  167. }
  168. reload = function () {
  169. location.reload();
  170. return false;
  171. }
  172. dialogOpen = function(opt){
  173. var defaults = {
  174. id : 'layerForm',
  175. title : '',
  176. width: '',
  177. height: '',
  178. url : null,
  179. scroll : false,
  180. data : {},
  181. btn: ['确定', '取消'],
  182. success: function(){},
  183. yes: function(){}
  184. }
  185. var option = $.extend({}, defaults, opt), content = null;
  186. if(option.scroll){
  187. content = [option.url]
  188. }else{
  189. content = [option.url, 'no']
  190. }
  191. top.layer.open({
  192. type : 2,
  193. id : option.id,
  194. title : option.title,
  195. closeBtn : 1,
  196. anim: -1,
  197. isOutAnim: false,
  198. shadeClose : false,
  199. shade : 0.3,
  200. area : [option.width, option.height],
  201. content : content,
  202. btn: option.btn,
  203. success: function(){
  204. option.success(option.id);
  205. },
  206. yes: function(){
  207. option.yes(option.id);
  208. }
  209. });
  210. }
  211. dialogContent = function(opt){
  212. var defaults = {
  213. title : '系统窗口',
  214. width: '',
  215. height: '',
  216. content : null,
  217. data : {},
  218. btn: ['确定', '取消'],
  219. success: null,
  220. yes: null
  221. }
  222. var option = $.extend({}, defaults, opt);
  223. return top.layer.open({
  224. type : 1,
  225. title : option.title,
  226. closeBtn : 1,
  227. anim: -1,
  228. isOutAnim: false,
  229. shadeClose : false,
  230. shade : 0.3,
  231. area : [option.width, option.height],
  232. shift : 5,
  233. content : option.content,
  234. btn: option.btn,
  235. success: option.success,
  236. yes: option.yes
  237. });
  238. }
  239. dialogAjax = function(opt){
  240. var defaults = {
  241. title : '系统窗口',
  242. width: '',
  243. height: '',
  244. url : null,
  245. data : {},
  246. btn: ['确定', '取消'],
  247. success: null,
  248. yes: null
  249. }
  250. var option = $.extend({}, defaults, opt);
  251. $.post(option.url, null, function(content){
  252. layer.open({
  253. type : 1,
  254. title : option.title,
  255. closeBtn : 1,
  256. anim: -1,
  257. isOutAnim: false,
  258. shadeClose : false,
  259. shade : 0.3,
  260. area : [option.width, option.height],
  261. shift : 5,
  262. content : content,
  263. btn: option.btn,
  264. success: option.success,
  265. yes: option.yes
  266. });
  267. });
  268. }
  269. dialogAlert = function (content, type) {
  270. var msgType = {
  271. success:1,
  272. error:2,
  273. warn:3,
  274. info:7
  275. };
  276. if(isNullOrEmpty(type)){
  277. type='info';
  278. }
  279. top.layer.alert(content, {
  280. icon: msgType[type],
  281. title: "系统提示",
  282. anim: -1,
  283. btnAlign: 'c',
  284. isOutAnim: false
  285. });
  286. }
  287. dialogConfirm = function (content, callBack) {
  288. top.layer.confirm(content, {
  289. area: '338px',
  290. icon: 7,
  291. anim: -1,
  292. isOutAnim: false,
  293. title: "系统提示",
  294. btn: ['确认', '取消'],
  295. btnAlign: 'c',
  296. yes: callBack
  297. });
  298. }
  299. dialogMsg = function(msg, type) {
  300. var msgType = {
  301. success:1,
  302. error:2,
  303. warn:3,
  304. info:7
  305. };
  306. if(isNullOrEmpty(type)){
  307. type='info';
  308. }
  309. top.layer.msg(msg, {
  310. icon: msgType[type],
  311. time: 2000
  312. });
  313. }
  314. dialogClose = function() {
  315. var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  316. top.layer.close(index); //再执行关闭
  317. }
  318. dialogLoading = function(flag) {
  319. if(flag){
  320. top.layer.load(0, {
  321. shade: [0.1,'#fff'],
  322. time: 2000
  323. });
  324. }else{
  325. top.layer.closeAll('loading');
  326. }
  327. }
  328. dialogToastr = function(msg) {
  329. top.layer.msg(msg);
  330. }
  331. dialogTip = function(msg, dom) {
  332. top.layer.tips(msg, dom);
  333. }
  334. $.fn.GetWebControls = function (keyValue) {
  335. var reVal = "";
  336. $(this).find('input,select,textarea').each(function (r) {
  337. var id = $(this).attr('id');
  338. var type = $(this).attr('type');
  339. switch (type) {
  340. case "checkbox":
  341. if ($("#" + id).is(":checked")) {
  342. reVal += '"' + id + '"' + ':' + '"1",'
  343. } else {
  344. reVal += '"' + id + '"' + ':' + '"0",'
  345. }
  346. break;
  347. default:
  348. var value = $("#" + id).val();
  349. if (value == "") {
  350. value = "&nbsp;";
  351. }
  352. reVal += '"' + id + '"' + ':' + '"' + $.trim(value) + '",'
  353. break;
  354. }
  355. });
  356. reVal = reVal.substr(0, reVal.length - 1);
  357. if (!keyValue) {
  358. reVal = reVal.replace(/&nbsp;/g, '');
  359. }
  360. reVal = reVal.replace(/\\/g, '\\\\');
  361. reVal = reVal.replace(/\n/g, '\\n');
  362. var postdata = jQuery.parseJSON('{' + reVal + '}');
  363. return postdata;
  364. };
  365. $.fn.SetWebControls = function (data) {
  366. var $id = $(this)
  367. for (var key in data) {
  368. var id = $id.find('#' + key);
  369. if (id.attr('id')) {
  370. var type = id.attr('type');
  371. var value = $.trim(data[key]).replace(/&nbsp;/g, '');
  372. switch (type) {
  373. case "checkbox":
  374. if (value == 1) {
  375. id.attr("checked", 'checked');
  376. } else {
  377. id.removeAttr("checked");
  378. }
  379. break;
  380. default:
  381. id.val(value);
  382. break;
  383. }
  384. }
  385. }
  386. }
  387. tabiframeId = function () {
  388. var iframeId = top.$(".DP_iframe:visible").attr("id");
  389. return iframeId;
  390. }
  391. $.currentIframe = function () {
  392. var tabId = tabiframeId();
  393. if(isNullOrEmpty(tabId)) {//单页iframe嵌套
  394. return $(window.parent.document).contents().find('#main')[0].contentWindow;
  395. }
  396. return $(window.parent.document).contents().find('#'+tabiframeId())[0].contentWindow;//多层tab页嵌套
  397. }
  398. /**
  399. * 根据ajax地址初始化select2选择器
  400. * @param opt
  401. * @returns {*}
  402. */
  403. $.fn.selectBindEx = function(opt) {
  404. var $select = $(this);
  405. var defaults = {
  406. url: '',
  407. async: true,
  408. text: 'name',
  409. value: 'id',
  410. placeholder: '请选择...',
  411. selected: '',
  412. allowClear: false,
  413. theme: "bootstrap",
  414. language: "zh-CN",
  415. change: function(){}
  416. }
  417. if (opt.search === false) {
  418. opt.minimumResultsForSearch = 'Infinity';
  419. }
  420. var option = $.extend({}, defaults, opt);
  421. var selectControl = null;
  422. $.ajax({
  423. type: 'get',
  424. async: option.async,
  425. contentType : 'application/json',
  426. url: option.url,
  427. data: null,
  428. success: function(r) {
  429. selectControl = $select.select2(option);
  430. $.each(r, function(idx, item){
  431. selectControl.append("<option value='"+item[option.value]+"'>"+item[option.text]+"</option>");
  432. })
  433. $select.val(option.selected);
  434. $select.on('change', function() {
  435. option.change($select.val());
  436. });
  437. },
  438. dataType: 'json'
  439. });
  440. return selectControl;
  441. }
  442. /**
  443. * 初始化select2选择器
  444. * @param placeholder
  445. * @returns {*|void}
  446. */
  447. $.fn.selectInitEx = function(placeholder, search) {
  448. var opt = {
  449. placeholder: placeholder,
  450. theme: "bootstrap",
  451. language: "zh-CN"
  452. }
  453. if (search === false) {
  454. opt.minimumResultsForSearch = 'Infinity';
  455. }
  456. return $(this).select2(opt);
  457. }
  458. /**
  459. * 富文本编辑器工具类
  460. * @type {{init: editor.init}}
  461. */
  462. editorUtils = {
  463. init: function(opt) {
  464. var defaults = {
  465. element: '#editor',
  466. change: function(){}
  467. };
  468. var option = $.extend({}, defaults, opt);
  469. var editor = new window.wangEditor(option.element);
  470. editor.customConfig.uploadImgServer = '/editor/upload';
  471. editor.customConfig.uploadImgHeaders = {
  472. 'token': token
  473. }
  474. editor.customConfig.onchange= function(html) {
  475. option.change(html);
  476. };
  477. editor.customConfig.customAlert = function(info) {
  478. dialogAlert(info, 'error');
  479. };
  480. editor.create();
  481. return editor;
  482. },
  483. set: function($editor, content) {
  484. $editor.txt.html(content);
  485. },
  486. get: function($editor) {
  487. return $editor.txt.html();
  488. },
  489. text: function($editor) {
  490. return $editor.txt.text();
  491. },
  492. append: function($editor, content) {
  493. $editor.txt.append(content)
  494. },
  495. clear: function($editor) {
  496. $editor.txt.clear()
  497. },
  498. hasContents: function ($editor) {
  499. var content = this.get($editor);
  500. return isNotNullOrEmpty(this.get($editor)) && "<p><br></p>" !== content;
  501. }
  502. }
  503. /**
  504. * switchery开关组件
  505. * @type {{}}
  506. * 选择器selector用于获取选择状态,开关instance用于设置状态,禁用,启用
  507. */
  508. switchUtils = {
  509. init: function(opt) {
  510. var defaults = {
  511. selector: '#editor',
  512. size: 'small',
  513. single: true,
  514. change: function(){}
  515. };
  516. var option = $.extend({}, defaults, opt);
  517. var switchContainer = [], switchResults = [];
  518. if (option.single) {
  519. switchContainer.push(document.querySelector(option.selector));
  520. } else {
  521. switchContainer = document.querySelectorAll(option.selector);
  522. }
  523. $.each(switchContainer, function(idx, item) {
  524. var $switchery = new Switchery(item, option);
  525. var $item = $(item);
  526. var result = {selector: item, instance: $switchery};
  527. $item.on('change', function(event) {
  528. option.change(result);
  529. });
  530. switchResults.push(result);
  531. });
  532. if (option.single) {
  533. return switchResults[0];
  534. }
  535. return switchResults;
  536. },
  537. set: function($switch, checked) {
  538. $switch = $switch.instance;
  539. if ((checked && !$switch.isChecked()) || (!checked && $switch.isChecked())) {
  540. $switch.setPosition(true);
  541. $switch.handleOnchange(true);
  542. }
  543. },
  544. on: function($switch) {
  545. this.set($switch, true);
  546. },
  547. off: function($switch) {
  548. this.set($switch, false);
  549. },
  550. disable: function($switch) {
  551. $switch.instance.disable();
  552. },
  553. enable: function($switch) {
  554. $switch.instance.enable();
  555. },
  556. checked: function($switch) {
  557. return $switch.selector.checked;
  558. },
  559. data: function($switch, key) {
  560. return $($switch.selector).data(key);
  561. }
  562. }