农燊高科官方网站
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tools.js 11 KiB

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