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

tools.js 16 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年前
4年前
4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. var serverApi = '';
  2. var friendsLinksList = [];
  3. var themeColor = '';
  4. var logo = '';
  5. define(['jquery', 'dialog','jsencrypt'], function ($, dialog,JSEncrypt) {
  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. doPostSign: function (url, data, cb, Bearer) {
  62. var _this = this;
  63. // data.deptId = 100 ;
  64. // console.log(data)
  65. var headAttribute = '';
  66. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  67. headAttribute = function (xhr) {
  68. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  69. }
  70. } else {
  71. headAttribute = function (xhr) {
  72. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  73. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  74. }
  75. }
  76. $.ajax({
  77. url: ajaxJsUrl + url + '?=' + Math.random(),
  78. type: 'POST',
  79. data: data,
  80. dataType: 'json',
  81. contentType: false,
  82. processData: false, // jQuery不要去处理发送的数据
  83. success: function (data) {
  84. var code = data.code;
  85. var msg = data.msg;
  86. if (code === 401) {
  87. _this.removeAllCookie();
  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 post
  104. * @param url (String)
  105. * @param data (Json) 需要提交的数据
  106. * @param cb(Function) 回调函数
  107. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  108. */
  109. doPostImg: function (url, data, cb, Bearer) {
  110. var _this = this;
  111. // data.deptId = 100 ;
  112. // console.log(data)
  113. var headAttribute = '';
  114. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  115. headAttribute = function (xhr) {
  116. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  117. }
  118. } else {
  119. headAttribute = function (xhr) {
  120. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  121. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  122. }
  123. }
  124. $.ajax({
  125. url: ajaxJsUrl + url + '?=' + Math.random(),
  126. type: 'POST',
  127. data: JSON.stringify(data),
  128. dataType: 'json',
  129. traditional:true,
  130. contentType: "application/json",
  131. beforeSend: headAttribute,
  132. success: function (data) {
  133. var code = data.code;
  134. var msg = data.msg;
  135. if (code === 401) {
  136. _this.removeAllCookie();
  137. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  138. _this.skip('/view/login/login.html')
  139. }, '重新登录', function () { }, "取消")
  140. } else if (code === 500) {
  141. _this.initError(msg)
  142. cb(data);
  143. } else if (code != 200) {
  144. _this.initError(msg)
  145. } else {
  146. cb(data);
  147. }
  148. }
  149. });
  150. },
  151. /**
  152. * ajax put
  153. * @param url (String)
  154. * @param data (Json) 需要提交的数据
  155. * @param cb(Function) 回调函数
  156. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  157. */
  158. doPut: function (url, data, cb, Bearer) {
  159. var _this = this;
  160. var headAttribute = '';
  161. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  162. headAttribute = function (xhr) {
  163. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  164. }
  165. } else {
  166. headAttribute = function (xhr) {
  167. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  168. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  169. }
  170. }
  171. $.ajax({
  172. url: ajaxJsUrl + url + '?=' + Math.random(),
  173. type: 'put',
  174. data: JSON.stringify(data),
  175. dataType: 'json',
  176. contentType: "application/json",
  177. beforeSend: headAttribute,
  178. success: function (data) {
  179. var code = data.code;
  180. var msg = data.msg;
  181. if (code === 401) {
  182. _this.removeAllCookie();
  183. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  184. _this.skip('/view/login/login.html')
  185. }, '重新登录', function () { }, "取消")
  186. } else if (code === 500) {
  187. _this.initError(msg)
  188. cb(data);
  189. } else if (code != 200) {
  190. _this.initError(msg)
  191. } else {
  192. cb(data);
  193. }
  194. }
  195. });
  196. },
  197. /**
  198. * ajax get
  199. * @param url(String)
  200. * @param data(Json) 需要提交的数据
  201. * @param cb(Function) 回调函数
  202. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  203. */
  204. doGet: function (uri, data, cb, Bearer) {
  205. var _this = this;
  206. // deptId=100&
  207. var url = ajaxJsUrl + uri + '?';
  208. var headAttribute = ''
  209. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  210. headAttribute = function (xhr) {
  211. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  212. }
  213. } else {
  214. headAttribute = function (xhr) {
  215. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  216. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  217. }
  218. }
  219. if (data) {
  220. url += $.map(data, function (n, i) {
  221. if (i == data.length - 1) {
  222. return i + '=' + n;
  223. } else {
  224. return i + '=' + n + '&';
  225. }
  226. }).join('');
  227. }
  228. $.ajax({
  229. url: url,
  230. type: 'GET',
  231. beforeSend: headAttribute,
  232. success: function (data) {
  233. var code = data.code;
  234. var msg = data.msg;
  235. if (code === 401) {
  236. _this.removeAllCookie();
  237. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  238. _this.skip('/view/login/login.html')
  239. }, '重新登录', function () { }, "取消")
  240. } else if (code === 500) {
  241. _this.initError(msg)
  242. } else if (code != 200) {
  243. _this.initError(msg)
  244. } else {
  245. cb(data);
  246. }
  247. },
  248. error: function (data) {
  249. console.log(data)
  250. }
  251. });
  252. },
  253. /**
  254. * ajax delete
  255. * @param url(String)
  256. * @param data(Json) 需要提交的数据
  257. * @param cb(Function) 回调函数
  258. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  259. */
  260. doDelete: function (uri, data, cb, Bearer) {
  261. var _this = this;
  262. var url = ajaxJsUrl + uri + '?';
  263. var headAttribute = ''
  264. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  265. headAttribute = function (xhr) {
  266. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  267. }
  268. } else {
  269. headAttribute = function (xhr) {
  270. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  271. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  272. }
  273. }
  274. if (data) {
  275. url += $.map(data, function (n, i) {
  276. if (i == data.length - 1) {
  277. return i + '=' + n;
  278. } else {
  279. return i + '=' + n + '&';
  280. }
  281. }).join('');
  282. }
  283. $.ajax({
  284. url: url,
  285. type: 'delete',
  286. beforeSend: headAttribute,
  287. success: function (data) {
  288. var code = data.code;
  289. var msg = data.msg;
  290. if (code === 401) {
  291. _this.removeAllCookie();
  292. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  293. _this.skip('/view/login/login.html')
  294. }, '重新登录', function () { }, "取消")
  295. } else if (code === 500) {
  296. _this.initError(msg)
  297. } else if (code != 200) {
  298. _this.initError(msg)
  299. } else {
  300. cb(data);
  301. }
  302. },
  303. error: function (data) {
  304. console.log(data)
  305. }
  306. });
  307. },
  308. /**
  309. * 创建dialog
  310. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  311. * @param title (String) dialog的title
  312. * @param content(String || HTMLElement) dialog的内容
  313. * @param ok (Function) 点击确定按钮的回调函数
  314. * @param cancel(Function) 点击取消按钮的回调函数
  315. * 可以通过showModal来实现modal显示
  316. */
  317. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  318. var initDialog = dialog({
  319. id: id ? id : '',
  320. title: title,
  321. content: content,
  322. okValue: okValue ? okValue : '确定',
  323. ok: ok,
  324. cancelValue: cancelValue ? cancelValue : '取消',
  325. cancel: cancel,
  326. padding: 40,
  327. top: '150px'
  328. });
  329. initDialog.show();
  330. },
  331. /**
  332. * 创建error dialog
  333. * @param errContent
  334. */
  335. initError: function (errContent) {
  336. var error = dialog({
  337. content: errContent,
  338. padding: 20
  339. });
  340. error.show();
  341. var time = setTimeout(function () {
  342. error.remove();
  343. }, 1500);
  344. },
  345. /**
  346. * 创建dialog tips
  347. * @param tips
  348. * @param align
  349. */
  350. initTips: function (tips, align, $target, times) {
  351. var tips = dialog({
  352. content: tips,
  353. padding: 20,
  354. skin: 'min-dialog tips',
  355. quickClose: true,
  356. align: align
  357. });
  358. tips.show($target);
  359. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  360. times = 2000
  361. } else {
  362. times = times
  363. }
  364. setTimeout(function () {
  365. tips.close().remove();
  366. }, times);
  367. },
  368. /**
  369. * setCookie 设置cookie
  370. * @param cname 名称
  371. * @param cvalue 值
  372. * @param seconds 有效期
  373. */
  374. setCookie: function (cname, cvalue, seconds) {
  375. var d = new Date();
  376. d.setTime(d.getTime() + (seconds * 1000));
  377. var expires = "expires=" + d.toGMTString();
  378. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  379. },
  380. /**
  381. * getCookie 获取cookie
  382. * @param cname 名称
  383. */
  384. getCookie: function (cname) {
  385. var name = cname + "=";
  386. var ca = document.cookie.split(';');
  387. for (var i = 0; i < ca.length; i++) {
  388. var c = ca[i];
  389. while (c.charAt(0) == ' ') c = c.substring(1);
  390. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  391. }
  392. return "";
  393. },
  394. /**
  395. * removeCookie 删除cookie
  396. * @param cname 名称
  397. */
  398. removeCookie: function (cname) {
  399. var exp = new Date();
  400. exp.setTime(exp.getTime() - 1);
  401. document.cookie = cname + "=; expires=" + exp.toGMTString();
  402. },
  403. /**
  404. * getParam 获取url参数
  405. * @param key 当前url链接
  406. */
  407. getParam: function (key) {
  408. var json = {}, data;
  409. $.each(location.search.substr(1).split("&"), function (i, n) {
  410. data = n.split("=");
  411. json[data[0]] = data[1];
  412. });
  413. return key != undefined ? json[key] : json;
  414. },
  415. /**
  416. * getParam 判断对象是否有内容
  417. * @param obj 传入对象
  418. */
  419. isEmptyObject: function (obj) {
  420. for (var key in obj) {
  421. return false;
  422. }
  423. return true;
  424. },
  425. /**
  426. * skip 页面跳转
  427. * @param url 跳转页面链接
  428. */
  429. skip: function (url) {
  430. if (url != '') {
  431. window.location = url
  432. }
  433. },
  434. /**
  435. * getNowFormatDate 获取时间
  436. * @param
  437. */
  438. getNowFormatDate: function () {
  439. var date = new Date();
  440. var seperator1 = "-";
  441. var seperator2 = ":";
  442. var month = date.getMonth() + 1;
  443. var day = date.getDate();
  444. var hours = date.getHours();
  445. var minutes = date.getMinutes();
  446. var seconds = date.getSeconds();
  447. if (month >= 1 && month <= 9) {
  448. month = "0" + month;
  449. }
  450. if (day >= 0 && day <= 9) {
  451. day = "0" + day;
  452. }
  453. if (hours >= 0 && hours <= 9) {
  454. hours = "0" + hours;
  455. }
  456. if (minutes >= 0 && minutes <= 9) {
  457. minutes = "0" + minutes;
  458. }
  459. if (seconds >= 0 && seconds <= 9) {
  460. seconds = "0" + seconds;
  461. }
  462. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;
  463. console.log(currentdate)
  464. return currentdate;
  465. },
  466. /**
  467. * getWebConfig 网站配置信息
  468. * @param
  469. */
  470. getWebConfig: function () {
  471. this.doGet(webConfig, {}, this.webConfigInformation, true)
  472. },
  473. /**
  474. * webConfigInformation 网站配置信息
  475. * @param
  476. */
  477. webConfigInformation: function (data) {
  478. if (data.code == 200) {
  479. var content = data.data;
  480. console.log(content)
  481. // content[13].configValue = 'red';
  482. if (content[13].configValue == 'red'){
  483. themeColor = 'red';
  484. document.documentElement.style.setProperty('--color', '#e8041f');
  485. document.documentElement.style.setProperty('--icon', "url('../images/icon3_red.png')");
  486. document.documentElement.style.setProperty('--logo', "url('../images/logo_red.png')");
  487. logo = '../images/logo_red.png';
  488. }
  489. if (content[13].configValue == 'green'){
  490. themeColor = 'green';
  491. document.documentElement.style.setProperty('--color', '#007b76');
  492. document.documentElement.style.setProperty('--icon', "url('../images/icon3.png')");
  493. document.documentElement.style.setProperty('--logo', "url('../images/logo.png')");
  494. logo = '../images/logo.png';
  495. }
  496. $("#webConfigName").html(content[0].configValue);
  497. $("#webConfigRecord").html(content[1].configValue + '<span style="margin-left: 10px;"></span>' + content[2].configValue);
  498. $("#webConfigAddress").html(content[0].configValue + '<span style="margin-left: 10px;"></span>' + content[3].configValue);
  499. serverApi = content[11].configValue;
  500. }
  501. },
  502. /**
  503. * removeAllCookie 清除所有Cookie
  504. * @param
  505. */
  506. removeAllCookie: function () {
  507. var keys = document.cookie.match(/[^ =;]+(?==)/g)
  508. if (keys) {
  509. for (var i = keys.length; i--;) {
  510. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
  511. }
  512. location.reload();
  513. }
  514. },
  515. /**
  516. * encrypt 加密
  517. * @param
  518. */
  519. encrypt: function (txt) {
  520. const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  521. 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
  522. const encryptor = new JSEncrypt()
  523. encryptor.setPublicKey(publicKey) // 设置公钥
  524. return encryptor.encrypt(txt) // 对数据进行加密
  525. },
  526. /**
  527. * encrypt 解密
  528. * @param
  529. */
  530. decrypt: function (txt) {
  531. const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
  532. '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
  533. 'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
  534. 'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
  535. 'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
  536. 'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
  537. 'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
  538. 'UP8iWi1Qw0Y='
  539. const encryptor = new JSEncrypt()
  540. encryptor.setPrivateKey(privateKey) // 设置私钥
  541. return encryptor.decrypt(txt) // 对数据进行解密
  542. },
  543. });
  544. return Tool;
  545. });