网站
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

tools.js 14 KiB

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