网站
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

483 lines
14 KiB

  1. var serverApi = '';
  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. console.log(url);
  188. $.ajax({
  189. url: url,
  190. type: 'GET',
  191. beforeSend: headAttribute,
  192. success: function (data) {
  193. var code = data.code;
  194. var msg = data.msg;
  195. if (code === 401) {
  196. _this.removeAllCookie();
  197. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  198. _this.skip('/view/login/login.html')
  199. }, '重新登录', function () { }, "取消")
  200. } else if (code === 500) {
  201. _this.initError(msg)
  202. } else if (code != 200) {
  203. _this.initError(msg)
  204. } else {
  205. cb(data);
  206. }
  207. },
  208. error: function (data) {
  209. console.log(data)
  210. }
  211. });
  212. },
  213. /**
  214. * ajax delete
  215. * @param url(String)
  216. * @param data(Json) 需要提交的数据
  217. * @param cb(Function) 回调函数
  218. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  219. */
  220. doDelete: function (uri, data, cb, Bearer) {
  221. var _this = this;
  222. var url = ajaxJsUrl + uri + '?';
  223. var headAttribute = ''
  224. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  225. headAttribute = function (xhr) {
  226. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  227. }
  228. } else {
  229. headAttribute = function (xhr) {
  230. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  231. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  232. }
  233. }
  234. if (data) {
  235. url += $.map(data, function (n, i) {
  236. if (i == data.length - 1) {
  237. return i + '=' + n;
  238. } else {
  239. return i + '=' + n + '&';
  240. }
  241. }).join('');
  242. }
  243. $.ajax({
  244. url: url,
  245. type: 'delete',
  246. beforeSend: headAttribute,
  247. success: function (data) {
  248. var code = data.code;
  249. var msg = data.msg;
  250. if (code === 401) {
  251. _this.removeAllCookie();
  252. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  253. _this.skip('/view/login/login.html')
  254. }, '重新登录', function () { }, "取消")
  255. } else if (code === 500) {
  256. _this.initError(msg)
  257. } else if (code != 200) {
  258. _this.initError(msg)
  259. } else {
  260. cb(data);
  261. }
  262. },
  263. error: function (data) {
  264. console.log(data)
  265. }
  266. });
  267. },
  268. /**
  269. * 创建dialog
  270. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  271. * @param title (String) dialog的title
  272. * @param content(String || HTMLElement) dialog的内容
  273. * @param ok (Function) 点击确定按钮的回调函数
  274. * @param cancel(Function) 点击取消按钮的回调函数
  275. * 可以通过showModal来实现modal显示
  276. */
  277. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  278. var initDialog = dialog({
  279. id: id ? id : '',
  280. title: title,
  281. content: content,
  282. okValue: okValue ? okValue : '确定',
  283. ok: ok,
  284. cancelValue: cancelValue ? cancelValue : '取消',
  285. cancel: cancel,
  286. padding: 40,
  287. top: '150px'
  288. });
  289. initDialog.show();
  290. },
  291. /**
  292. * 创建error dialog
  293. * @param errContent
  294. */
  295. initError: function (errContent) {
  296. var error = dialog({
  297. content: errContent,
  298. padding: 20
  299. });
  300. error.show();
  301. var time = setTimeout(function () {
  302. error.remove();
  303. }, 1500);
  304. },
  305. /**
  306. * 创建dialog tips
  307. * @param tips
  308. * @param align
  309. */
  310. initTips: function (tips, align, $target, times) {
  311. var tips = dialog({
  312. content: tips,
  313. padding: 20,
  314. skin: 'min-dialog tips',
  315. quickClose: true,
  316. align: align
  317. });
  318. tips.show($target);
  319. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  320. times = 2000
  321. } else {
  322. times = times
  323. }
  324. setTimeout(function () {
  325. tips.close().remove();
  326. }, times);
  327. },
  328. /**
  329. * setCookie 设置cookie
  330. * @param cname 名称
  331. * @param cvalue 值
  332. * @param seconds 有效期
  333. */
  334. setCookie: function (cname, cvalue, seconds) {
  335. var d = new Date();
  336. d.setTime(d.getTime() + (seconds * 1000));
  337. var expires = "expires=" + d.toGMTString();
  338. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  339. },
  340. /**
  341. * getCookie 获取cookie
  342. * @param cname 名称
  343. */
  344. getCookie: function (cname) {
  345. var name = cname + "=";
  346. var ca = document.cookie.split(';');
  347. for (var i = 0; i < ca.length; i++) {
  348. var c = ca[i];
  349. while (c.charAt(0) == ' ') c = c.substring(1);
  350. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  351. }
  352. return "";
  353. },
  354. /**
  355. * removeCookie 删除cookie
  356. * @param cname 名称
  357. */
  358. removeCookie: function (cname) {
  359. var exp = new Date();
  360. exp.setTime(exp.getTime() - 1);
  361. document.cookie = cname + "=; expires=" + exp.toGMTString();
  362. },
  363. /**
  364. * getParam 获取url参数
  365. * @param key 当前url链接
  366. */
  367. getParam: function (key) {
  368. var json = {}, data;
  369. $.each(location.search.substr(1).split("&"), function (i, n) {
  370. data = n.split("=");
  371. json[data[0]] = data[1];
  372. });
  373. return key != undefined ? json[key] : json;
  374. },
  375. /**
  376. * getParam 判断对象是否有内容
  377. * @param obj 传入对象
  378. */
  379. isEmptyObject: function (obj) {
  380. for (var key in obj) {
  381. return false;
  382. }
  383. return true;
  384. },
  385. /**
  386. * skip 页面跳转
  387. * @param url 跳转页面链接
  388. */
  389. skip: function (url) {
  390. if (url != '') {
  391. window.location = url
  392. }
  393. },
  394. /**
  395. * getNowFormatDate 获取时间
  396. * @param
  397. */
  398. getNowFormatDate: function () {
  399. var date = new Date();
  400. var seperator1 = "-";
  401. var seperator2 = ":";
  402. var month = date.getMonth() + 1;
  403. var day = date.getDate();
  404. var hours = date.getHours();
  405. var minutes = date.getMinutes();
  406. var seconds = date.getSeconds();
  407. if (month >= 1 && month <= 9) {
  408. month = "0" + month;
  409. }
  410. if (day >= 0 && day <= 9) {
  411. day = "0" + day;
  412. }
  413. if (hours >= 0 && hours <= 9) {
  414. hours = "0" + hours;
  415. }
  416. if (minutes >= 0 && minutes <= 9) {
  417. minutes = "0" + minutes;
  418. }
  419. if (seconds >= 0 && seconds <= 9) {
  420. seconds = "0" + seconds;
  421. }
  422. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;
  423. console.log(currentdate)
  424. return currentdate;
  425. },
  426. /**
  427. * getWebConfig 网站配置信息
  428. * @param
  429. */
  430. getWebConfig: function () {
  431. this.doGet(webConfig, {}, this.webConfigInformation, true)
  432. },
  433. /**
  434. * webConfigInformation 网站配置信息
  435. * @param
  436. */
  437. webConfigInformation: function (data) {
  438. if (data.code == 200) {
  439. var content = data.data;
  440. console.log(content)
  441. // content[13].configValue = 'red';
  442. if (content[13].configValue == 'red'){
  443. themeColor = 'red';
  444. document.documentElement.style.setProperty('--color', '#e8041f');
  445. document.documentElement.style.setProperty('--icon', "url('../images/icon3_red.png')");
  446. document.documentElement.style.setProperty('--logo', "url('../images/logo_red.png')");
  447. logo = '../images/logo_red.png';
  448. }
  449. if (content[13].configValue == 'green'){
  450. themeColor = 'green';
  451. document.documentElement.style.setProperty('--color', '#007b76');
  452. document.documentElement.style.setProperty('--icon', "url('../images/icon3.png')");
  453. document.documentElement.style.setProperty('--logo', "url('../images/logo.png')");
  454. logo = '../images/logo.png';
  455. }
  456. $("#webConfigName").html(content[0].configValue);
  457. $("#webConfigRecord").html(content[1].configValue + '<span style="margin-left: 10px;"></span>' + content[2].configValue);
  458. $("#webConfigAddress").html(content[0].configValue + '<span style="margin-left: 10px;"></span>' + content[3].configValue);
  459. serverApi = content[11].configValue;
  460. }
  461. },
  462. /**
  463. * removeAllCookie 清除所有Cookie
  464. * @param
  465. */
  466. removeAllCookie: function () {
  467. var keys = document.cookie.match(/[^ =;]+(?==)/g)
  468. if (keys) {
  469. for (var i = keys.length; i--;) {
  470. document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
  471. }
  472. location.reload();
  473. }
  474. },
  475. });
  476. return Tool;
  477. });