util.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. function doajax(method, dataurl, data, sucessCallBack, errorCallBack) {
  2. var user_key = undefined;
  3. var user = $.zui.store.get("user")
  4. if (typeof(user) == "undefined" || user == null) {
  5. } else {
  6. user_key = "XinTong " + (user.token);
  7. }
  8. if (typeof(user_key) == "undefined" || user_key == null) {
  9. $.ajax({
  10. type: method,
  11. url: dataurl,
  12. contentType: "application/json",
  13. dataType: "json",
  14. async: true,
  15. data: JSON.stringify(data),
  16. success: sucessCallBack,
  17. error: function(error) {
  18. if (HandleError(error)) return;
  19. errorCallBack(error);
  20. }
  21. })
  22. } else {
  23. $.ajax({
  24. type: method,
  25. url: dataurl,
  26. contentType: "application/json",
  27. dataType: "json",
  28. beforeSend: function(xhr) {
  29. xhr.setRequestHeader("token", user_key);
  30. },
  31. headers: {
  32. 'token': user_key
  33. },
  34. async: true,
  35. data: JSON.stringify(data),
  36. success: sucessCallBack,
  37. error: function(error) {
  38. if (HandleError(error)) return;
  39. errorCallBack(error);
  40. }
  41. })
  42. }
  43. }
  44. function ajaxGet(dataurl, data, sucessCallBack, errorCallBack) {
  45. doajax("GET", dataurl, data, sucessCallBack, errorCallBack)
  46. }
  47. function ajaxPost(dataurl, data, sucessCallBack, errorCallBack) {
  48. doajax("POST", dataurl, data, sucessCallBack, errorCallBack)
  49. }
  50. function ajaxPut(dataurl, data, sucessCallBack, errorCallBack) {
  51. doajax("PUT", dataurl, data, sucessCallBack, errorCallBack)
  52. }
  53. function ajaxDelete(dataurl, data, sucessCallBack, errorCallBack) {
  54. doajax("DELETE", dataurl, data, sucessCallBack, errorCallBack)
  55. }
  56. function HandleError(error) {
  57. if (typeof(error) != "undefined" && error != null) {
  58. if (typeof(error.status) != "undefined" && error.status != null) {
  59. if (error.status == 403) {
  60. //未登录退出
  61. layer.msg('登陆已经过期,将重新登陆!', {
  62. time: 2000, //20s后自动关闭
  63. }, function() {
  64. self.location = base_ui_url + UI_USER_LOGIN
  65. })
  66. return true;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. function GetQueryString(name) {
  73. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  74. var r = window.location.search.substr(1).match(reg);
  75. if (r != null) {
  76. return unescape(r[2]);
  77. }
  78. return null;
  79. }
  80. function getItemByIdFromArr(id,curDatas){
  81. var rowData;
  82. for(var i=0;i<curDatas.length;i++){
  83. if(id==curDatas[i].id){
  84. rowData = curDatas[i];
  85. break;
  86. }
  87. }
  88. return rowData;
  89. }
  90. function showPopup4Common(title,callback,area,divId,offset){
  91. if(!area) area='auto';
  92. if(!divId) divId='#form-div';
  93. if(!offset) offset='auto';
  94. //添加常量页面
  95. return layer.open({
  96. type: 1,
  97. area: area,
  98. title: title,
  99. closeBtn: 1,
  100. shadeClose: true,
  101. offset: offset,
  102. skin: 'layui-layer-lan',
  103. content: $(divId),
  104. btn: '保存',
  105. btnAlign: 'c', //按钮居中
  106. shade: 0 ,//不显示遮罩
  107. yes: function(index){
  108. if(callback!=false){
  109. callback();
  110. }
  111. layer.close(index);
  112. }
  113. });
  114. }
  115. function deleteItem4Common(id,url,okCb,offset){
  116. if(!offset) offset='auto';
  117. //询问框
  118. layer.confirm('确定删除记录?', {
  119. btn: ['删除','取消'], //按钮
  120. offset:offset
  121. }, function(){
  122. var param ={"id":id};
  123. no_return_common_service(url, param, function(data) {
  124. okCb();//重新加载表格数据
  125. layer.msg(data, {
  126. time: 2000//20s后自动关闭
  127. });
  128. }, function(error) {
  129. alert(error);
  130. });
  131. }, function(index){
  132. layer.close(index);
  133. });
  134. }
  135. function addOrUpdateItem4Common(param,url,okCb){
  136. no_return_common_service(url, param, function(data) {
  137. if(okCb) okCb();//重新加载表格数据
  138. layer.msg(data, {
  139. time: 2000//20s后自动关闭
  140. });
  141. }, function(error) {
  142. alert(error);
  143. });
  144. }
  145. function setFeSelect(div,selectId){
  146. get_common_service("/dept/getAllFs",null,function(data){
  147. var optStr = '<option value="">全部</option>';
  148. for(var i in data) {
  149. if(data[i].deptid==selectId)
  150. optStr += '<option value="'+data[i].deptid+'" selected="selected">'+data[i].name+'</option>';
  151. else
  152. optStr += '<option value="'+data[i].deptid+'">'+data[i].name+'</option>';
  153. }
  154. $(div).html('');
  155. $(div).html(optStr);
  156. });
  157. }
  158. function setLaneSelect(div,selectId){
  159. var optStr = '';
  160. var data =[];
  161. for(var i =0;i<=8;i++) {
  162. if(i==0)
  163. data.push({id:i,name:'值机室'});
  164. else
  165. data.push({id:i,name:i+'车道'});
  166. }
  167. for(var i in data) {
  168. if(i==selectId)
  169. optStr += '<option value="'+data[i].id+'" selected="selected">'+data[i].name+'</option>';
  170. else
  171. optStr += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
  172. }
  173. $(div).html('');
  174. $(div).html(optStr);
  175. }
  176. // var ViewMap = new HashMap()
  177. // ViewMap.set("/view/mytask/unchecked.html", __inline('/view/mytask/unchecked.html'));
  178. // ViewMap.set("/view/mytask/unexamined.html", __inline('/view/mytask/unexamined.html'));
  179. // ViewMap.set("/view/mytask/undispatched.html", __inline('/view/mytask/undispatched.html'));
  180. // ViewMap.set("/view/mytask/dispatched.html", __inline('/view/mytask/dispatched.html'));
  181. // ViewMap.set("/view/constant/constant.html", __inline('/view/constant/constant.html'));
  182. /**
  183. * @Func 处理长整形时间格式化
  184. * @time long
  185. * @format string 有默认值
  186. */
  187. function timeStamp2String(time,format){
  188. if(time == "" || time == null)
  189. return ;
  190. if(format==undefined || format == "")
  191. format = "yyyy/MM/dd hh:mm:ss";
  192. var datetime = new Date();
  193. datetime.setTime(time);
  194. return datetime.Format(format);
  195. };
  196. /**
  197. * @Func 时间格式化
  198. * @FuncName Format
  199. * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  200. */
  201. Date.prototype.Format = function (fmt) {
  202. var o = {
  203. "M+": this.getMonth() + 1,
  204. "d+": this.getDate(),
  205. "H+": this.getHours(),
  206. "m+": this.getMinutes(),
  207. "s+": this.getSeconds(),
  208. "q+": Math.floor((this.getMonth() + 3) / 3),
  209. "S": this.getMilliseconds()
  210. };
  211. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  212. for (var k in o)
  213. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  214. return fmt;
  215. };
  216. /**
  217. * 提示框
  218. */
  219. function tip(data,timeout){
  220. if(data==undefined || data=='')
  221. return ;
  222. layer.msg(data, {
  223. time: (timeout!=null)?timeout:2000 //20s后自动关闭
  224. });
  225. }
  226. function showPopup4CommonBtns(title,area,divId,btns,callback1,callback2){
  227. if(!area) area='auto';
  228. if(!divId) divId='#form-div';
  229. //添加常量页面
  230. return layer.open({
  231. type: 1,
  232. area: area,
  233. title: title,
  234. closeBtn: 1,
  235. shadeClose: true,
  236. content: $(divId),
  237. btn: btns,
  238. btnAlign: 'c', //按钮居中
  239. shade: 0 ,//不显示遮罩
  240. yes: function(index){
  241. if(callback!=false){
  242. callback();
  243. }
  244. layer.close(index);
  245. }
  246. });
  247. }
  248. function getCurrentUser() {
  249. var user = $.zui.store.get("user")
  250. return user;
  251. }
  252. function hasRole(role){
  253. var user = getCurrentUser();
  254. var roles = user.roles;
  255. for(var i in roles){
  256. if(roles[i].name==role) return true;
  257. }
  258. return false;
  259. }
  260. function roleContains(role){
  261. var user = getCurrentUser();
  262. var roles = user.roles;
  263. for(var i in roles){
  264. if(roles[i].name.indexOf(role) >= 0) return true;
  265. }
  266. return false;
  267. }
  268. function setCheckmanSelect(div, selectId, notHaveUserId, callback){
  269. post_common_service("/user/getAllCheckman",null,function(data){
  270. var optStr = '';
  271. for(var i in data) {
  272. if(data[i].id==notHaveUserId) continue;
  273. if(data[i].id==selectId)
  274. optStr += '<option value="'+data[i].id+'" selected="selected">'+data[i].truename+'</option>';
  275. else
  276. optStr += '<option value="'+data[i].id+'">'+data[i].truename+'</option>';
  277. }
  278. $(div).html('');
  279. $(div).html(optStr);
  280. if(callback) callback();
  281. });
  282. }
  283. function genAppeaFiles(file_src){
  284. initStringfunc();
  285. var files = file_src.split(",");
  286. var optionStr="";
  287. for(var i in files) {
  288. var fileSrc = base_image_server_url+files[i];
  289. if(fileSrc.endWith("png") || fileSrc.endWith("jpg")||fileSrc.endWith("ico")){
  290. if(ISCLIENT) {
  291. optionStr+= '<a href="javascript:void(0)" onclick="showClientImg('+fileSrc+')"><img src="'+fileSrc+'" style="height:55px;margin:5px;"></a>';
  292. }else{
  293. optionStr+= '<a href="'+fileSrc+'" target="_blank"><img src="'+fileSrc+'" style="height:55px;margin:5px;"></a>';
  294. }
  295. }else if(fileSrc.endWith("doc")|| fileSrc.endWith("docx")){
  296. if(ISCLIENT) {
  297. optionStr+= '<a href="javascript:void(0)" onclick="downloadClientfile('+fileSrc+')"><i class="icon icon-file-word icon-4x" style="vertical-align: middle"></a>';
  298. }else{
  299. optionStr+= '<a href="'+fileSrc+'" target="_blank"><i class="icon icon-file-word icon-4x" style="vertical-align: middle"></a>';
  300. }
  301. }else if(fileSrc.endWith("txt")){
  302. if(ISCLIENT) {
  303. optionStr+= '<a href="javascript:void(0)" onclick="downloadClientfile('+fileSrc+')"><i class="icon icon-file-code icon-4x" style="vertical-align: middle"></a>';
  304. }else{
  305. optionStr+= '<a href="'+fileSrc+'" target="_blank"><i class="icon icon-file-code icon-4x" style="vertical-align: middle"></a>';
  306. }
  307. }
  308. }
  309. return optionStr;
  310. }
  311. initStringfunc();
  312. function initStringfunc(){
  313. String.prototype.endWith = function(str){
  314. if(str==null || str=="" || this.length == 0 ||str.length > this.length){
  315. return false;
  316. }
  317. if(this.substring(this.length - str.length) == str){
  318. return true;
  319. }else{
  320. return false;
  321. }
  322. };
  323. String.prototype.startWith = function(str){
  324. if(str == null || str== "" || this.length== 0 || str.length > this.length){
  325. return false;
  326. }
  327. if(this.substr(0,str.length) == str){
  328. return true;
  329. }else{
  330. return false;
  331. }
  332. };
  333. }
  334. function showClientImg(src){
  335. callFunc("showImage", src);
  336. }
  337. function downloadClientfile(src){
  338. callFunc("downloadfile", src);
  339. }
  340. function setTeamUserSelect(div,selectId,classId){
  341. post_common_service("/team/getTeamPersonsByUserClassId/"+classId,null,function(data){
  342. var optStr = '';
  343. for(var i in data) {
  344. if(data[i].positionid==4) data[i].truename=data[i].truename+'(班长)';
  345. if(data[i].id==selectId)
  346. optStr += '<option value="'+data[i].id+'" selected="selected">'+data[i].truename+'</option>';
  347. else
  348. optStr += '<option value="'+data[i].id+'">'+data[i].truename+'</option>';
  349. }
  350. $(div).html('');
  351. $(div).html(optStr);
  352. });
  353. }