网站
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

630 行
18 KiB

  1. var serverApi = '';
  2. var friendsLinksList = [];
  3. var themeColor = '';
  4. var logo = '';
  5. var lastTime = 0.00;
  6. define(['jquery', 'dialog','jsencrypt'], function ($, dialog,JSEncrypt) {
  7. // 工具类
  8. function Tool() {
  9. this.version = "1.0.0";
  10. this.description = "这是一个工具类";
  11. }
  12. var ajaxJsUrl = "/api";
  13. $.extend(Tool.prototype, {
  14. /**
  15. * ajax post
  16. * @param url (String)
  17. * @param data (Json) 需要提交的数据
  18. * @param cb(Function) 回调函数
  19. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  20. */
  21. doPost: function (url, data, cb, Bearer) {
  22. var _this = this;
  23. // data.deptId = 100 ;
  24. // console.log(data)
  25. var headAttribute = '';
  26. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  27. headAttribute = function (xhr) {
  28. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  29. }
  30. } else {
  31. headAttribute = function (xhr) {
  32. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  33. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  34. }
  35. }
  36. $.ajax({
  37. url: ajaxJsUrl + url + '?=' + Math.random(),
  38. type: 'POST',
  39. data: JSON.stringify(data),
  40. dataType: 'json',
  41. contentType: "application/json",
  42. beforeSend: headAttribute,
  43. success: function (data) {
  44. var code = data.code;
  45. var msg = data.msg;
  46. if (code === 401) {
  47. _this.removeAllCookie();
  48. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  49. _this.skip('/view/login/login.html')
  50. }, '重新登录', function () { }, "取消")
  51. } else if (code === 500) {
  52. _this.initError(msg)
  53. cb(data);
  54. } else if (code != 200) {
  55. _this.initError(msg)
  56. } else {
  57. cb(data);
  58. }
  59. }
  60. });
  61. },
  62. doPostSign: function (url, data, cb, Bearer) {
  63. var _this = this;
  64. // data.deptId = 100 ;
  65. // console.log(data)
  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: 'POST',
  80. data: data,
  81. dataType: 'json',
  82. contentType: false,
  83. processData: false, // jQuery不要去处理发送的数据
  84. success: function (data) {
  85. var code = data.code;
  86. var msg = data.msg;
  87. if (code === 401) {
  88. _this.removeAllCookie();
  89. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  90. _this.skip('/view/login/login.html')
  91. }, '重新登录', function () { }, "取消")
  92. } else if (code === 500) {
  93. _this.initError(msg)
  94. cb(data);
  95. } else if (code != 200) {
  96. _this.initError(msg)
  97. } else {
  98. cb(data);
  99. }
  100. }
  101. });
  102. },
  103. /**
  104. * ajax post
  105. * @param url (String)
  106. * @param data (Json) 需要提交的数据
  107. * @param cb(Function) 回调函数
  108. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  109. */
  110. doPostImg: function (url, data, cb, Bearer) {
  111. var _this = this;
  112. // data.deptId = 100 ;
  113. // console.log(data)
  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. $.ajax({
  126. url: ajaxJsUrl + url + '?=' + Math.random(),
  127. type: 'POST',
  128. data: JSON.stringify(data),
  129. dataType: 'json',
  130. traditional:true,
  131. contentType: "application/json",
  132. beforeSend: headAttribute,
  133. success: function (data) {
  134. var code = data.code;
  135. var msg = data.msg;
  136. if (code === 401) {
  137. _this.removeAllCookie();
  138. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  139. _this.skip('/view/login/login.html')
  140. }, '重新登录', function () { }, "取消")
  141. } else if (code === 500) {
  142. _this.initError(msg)
  143. cb(data);
  144. } else if (code != 200) {
  145. _this.initError(msg)
  146. } else {
  147. cb(data);
  148. }
  149. }
  150. });
  151. },
  152. /**
  153. * ajax put
  154. * @param url (String)
  155. * @param data (Json) 需要提交的数据
  156. * @param cb(Function) 回调函数
  157. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  158. */
  159. doPut: function (url, data, cb, Bearer) {
  160. var _this = this;
  161. var headAttribute = '';
  162. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  163. headAttribute = function (xhr) {
  164. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  165. }
  166. } else {
  167. headAttribute = function (xhr) {
  168. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  169. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  170. }
  171. }
  172. $.ajax({
  173. url: ajaxJsUrl + url + '?=' + Math.random(),
  174. type: 'put',
  175. data: JSON.stringify(data),
  176. dataType: 'json',
  177. contentType: "application/json",
  178. beforeSend: headAttribute,
  179. success: function (data) {
  180. var code = data.code;
  181. var msg = data.msg;
  182. if (code === 401) {
  183. _this.removeAllCookie();
  184. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  185. _this.skip('/view/login/login.html')
  186. }, '重新登录', function () { }, "取消")
  187. } else if (code === 500) {
  188. _this.initError(msg)
  189. cb(data);
  190. } else if (code != 200) {
  191. _this.initError(msg)
  192. } else {
  193. cb(data);
  194. }
  195. }
  196. });
  197. },
  198. /**
  199. * ajax get
  200. * @param url(String)
  201. * @param data(Json) 需要提交的数据
  202. * @param cb(Function) 回调函数
  203. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  204. */
  205. doGet: function (uri, data, cb, Bearer) {
  206. var _this = this;
  207. // deptId=100&
  208. var url = ajaxJsUrl + uri + '?';
  209. var headAttribute = ''
  210. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  211. headAttribute = function (xhr) {
  212. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  213. }
  214. } else {
  215. headAttribute = function (xhr) {
  216. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  217. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  218. }
  219. }
  220. if (data) {
  221. url += $.map(data, function (n, i) {
  222. if (i == data.length - 1) {
  223. return i + '=' + n;
  224. } else {
  225. return i + '=' + n + '&';
  226. }
  227. }).join('');
  228. }
  229. $.ajax({
  230. url: url,
  231. type: 'GET',
  232. beforeSend: headAttribute,
  233. success: function (data) {
  234. var code = data.code;
  235. var msg = data.msg;
  236. if (code === 401) {
  237. _this.removeAllCookie();
  238. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  239. _this.skip('/view/login/login.html')
  240. }, '重新登录', function () { }, "取消")
  241. } else if (code === 500) {
  242. _this.initError(msg)
  243. } else if (code != 200) {
  244. _this.initError(msg)
  245. } else {
  246. cb(data);
  247. }
  248. },
  249. error: function (data) {
  250. console.log(data)
  251. }
  252. });
  253. },
  254. /**
  255. * ajax delete
  256. * @param url(String)
  257. * @param data(Json) 需要提交的数据
  258. * @param cb(Function) 回调函数
  259. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  260. */
  261. doDelete: function (uri, data, cb, Bearer) {
  262. var _this = this;
  263. var url = ajaxJsUrl + uri + '?';
  264. var headAttribute = ''
  265. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  266. headAttribute = function (xhr) {
  267. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  268. }
  269. } else {
  270. headAttribute = function (xhr) {
  271. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  272. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  273. }
  274. }
  275. if (data) {
  276. url += $.map(data, function (n, i) {
  277. if (i == data.length - 1) {
  278. return i + '=' + n;
  279. } else {
  280. return i + '=' + n + '&';
  281. }
  282. }).join('');
  283. }
  284. $.ajax({
  285. url: url,
  286. type: 'delete',
  287. beforeSend: headAttribute,
  288. success: function (data) {
  289. var code = data.code;
  290. var msg = data.msg;
  291. if (code === 401) {
  292. _this.removeAllCookie();
  293. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  294. _this.skip('/view/login/login.html')
  295. }, '重新登录', function () { }, "取消")
  296. } else if (code === 500) {
  297. _this.initError(msg)
  298. } else if (code != 200) {
  299. _this.initError(msg)
  300. } else {
  301. cb(data);
  302. }
  303. },
  304. error: function (data) {
  305. console.log(data)
  306. }
  307. });
  308. },
  309. /**
  310. * 创建dialog
  311. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  312. * @param title (String) dialog的title
  313. * @param content(String || HTMLElement) dialog的内容
  314. * @param ok (Function) 点击确定按钮的回调函数
  315. * @param cancel(Function) 点击取消按钮的回调函数
  316. * 可以通过showModal来实现modal显示
  317. */
  318. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  319. var initDialog = dialog({
  320. id: id ? id : '',
  321. title: title,
  322. content: content,
  323. okValue: okValue ? okValue : '确定',
  324. ok: ok,
  325. cancelValue: cancelValue ? cancelValue : '取消',
  326. cancel: cancel,
  327. padding: 40,
  328. top: '150px'
  329. });
  330. initDialog.show();
  331. },
  332. /**
  333. * 创建error dialog
  334. * @param errContent
  335. */
  336. initError: function (errContent) {
  337. var error = dialog({
  338. content: errContent,
  339. padding: 20
  340. });
  341. error.show();
  342. var time = setTimeout(function () {
  343. error.remove();
  344. }, 1500);
  345. },
  346. /**
  347. * 创建dialog tips
  348. * @param tips
  349. * @param align
  350. */
  351. initTips: function (tips, align, $target, times) {
  352. var tips = dialog({
  353. content: tips,
  354. padding: 20,
  355. skin: 'min-dialog tips',
  356. quickClose: true,
  357. align: align
  358. });
  359. tips.show($target);
  360. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  361. times = 2000
  362. } else {
  363. times = times
  364. }
  365. setTimeout(function () {
  366. tips.close().remove();
  367. }, times);
  368. },
  369. /**
  370. * setCookie 设置cookie
  371. * @param cname 名称
  372. * @param cvalue 值
  373. * @param seconds 有效期
  374. */
  375. setCookie: function (cname, cvalue, seconds) {
  376. var d = new Date();
  377. d.setTime(d.getTime() + (seconds * 1000));
  378. var expires = "expires=" + d.toGMTString();
  379. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  380. },
  381. /**
  382. * getCookie 获取cookie
  383. * @param cname 名称
  384. */
  385. getCookie: function (cname) {
  386. var name = cname + "=";
  387. var ca = document.cookie.split(';');
  388. for (var i = 0; i < ca.length; i++) {
  389. var c = ca[i];
  390. while (c.charAt(0) == ' ') c = c.substring(1);
  391. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  392. }
  393. return "";
  394. },
  395. /**
  396. * removeCookie 删除cookie
  397. * @param cname 名称
  398. */
  399. removeCookie: function (cname) {
  400. var exp = new Date();
  401. exp.setTime(exp.getTime() - 1);
  402. document.cookie = cname + "=; expires=" + exp.toGMTString();
  403. },
  404. /**
  405. * getParam 获取url参数
  406. * @param key 当前url链接
  407. */
  408. getParam: function (key) {
  409. var json = {}, data;
  410. $.each(location.search.substr(1).split("&"), function (i, n) {
  411. data = n.split("=");
  412. json[data[0]] = data[1];
  413. });
  414. return key != undefined ? json[key] : json;
  415. },
  416. /**
  417. * getParam 判断对象是否有内容
  418. * @param obj 传入对象
  419. */
  420. isEmptyObject: function (obj) {
  421. for (var key in obj) {
  422. return false;
  423. }
  424. return true;
  425. },
  426. /**
  427. * skip 页面跳转
  428. * @param url 跳转页面链接
  429. */
  430. skip: function (url) {
  431. if (url != '') {
  432. window.open(url)
  433. }
  434. },
  435. /**
  436. * getNowFormatDate 获取时间
  437. * @param
  438. */
  439. getNowFormatDate: function () {
  440. var date = new Date();
  441. var seperator1 = "-";
  442. var seperator2 = ":";
  443. var month = date.getMonth() + 1;
  444. var day = date.getDate();
  445. var hours = date.getHours();
  446. var minutes = date.getMinutes();
  447. var seconds = date.getSeconds();
  448. if (month >= 1 && month <= 9) {
  449. month = "0" + month;
  450. }
  451. if (day >= 0 && day <= 9) {
  452. day = "0" + day;
  453. }
  454. if (hours >= 0 && hours <= 9) {
  455. hours = "0" + hours;
  456. }
  457. if (minutes >= 0 && minutes <= 9) {
  458. minutes = "0" + minutes;
  459. }
  460. if (seconds >= 0 && seconds <= 9) {
  461. seconds = "0" + seconds;
  462. }
  463. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;
  464. console.log(currentdate)
  465. return currentdate;
  466. },
  467. /**
  468. * getNowFormatDateNew 获取时间
  469. * @param
  470. */
  471. getNowFormatDateNew: function (data) {
  472. var date = data;
  473. var seperator1 = "-";
  474. var seperator2 = ":";
  475. var month = date.getMonth() + 1;
  476. var day = date.getDate();
  477. var hours = date.getHours();
  478. var minutes = date.getMinutes();
  479. var seconds = date.getSeconds();
  480. if (month >= 1 && month <= 9) {
  481. month = "0" + month;
  482. }
  483. if (day >= 0 && day <= 9) {
  484. day = "0" + day;
  485. }
  486. if (hours >= 0 && hours <= 9) {
  487. hours = "0" + hours;
  488. }
  489. if (minutes >= 0 && minutes <= 9) {
  490. minutes = "0" + minutes;
  491. }
  492. if (seconds >= 0 && seconds <= 9) {
  493. seconds = "0" + seconds;
  494. }
  495. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;
  496. console.log(currentdate)
  497. return currentdate;
  498. },
  499. /**
  500. * getWebConfig 网站配置信息
  501. * @param
  502. */
  503. getWebConfig: function () {
  504. this.doGet(webConfig, {}, this.webConfigInformation, true)
  505. },
  506. /**
  507. * webConfigInformation 网站配置信息
  508. * @param
  509. */
  510. webConfigInformation: function (data) {
  511. if (data.code == 200) {
  512. var content = data.data;
  513. console.log(content)
  514. // content[13].configValue = 'red';
  515. document.documentElement.style.setProperty('--color', '#1269d3');
  516. // if (content[13].configValue == 'red'){
  517. // themeColor = 'red';
  518. // document.documentElement.style.setProperty('--color', '#e8041f');
  519. // document.documentElement.style.setProperty('--icon', "url('../images/icon3_red.png')");
  520. // document.documentElement.style.setProperty('--logo', "url('../images/logo_red.png')");
  521. // logo = '../images/logo_red.png';
  522. // }
  523. // if (content[13].configValue == 'green'){
  524. // themeColor = 'green';
  525. // document.documentElement.style.setProperty('--color', '#007b76');
  526. // document.documentElement.style.setProperty('--icon', "url('../images/icon3.png')");
  527. // document.documentElement.style.setProperty('--logo', "url('../images/logo.png')");
  528. // logo = '../images/logo.png';
  529. // }
  530. $("#webConfigName").html(content[0].configValue);
  531. $("#webConfigRecord").html(content[1].configValue + '<span style="margin-left: 10px;"></span>' + content[2].configValue);
  532. $("#webConfigAddress").html(content[0].configValue + '<span style="margin-left: 10px;"></span>' + content[3].configValue);
  533. serverApi = content[11].configValue;
  534. lastTime = parseFloat(content[8].configValue);
  535. }
  536. },
  537. /**
  538. * removeAllCookie 清除所有Cookie
  539. * @param
  540. */
  541. removeAllCookie: function () {
  542. var keys = document.cookie.match(/[^ =;]+(?==)/g)
  543. if (keys) {
  544. for (var i = keys.length; i--;) {
  545. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
  546. }
  547. location.reload();
  548. }
  549. },
  550. /**
  551. * encrypt 加密
  552. * @param
  553. */
  554. encrypt: function (txt) {
  555. const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  556. 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
  557. const encryptor = new JSEncrypt()
  558. encryptor.setPublicKey(publicKey) // 设置公钥
  559. return encryptor.encrypt(txt) // 对数据进行加密
  560. },
  561. /**
  562. * encrypt 解密
  563. * @param
  564. */
  565. decrypt: function (txt) {
  566. const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
  567. '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
  568. 'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
  569. 'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
  570. 'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
  571. 'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
  572. 'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
  573. 'UP8iWi1Qw0Y='
  574. const encryptor = new JSEncrypt()
  575. encryptor.setPrivateKey(privateKey) // 设置私钥
  576. return encryptor.decrypt(txt) // 对数据进行解密
  577. },
  578. format: function (time, format) {
  579. var t = new Date(time);
  580. var tf = function (i) { return (i < 10 ? '0' : '') + i };
  581. return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
  582. switch (a) {
  583. case 'yyyy':
  584. return tf(t.getFullYear());
  585. break;
  586. case 'MM':
  587. return tf(t.getMonth() + 1);
  588. break;
  589. case 'mm':
  590. return tf(t.getMinutes());
  591. break;
  592. case 'dd':
  593. return tf(t.getDate());
  594. break;
  595. case 'HH':
  596. return tf(t.getHours());
  597. break;
  598. case 'ss':
  599. return tf(t.getSeconds());
  600. break;
  601. }
  602. })
  603. },
  604. /**
  605. * 计算出相差天数
  606. * @param secondSub
  607. */
  608. formatTotalDateSub: function (secondSub) {
  609. var days = Math.floor(secondSub / (24 * 3600)); // 计算出小时数
  610. var leave1 = secondSub % (24*3600) ; // 计算天数后剩余的毫秒数
  611. var hours = Math.floor(leave1 / 3600); // 计算相差分钟数
  612. var leave2 = leave1 % (3600); // 计算小时数后剩余的毫秒数
  613. var minutes = Math.floor(leave2 / 60); // 计算相差秒数
  614. var leave3 = leave2 % 60; // 计算分钟数后剩余的毫秒数
  615. var seconds = Math.round(leave3);
  616. return days + "天" + hours + "时" + minutes + "分" + seconds + '秒';
  617. }
  618. });
  619. return Tool;
  620. });