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

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