网站
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

241 строка
6.2 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('/user/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 get
  56. * @param url(String)
  57. * @param data(Json) 需要提交的数据
  58. * @param cb(Function) 回调函数
  59. * @param noHead(Boolean) 是否需要Bearer,不需要传true
  60. */
  61. doGet: function (uri, data, cb , Bearer) {
  62. var _this = this;
  63. var url = ajaxJsUrl+uri + '?';
  64. var headAttribute = ''
  65. if(Bearer && Bearer==true || _this.getCookie('Admin-Token')==''){
  66. headAttribute = function (xhr) {
  67. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  68. }
  69. }else{
  70. headAttribute = function (xhr) {
  71. xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  72. xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token'))
  73. }
  74. }
  75. if (data) {
  76. url += $.map(data, function (n, i) {
  77. if (i == data.length - 1) {
  78. return i + '=' + n;
  79. } else {
  80. return i + '=' + n + '&';
  81. }
  82. }).join('');
  83. }
  84. $.ajax({
  85. url: url,
  86. type: 'GET',
  87. beforeSend:headAttribute,
  88. success: function (data) {
  89. var code = data.code;
  90. var msg = data.msg;
  91. if(code === 401){
  92. _this.initDialog('系统提示','登录状态已过期,您可以继续留在该页面,或者重新登录',function () {
  93. _this.skip('/user/login.html')
  94. },'重新登录',function () {},"取消")
  95. }else if(code === 500){
  96. _this.initError(msg)
  97. }else if(code !=200){
  98. _this.initError(msg)
  99. }else{
  100. cb(data);
  101. }
  102. },
  103. error:function(data){
  104. console.log(data)
  105. }
  106. });
  107. },
  108. /**
  109. * 创建dialog
  110. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  111. * @param title (String) dialog的title
  112. * @param content(String || HTMLElement) dialog的内容
  113. * @param ok (Function) 点击确定按钮的回调函数
  114. * @param cancel(Function) 点击取消按钮的回调函数
  115. * 可以通过showModal来实现modal显示
  116. */
  117. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  118. var initDialog = dialog({
  119. id: id ? id : '',
  120. title: title,
  121. content: content,
  122. okValue: okValue ? okValue : '确定',
  123. ok: ok,
  124. cancelValue: cancelValue ? cancelValue : '取消',
  125. cancel: cancel,
  126. padding: 40,
  127. top: '150px'
  128. });
  129. initDialog.show();
  130. },
  131. /**
  132. * 创建error dialog
  133. * @param errContent
  134. */
  135. initError: function (errContent) {
  136. var error = dialog({
  137. content: errContent,
  138. padding: 20
  139. });
  140. error.show();
  141. var time = setTimeout(function () {
  142. error.remove();
  143. }, 1500);
  144. },
  145. /**
  146. * 创建dialog tips
  147. * @param tips
  148. * @param align
  149. */
  150. initTips: function (tips, align, $target, times) {
  151. var tips = dialog({
  152. content: tips,
  153. padding: 20,
  154. skin: 'min-dialog tips',
  155. quickClose: true,
  156. align: align
  157. });
  158. tips.show($target);
  159. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  160. times = 2000
  161. } else {
  162. times = times
  163. }
  164. setTimeout(function () {
  165. tips.close().remove();
  166. }, times);
  167. },
  168. /**
  169. * setCookie 设置cookie
  170. * @param cname 名称
  171. * @param cvalue 值
  172. * @param seconds 有效期
  173. */
  174. setCookie: function (cname, cvalue, seconds) {
  175. var d = new Date();
  176. d.setTime(d.getTime() + (seconds * 1000));
  177. var expires = "expires=" + d.toGMTString();
  178. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  179. },
  180. /**
  181. * getCookie 获取cookie
  182. * @param cname 名称
  183. */
  184. getCookie: function (cname) {
  185. var name = cname + "=";
  186. var ca = document.cookie.split(';');
  187. for (var i = 0; i < ca.length; i++) {
  188. var c = ca[i];
  189. while (c.charAt(0) == ' ') c = c.substring(1);
  190. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  191. }
  192. return "";
  193. },
  194. /**
  195. * removeCookie 删除cookie
  196. * @param cname 名称
  197. */
  198. removeCookie: function (cname) {
  199. var exp = new Date();
  200. exp.setTime(exp.getTime() - 1);
  201. document.cookie = cname + "=; expires=" + exp.toGMTString();
  202. },
  203. /**
  204. * getParam 获取url参数
  205. * @param key 当前url链接
  206. */
  207. getParam: function (key) {
  208. var json = {}, data;
  209. $.each(location.search.substr(1).split("&"), function (i, n) {
  210. data = n.split("=");
  211. json[data[0]] = data[1];
  212. });
  213. return key != undefined ? json[key] : json;
  214. },
  215. /**
  216. * getParam 判断对象是否有内容
  217. * @param obj 传入对象
  218. */
  219. isEmptyObject: function (obj) {
  220. for (var key in obj) {
  221. return false;
  222. }
  223. return true;
  224. },
  225. /**
  226. * skip 页面跳转
  227. * @param url 跳转页面链接
  228. */
  229. skip:function(url){
  230. if(url!=''){
  231. window.location = url
  232. }
  233. }
  234. });
  235. return Tool;
  236. });