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

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