农燊高科官方网站
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

tools.js 11 KiB

hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. define(['jquery', 'dialog'], function ($, dialog) {
  2. // 工具类
  3. function Tool() {
  4. this.version = "1.0.0";
  5. this.description = "这是一个工具类";
  6. }
  7. var ajaxJsUrl = "/api";
  8. var module = {};
  9. $.extend(Tool.prototype, {
  10. /**
  11. * ajax post
  12. * @param url (String)
  13. * @param data (Json) 需要提交的数据
  14. * @param cb(Function) 回调函数
  15. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  16. */
  17. doPost: function (url, data, cb, Bearer) {
  18. var _this = this;
  19. data.deptId = 100 ;
  20. console.log(JSON.stringify(data))
  21. var headAttribute = '';
  22. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  23. headAttribute = function (xhr) {
  24. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  25. }
  26. } else {
  27. headAttribute = function (xhr) {
  28. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  29. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  30. }
  31. }
  32. $.ajax({
  33. url: ajaxJsUrl + url + '?=' + Math.random(),
  34. type: 'POST',
  35. data: JSON.stringify(data),
  36. dataType: 'json',
  37. contentType: "application/json",
  38. beforeSend: headAttribute,
  39. success: function (data) {
  40. var code = data.code;
  41. var msg = data.msg;
  42. if (code === 401) {
  43. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  44. _this.skip('/view/login/login.html')
  45. }, '重新登录', function () { }, "取消")
  46. } else if (code === 500) {
  47. _this.initError(msg)
  48. cb(data);
  49. } else if (code != 200) {
  50. _this.initError(msg)
  51. } else {
  52. cb(data);
  53. }
  54. }
  55. });
  56. },
  57. /**
  58. * ajax put
  59. * @param url (String)
  60. * @param data (Json) 需要提交的数据
  61. * @param cb(Function) 回调函数
  62. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  63. */
  64. doPut: function (url, data, cb, Bearer) {
  65. var _this = this;
  66. var headAttribute = '';
  67. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  68. headAttribute = function (xhr) {
  69. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  70. }
  71. } else {
  72. headAttribute = function (xhr) {
  73. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  74. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  75. }
  76. }
  77. $.ajax({
  78. url: ajaxJsUrl + url + '?=' + Math.random(),
  79. type: 'put',
  80. data: JSON.stringify(data),
  81. dataType: 'json',
  82. contentType: "application/json",
  83. beforeSend: headAttribute,
  84. success: function (data) {
  85. var code = data.code;
  86. var msg = data.msg;
  87. if (code === 401) {
  88. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  89. _this.skip('/view/login/login.html')
  90. }, '重新登录', function () { }, "取消")
  91. } else if (code === 500) {
  92. _this.initError(msg)
  93. cb(data);
  94. } else if (code != 200) {
  95. _this.initError(msg)
  96. } else {
  97. cb(data);
  98. }
  99. }
  100. });
  101. },
  102. /**
  103. * ajax get
  104. * @param url(String)
  105. * @param data(Json) 需要提交的数据
  106. * @param cb(Function) 回调函数
  107. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  108. */
  109. doGet: function (uri, data, cb, Bearer) {
  110. var _this = this;
  111. var url = ajaxJsUrl + uri + '?deptId=100&';
  112. console.log(url);
  113. var headAttribute = ''
  114. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  115. headAttribute = function (xhr) {
  116. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  117. }
  118. } else {
  119. headAttribute = function (xhr) {
  120. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  121. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  122. }
  123. }
  124. if (data) {
  125. url += $.map(data, function (n, i) {
  126. if (i == data.length - 1) {
  127. return i + '=' + n;
  128. } else {
  129. return i + '=' + n + '&';
  130. }
  131. }).join('');
  132. }
  133. $.ajax({
  134. url: url,
  135. type: 'GET',
  136. beforeSend: headAttribute,
  137. success: function (data) {
  138. var code = data.code;
  139. var msg = data.msg;
  140. if (code === 401) {
  141. _this.removeCookie('Admin-Token');
  142. _this.removeCookie('userName');
  143. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  144. _this.skip('/view/login/login.html')
  145. }, '重新登录', function () { }, "取消")
  146. } else if (code === 500) {
  147. _this.initError(msg)
  148. } else if (code != 200) {
  149. _this.initError(msg)
  150. } else {
  151. cb(data);
  152. }
  153. },
  154. error: function (data) {
  155. console.log(data)
  156. }
  157. });
  158. },
  159. /**
  160. * ajax delete
  161. * @param url(String)
  162. * @param data(Json) 需要提交的数据
  163. * @param cb(Function) 回调函数
  164. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  165. */
  166. doDelete: function (uri, data, cb, Bearer) {
  167. var _this = this;
  168. var url = ajaxJsUrl + uri + '?';
  169. var headAttribute = ''
  170. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  171. headAttribute = function (xhr) {
  172. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  173. }
  174. } else {
  175. headAttribute = function (xhr) {
  176. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  177. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  178. }
  179. }
  180. if (data) {
  181. url += $.map(data, function (n, i) {
  182. if (i == data.length - 1) {
  183. return i + '=' + n;
  184. } else {
  185. return i + '=' + n + '&';
  186. }
  187. }).join('');
  188. }
  189. $.ajax({
  190. url: url,
  191. type: 'delete',
  192. beforeSend: headAttribute,
  193. success: function (data) {
  194. var code = data.code;
  195. var msg = data.msg;
  196. if (code === 401) {
  197. _this.removeCookie('Admin-Token');
  198. _this.removeCookie('userName');
  199. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  200. _this.skip('/view/login/login.html')
  201. }, '重新登录', function () { }, "取消")
  202. } else if (code === 500) {
  203. _this.initError(msg)
  204. } else if (code != 200) {
  205. _this.initError(msg)
  206. } else {
  207. cb(data);
  208. }
  209. },
  210. error: function (data) {
  211. console.log(data)
  212. }
  213. });
  214. },
  215. /**
  216. * 创建dialog
  217. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  218. * @param title (String) dialog的title
  219. * @param content(String || HTMLElement) dialog的内容
  220. * @param ok (Function) 点击确定按钮的回调函数
  221. * @param cancel(Function) 点击取消按钮的回调函数
  222. * 可以通过showModal来实现modal显示
  223. */
  224. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  225. var initDialog = dialog({
  226. id: id ? id : '',
  227. title: title,
  228. content: content,
  229. okValue: okValue ? okValue : '确定',
  230. ok: ok,
  231. cancelValue: cancelValue ? cancelValue : '取消',
  232. cancel: cancel,
  233. padding: 40,
  234. top: '150px'
  235. });
  236. initDialog.show();
  237. },
  238. /**
  239. * 创建error dialog
  240. * @param errContent
  241. */
  242. initError: function (errContent) {
  243. var error = dialog({
  244. content: errContent,
  245. padding: 20
  246. });
  247. error.show();
  248. var time = setTimeout(function () {
  249. error.remove();
  250. }, 1500);
  251. },
  252. /**
  253. * 创建dialog tips
  254. * @param tips
  255. * @param align
  256. */
  257. initTips: function (tips, align, $target, times) {
  258. var tips = dialog({
  259. content: tips,
  260. padding: 20,
  261. skin: 'min-dialog tips',
  262. quickClose: true,
  263. align: align
  264. });
  265. tips.show($target);
  266. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  267. times = 2000
  268. } else {
  269. times = times
  270. }
  271. setTimeout(function () {
  272. tips.close().remove();
  273. }, times);
  274. },
  275. /**
  276. * setCookie 设置cookie
  277. * @param cname 名称
  278. * @param cvalue 值
  279. * @param seconds 有效期
  280. */
  281. setCookie: function (cname, cvalue, seconds) {
  282. var d = new Date();
  283. d.setTime(d.getTime() + (seconds * 1000));
  284. var expires = "expires=" + d.toGMTString();
  285. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  286. },
  287. /**
  288. * getCookie 获取cookie
  289. * @param cname 名称
  290. */
  291. getCookie: function (cname) {
  292. var name = cname + "=";
  293. var ca = document.cookie.split(';');
  294. for (var i = 0; i < ca.length; i++) {
  295. var c = ca[i];
  296. while (c.charAt(0) == ' ') c = c.substring(1);
  297. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  298. }
  299. return "";
  300. },
  301. /**
  302. * removeCookie 删除cookie
  303. * @param cname 名称
  304. */
  305. removeCookie: function (cname) {
  306. var exp = new Date();
  307. exp.setTime(exp.getTime() - 1);
  308. document.cookie = cname + "=; expires=" + exp.toGMTString();
  309. },
  310. /**
  311. * getParam 获取url参数
  312. * @param key 当前url链接
  313. */
  314. getParam: function (key) {
  315. var json = {}, data;
  316. $.each(location.search.substr(1).split("&"), function (i, n) {
  317. data = n.split("=");
  318. json[data[0]] = data[1];
  319. });
  320. return key != undefined ? json[key] : json;
  321. },
  322. /**
  323. * getParam 判断对象是否有内容
  324. * @param obj 传入对象
  325. */
  326. isEmptyObject: function (obj) {
  327. for (var key in obj) {
  328. return false;
  329. }
  330. return true;
  331. },
  332. /**
  333. * skip 页面跳转
  334. * @param url 跳转页面链接
  335. */
  336. skip: function (url) {
  337. if (url != '') {
  338. window.location = url
  339. }
  340. },
  341. /**
  342. * getNowFormatDate 获取时间
  343. * @param
  344. */
  345. getNowFormatDate:function () {
  346. var date = new Date();
  347. var seperator1 = "-";
  348. var seperator2 = ":";
  349. var month = date.getMonth() + 1;
  350. var day = date.getDate();
  351. var hours = date.getHours();
  352. var minutes = date.getMinutes();
  353. var seconds = date.getSeconds();
  354. if (month >= 1 && month <= 9) {
  355. month = "0" + month;
  356. }
  357. if (day >= 0 && day <= 9) {
  358. day = "0" + day;
  359. }
  360. if (hours >= 0 && hours <= 9) {
  361. hours = "0" + hours;
  362. }
  363. if (minutes >= 0 && minutes <= 9) {
  364. minutes = "0" + minutes;
  365. }
  366. if (seconds >= 0 && seconds <= 9) {
  367. seconds = "0" + seconds;
  368. }
  369. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;
  370. console.log(currentdate)
  371. return currentdate;
  372. },
  373. /**
  374. * getWebConfig 网站配置信息
  375. * @param
  376. */
  377. getWebConfig: function () {
  378. this.doGet(webConfig, {}, this.webConfigInformation, true)
  379. },
  380. /**
  381. * webConfigInformation 网站配置信息
  382. * @param
  383. */
  384. webConfigInformation : function (data) {
  385. console.log(data)
  386. if (data.code == 200) {
  387. var content = data.data;
  388. $("#webConfigName").html(content[0].configValue);
  389. $("#webConfigRecord").html(content[1].configValue + content[2].configValue);
  390. $("#webConfigAddress").html(content[0].configValue+'&nbsp;'+content[3].configValue);
  391. }
  392. },
  393. });
  394. return Tool;
  395. });