农燊高科官方网站
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

464 wiersze
13 KiB

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