农燊高科官方网站
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

286 righe
7.6 KiB

  1. define(['jquery', 'dialog'], function ($, dialog) {
  2. // 工具类
  3. function Tool() {
  4. this.version = "1.0.0";
  5. this.description = "这是一个工具类";
  6. }
  7. var ajaxJsUrl = "/api";
  8. $.extend(Tool.prototype, {
  9. /**
  10. * ajax post
  11. * @param url (String)
  12. * @param data (Json) 需要提交的数据
  13. * @param cb(Function) 回调函数
  14. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  15. */
  16. doPost: function (url, data, cb, Bearer) {
  17. var _this = this;
  18. var headAttribute = '';
  19. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  20. headAttribute = function (xhr) {
  21. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  22. }
  23. } else {
  24. headAttribute = function (xhr) {
  25. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  26. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  27. }
  28. }
  29. $.ajax({
  30. url: ajaxJsUrl + url + '?=' + Math.random(),
  31. type: 'POST',
  32. data: JSON.stringify(data),
  33. dataType: 'json',
  34. contentType: "application/json",
  35. beforeSend: headAttribute,
  36. success: function (data) {
  37. var code = data.code;
  38. var msg = data.msg;
  39. if (code === 401) {
  40. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  41. _this.skip('/view/login/login.html')
  42. }, '重新登录', function () { }, "取消")
  43. } else if (code === 500) {
  44. _this.initError(msg)
  45. cb(data);
  46. } else if (code != 200) {
  47. _this.initError(msg)
  48. } else {
  49. cb(data);
  50. }
  51. }
  52. });
  53. },
  54. /**
  55. * ajax put
  56. * @param url (String)
  57. * @param data (Json) 需要提交的数据
  58. * @param cb(Function) 回调函数
  59. * @param Bearer(Boolean) 是否需要Bearer,不需要传true
  60. */
  61. doPut: function (url, data, cb, Bearer) {
  62. var _this = this;
  63. var headAttribute = '';
  64. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  65. headAttribute = function (xhr) {
  66. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  67. }
  68. } else {
  69. headAttribute = function (xhr) {
  70. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  71. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  72. }
  73. }
  74. $.ajax({
  75. url: ajaxJsUrl + url + '?=' + Math.random(),
  76. type: 'put',
  77. data: JSON.stringify(data),
  78. dataType: 'json',
  79. contentType: "application/json",
  80. beforeSend: headAttribute,
  81. success: function (data) {
  82. var code = data.code;
  83. var msg = data.msg;
  84. if (code === 401) {
  85. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  86. _this.skip('/view/login/login.html')
  87. }, '重新登录', function () { }, "取消")
  88. } else if (code === 500) {
  89. _this.initError(msg)
  90. cb(data);
  91. } else if (code != 200) {
  92. _this.initError(msg)
  93. } else {
  94. cb(data);
  95. }
  96. }
  97. });
  98. },
  99. /**
  100. * ajax get
  101. * @param url(String)
  102. * @param data(Json) 需要提交的数据
  103. * @param cb(Function) 回调函数
  104. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  105. */
  106. doGet: function (uri, data, cb, Bearer) {
  107. var _this = this;
  108. var url = ajaxJsUrl + uri + '?';
  109. var headAttribute = ''
  110. if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') {
  111. headAttribute = function (xhr) {
  112. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  113. }
  114. } else {
  115. headAttribute = function (xhr) {
  116. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  117. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  118. }
  119. }
  120. if (data) {
  121. url += $.map(data, function (n, i) {
  122. if (i == data.length - 1) {
  123. return i + '=' + n;
  124. } else {
  125. return i + '=' + n + '&';
  126. }
  127. }).join('');
  128. }
  129. $.ajax({
  130. url: url,
  131. type: 'GET',
  132. beforeSend: headAttribute,
  133. success: function (data) {
  134. var code = data.code;
  135. var msg = data.msg;
  136. if (code === 401) {
  137. _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () {
  138. _this.skip('/view/login/login.html')
  139. }, '重新登录', function () { }, "取消")
  140. } else if (code === 500) {
  141. _this.initError(msg)
  142. } else if (code != 200) {
  143. _this.initError(msg)
  144. } else {
  145. cb(data);
  146. }
  147. },
  148. error: function (data) {
  149. console.log(data)
  150. }
  151. });
  152. },
  153. /**
  154. * 创建dialog
  155. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  156. * @param title (String) dialog的title
  157. * @param content(String || HTMLElement) dialog的内容
  158. * @param ok (Function) 点击确定按钮的回调函数
  159. * @param cancel(Function) 点击取消按钮的回调函数
  160. * 可以通过showModal来实现modal显示
  161. */
  162. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  163. var initDialog = dialog({
  164. id: id ? id : '',
  165. title: title,
  166. content: content,
  167. okValue: okValue ? okValue : '确定',
  168. ok: ok,
  169. cancelValue: cancelValue ? cancelValue : '取消',
  170. cancel: cancel,
  171. padding: 40,
  172. top: '150px'
  173. });
  174. initDialog.show();
  175. },
  176. /**
  177. * 创建error dialog
  178. * @param errContent
  179. */
  180. initError: function (errContent) {
  181. var error = dialog({
  182. content: errContent,
  183. padding: 20
  184. });
  185. error.show();
  186. var time = setTimeout(function () {
  187. error.remove();
  188. }, 1500);
  189. },
  190. /**
  191. * 创建dialog tips
  192. * @param tips
  193. * @param align
  194. */
  195. initTips: function (tips, align, $target, times) {
  196. var tips = dialog({
  197. content: tips,
  198. padding: 20,
  199. skin: 'min-dialog tips',
  200. quickClose: true,
  201. align: align
  202. });
  203. tips.show($target);
  204. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  205. times = 2000
  206. } else {
  207. times = times
  208. }
  209. setTimeout(function () {
  210. tips.close().remove();
  211. }, times);
  212. },
  213. /**
  214. * setCookie 设置cookie
  215. * @param cname 名称
  216. * @param cvalue 值
  217. * @param seconds 有效期
  218. */
  219. setCookie: function (cname, cvalue, seconds) {
  220. var d = new Date();
  221. d.setTime(d.getTime() + (seconds * 1000));
  222. var expires = "expires=" + d.toGMTString();
  223. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  224. },
  225. /**
  226. * getCookie 获取cookie
  227. * @param cname 名称
  228. */
  229. getCookie: function (cname) {
  230. var name = cname + "=";
  231. var ca = document.cookie.split(';');
  232. for (var i = 0; i < ca.length; i++) {
  233. var c = ca[i];
  234. while (c.charAt(0) == ' ') c = c.substring(1);
  235. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  236. }
  237. return "";
  238. },
  239. /**
  240. * removeCookie 删除cookie
  241. * @param cname 名称
  242. */
  243. removeCookie: function (cname) {
  244. var exp = new Date();
  245. exp.setTime(exp.getTime() - 1);
  246. document.cookie = cname + "=; expires=" + exp.toGMTString();
  247. },
  248. /**
  249. * getParam 获取url参数
  250. * @param key 当前url链接
  251. */
  252. getParam: function (key) {
  253. var json = {}, data;
  254. $.each(location.search.substr(1).split("&"), function (i, n) {
  255. data = n.split("=");
  256. json[data[0]] = data[1];
  257. });
  258. return key != undefined ? json[key] : json;
  259. },
  260. /**
  261. * getParam 判断对象是否有内容
  262. * @param obj 传入对象
  263. */
  264. isEmptyObject: function (obj) {
  265. for (var key in obj) {
  266. return false;
  267. }
  268. return true;
  269. },
  270. /**
  271. * skip 页面跳转
  272. * @param url 跳转页面链接
  273. */
  274. skip: function (url) {
  275. if (url != '') {
  276. window.location = url
  277. }
  278. }
  279. });
  280. return Tool;
  281. });