common.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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: true,
  50. pagination: true,
  51. smartDisplay: false,
  52. pageSize: 10,
  53. pageList: [10, 20, 30, 40, 50],
  54. paginationLoop: false,
  55. sidePagination: 'server',
  56. queryParamsType : null,
  57. columns: []
  58. }
  59. var option = $.extend({}, defaults, opt);
  60. if(!option.pagination){
  61. option.responseHandler = function(res) {
  62. return res.rows;
  63. }
  64. }
  65. $(this).bootstrapTable(option);
  66. }
  67. formatDate = function (v, format) {
  68. if (!v) return "";
  69. var d = v;
  70. if (typeof v === 'string') {
  71. if (v.indexOf("/Date(") > -1)
  72. d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
  73. else
  74. d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));//.split(".")[0] 用来处理出现毫秒的情况,截取掉.xxx,否则会出错
  75. }
  76. var o = {
  77. "M+": d.getMonth() + 1,
  78. "d+": d.getDate(),
  79. "h+": d.getHours(),
  80. "m+": d.getMinutes(),
  81. "s+": d.getSeconds(),
  82. "q+": Math.floor((d.getMonth() + 3) / 3),
  83. "S": d.getMilliseconds()
  84. };
  85. if (/(y+)/.test(format)) {
  86. format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
  87. }
  88. for (var k in o) {
  89. if (new RegExp("(" + k + ")").test(format)) {
  90. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  91. }
  92. }
  93. return format;
  94. }
  95. function today() {
  96. var dd = new Date();
  97. return formatDate(dd, 'yyyy-MM-dd');
  98. }
  99. function countDay(dayCount) {
  100. var dd = new Date();
  101. dd.setDate(dd.getDate()+dayCount);//获取AddDayCount天后的日期
  102. var y = dd.getFullYear();
  103. var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0
  104. var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0
  105. return y+"-"+m+"-"+d;
  106. }
  107. isNullOrEmpty = function (obj) {
  108. if ((typeof (obj) == "string" && obj == "") || obj == null || obj == undefined) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. isNotNullOrEmpty = function (obj) {
  115. if ((typeof (obj) == "string" && obj == "") || obj == null || obj == undefined) {
  116. return false;
  117. } else {
  118. return true;
  119. }
  120. }
  121. checkedArray = function (id) {
  122. var isOK = true;
  123. if (id == undefined || id == "" || id == 'null' || id == 'undefined') {
  124. isOK = false;
  125. dialogMsg('您没有选中任何数据项!');
  126. }
  127. return isOK;
  128. }
  129. checkedRow = function (id) {
  130. var isOK = true;
  131. if (id == undefined || id == "" || id == 'null' || id == 'undefined') {
  132. isOK = false;
  133. dialogMsg('您没有选中任何数据项!');
  134. } else if (id.length > 1) {
  135. isOK = false;
  136. dialogMsg('您只能选择一条数据项!');
  137. }
  138. return isOK;
  139. }
  140. reload = function () {
  141. location.reload();
  142. return false;
  143. }
  144. dialogOpen = function(opt){
  145. var defaults = {
  146. id : 'layerForm',
  147. title : '',
  148. width: '',
  149. height: '',
  150. url : null,
  151. scroll : false,
  152. data : {},
  153. btn: ['确定', '取消'],
  154. success: function(){},
  155. yes: function(){}
  156. }
  157. var option = $.extend({}, defaults, opt), content = null;
  158. if(option.scroll){
  159. content = [option.url]
  160. }else{
  161. content = [option.url, 'no']
  162. }
  163. top.layer.open({
  164. type : 2,
  165. id : option.id,
  166. title : option.title,
  167. closeBtn : 1,
  168. anim: -1,
  169. isOutAnim: false,
  170. shadeClose : false,
  171. shade : 0.3,
  172. area : [option.width, option.height],
  173. content : content,
  174. btn: option.btn,
  175. success: function(){
  176. option.success(option.id);
  177. },
  178. yes: function(){
  179. option.yes(option.id);
  180. }
  181. });
  182. }
  183. dialogContent = function(opt){
  184. var defaults = {
  185. title : '系统窗口',
  186. width: '',
  187. height: '',
  188. content : null,
  189. data : {},
  190. btn: ['确定', '取消'],
  191. success: null,
  192. yes: null
  193. }
  194. var option = $.extend({}, defaults, opt);
  195. return top.layer.open({
  196. type : 1,
  197. title : option.title,
  198. closeBtn : 1,
  199. anim: -1,
  200. isOutAnim: false,
  201. shadeClose : false,
  202. shade : 0.3,
  203. area : [option.width, option.height],
  204. shift : 5,
  205. content : option.content,
  206. btn: option.btn,
  207. success: option.success,
  208. yes: option.yes
  209. });
  210. }
  211. dialogAjax = function(opt){
  212. var defaults = {
  213. title : '系统窗口',
  214. width: '',
  215. height: '',
  216. url : null,
  217. data : {},
  218. btn: ['确定', '取消'],
  219. success: null,
  220. yes: null
  221. }
  222. var option = $.extend({}, defaults, opt);
  223. $.post(option.url, null, function(content){
  224. layer.open({
  225. type : 1,
  226. title : option.title,
  227. closeBtn : 1,
  228. anim: -1,
  229. isOutAnim: false,
  230. shadeClose : false,
  231. shade : 0.3,
  232. area : [option.width, option.height],
  233. shift : 5,
  234. content : content,
  235. btn: option.btn,
  236. success: option.success,
  237. yes: option.yes
  238. });
  239. });
  240. }
  241. dialogAlert = function (content, type) {
  242. var msgType = {
  243. success:1,
  244. error:2,
  245. warn:3,
  246. info:7
  247. };
  248. if(isNullOrEmpty(type)){
  249. type='info';
  250. }
  251. top.layer.alert(content, {
  252. icon: msgType[type],
  253. title: "系统提示",
  254. anim: -1,
  255. btnAlign: 'c',
  256. isOutAnim: false
  257. });
  258. }
  259. dialogConfirm = function (content, callBack) {
  260. top.layer.confirm(content, {
  261. area: '338px',
  262. icon: 7,
  263. anim: -1,
  264. isOutAnim: false,
  265. title: "系统提示",
  266. btn: ['确认', '取消'],
  267. btnAlign: 'c',
  268. yes: callBack
  269. });
  270. }
  271. dialogMsg = function(msg, type) {
  272. var msgType = {
  273. success:1,
  274. error:2,
  275. warn:3,
  276. info:7
  277. };
  278. if(isNullOrEmpty(type)){
  279. type='info';
  280. }
  281. top.layer.msg(msg, {
  282. icon: msgType[type],
  283. time: 2000
  284. });
  285. }
  286. dialogClose = function() {
  287. var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  288. top.layer.close(index); //再执行关闭
  289. }
  290. dialogLoading = function(flag) {
  291. if(flag){
  292. top.layer.load(0, {
  293. shade: [0.1,'#fff'],
  294. time: 2000
  295. });
  296. }else{
  297. top.layer.closeAll('loading');
  298. }
  299. }
  300. dialogToastr = function(msg) {
  301. top.layer.msg(msg);
  302. }
  303. dialogTip = function(msg, dom) {
  304. top.layer.tips(msg, dom);
  305. }
  306. $.fn.GetWebControls = function (keyValue) {
  307. var reVal = "";
  308. $(this).find('input,select,textarea').each(function (r) {
  309. var id = $(this).attr('id');
  310. var type = $(this).attr('type');
  311. switch (type) {
  312. case "checkbox":
  313. if ($("#" + id).is(":checked")) {
  314. reVal += '"' + id + '"' + ':' + '"1",'
  315. } else {
  316. reVal += '"' + id + '"' + ':' + '"0",'
  317. }
  318. break;
  319. default:
  320. var value = $("#" + id).val();
  321. if (value == "") {
  322. value = "&nbsp;";
  323. }
  324. reVal += '"' + id + '"' + ':' + '"' + $.trim(value) + '",'
  325. break;
  326. }
  327. });
  328. reVal = reVal.substr(0, reVal.length - 1);
  329. if (!keyValue) {
  330. reVal = reVal.replace(/&nbsp;/g, '');
  331. }
  332. reVal = reVal.replace(/\\/g, '\\\\');
  333. reVal = reVal.replace(/\n/g, '\\n');
  334. var postdata = jQuery.parseJSON('{' + reVal + '}');
  335. return postdata;
  336. };
  337. $.fn.SetWebControls = function (data) {
  338. var $id = $(this)
  339. for (var key in data) {
  340. var id = $id.find('#' + key);
  341. if (id.attr('id')) {
  342. var type = id.attr('type');
  343. var value = $.trim(data[key]).replace(/&nbsp;/g, '');
  344. switch (type) {
  345. case "checkbox":
  346. if (value == 1) {
  347. id.attr("checked", 'checked');
  348. } else {
  349. id.removeAttr("checked");
  350. }
  351. break;
  352. default:
  353. id.val(value);
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. tabiframeId = function () {
  360. var iframeId = top.$(".DP_iframe:visible").attr("id");
  361. return iframeId;
  362. }
  363. $.currentIframe = function () {
  364. var tabId = tabiframeId();
  365. if(isNullOrEmpty(tabId)) {//单页iframe嵌套
  366. return $(window.parent.document).contents().find('#main')[0].contentWindow;
  367. }
  368. return $(window.parent.document).contents().find('#'+tabiframeId())[0].contentWindow;//多层tab页嵌套
  369. }