网站
選択できるのは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年前
4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.removeAllCookie();
  44. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  45. _this.skip('/view/login/login.html')
  46. }, '重新登录', function () { }, "取消")
  47. } else if (code === 500) {
  48. _this.initError(msg)
  49. cb(data);
  50. } else if (code != 200) {
  51. _this.initError(msg)
  52. } else {
  53. cb(data);
  54. }
  55. }
  56. });
  57. },
  58. /**
  59. * ajax put
  60. * @param url (String)
  61. * @param data (Json) 需要提交的数据
  62. * @param cb(Function) 回调函数
  63. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  64. */
  65. doPut: function (url, data, cb, Bearer) {
  66. var _this = this;
  67. var headAttribute = '';
  68. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  69. headAttribute = function (xhr) {
  70. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  71. }
  72. } else {
  73. headAttribute = function (xhr) {
  74. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  75. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  76. }
  77. }
  78. $.ajax({
  79. url: ajaxJsUrl + url + '?=' + Math.random(),
  80. type: 'put',
  81. data: JSON.stringify(data),
  82. dataType: 'json',
  83. contentType: "application/json",
  84. beforeSend: headAttribute,
  85. success: function (data) {
  86. var code = data.code;
  87. var msg = data.msg;
  88. if (code === 401) {
  89. _this.removeAllCookie();
  90. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  91. _this.skip('/view/login/login.html')
  92. }, '重新登录', function () { }, "取消")
  93. } else if (code === 500) {
  94. _this.initError(msg)
  95. cb(data);
  96. } else if (code != 200) {
  97. _this.initError(msg)
  98. } else {
  99. cb(data);
  100. }
  101. }
  102. });
  103. },
  104. /**
  105. * ajax get
  106. * @param url(String)
  107. * @param data(Json) 需要提交的数据
  108. * @param cb(Function) 回调函数
  109. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  110. */
  111. doGet: function (uri, data, cb, Bearer) {
  112. var _this = this;
  113. // deptId=100&
  114. var url = ajaxJsUrl + uri + '?';
  115. var headAttribute = ''
  116. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  117. headAttribute = function (xhr) {
  118. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  119. }
  120. } else {
  121. headAttribute = function (xhr) {
  122. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  123. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  124. }
  125. }
  126. if (data) {
  127. url += $.map(data, function (n, i) {
  128. if (i == data.length - 1) {
  129. return i + '=' + n;
  130. } else {
  131. return i + '=' + n + '&';
  132. }
  133. }).join('');
  134. }
  135. $.ajax({
  136. url: url,
  137. type: 'GET',
  138. beforeSend: headAttribute,
  139. success: function (data) {
  140. var code = data.code;
  141. var msg = data.msg;
  142. if (code === 401) {
  143. _this.removeAllCookie();
  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.removeAllCookie();
  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. * webConfigInformation 网站配置信息
  395. * @param
  396. */
  397. removeAllCookie : function () {
  398. var keys = document.cookie.match(/[^ =;]+(?==)/g)
  399. if (keys) {
  400. for (var i = keys.length; i--;) {
  401. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
  402. }
  403. location.reload();
  404. }
  405. },
  406. });
  407. return Tool;
  408. });