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

tools.js 6.0 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年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 noHead(Boolean) 是否需要Bearer
  15. */
  16. doPost: function (url, data, cb, noHead) {
  17. var _this = this;
  18. var headAttribute = '';
  19. if(noHead && noHead==true){
  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
  60. */
  61. doGet: function (uri, data, cb , noHead) {
  62. var _this = this;
  63. var url = ajaxJsUrl+uri + '?';
  64. var headAttribute = ''
  65. if(noHead && noHead==true){
  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. });
  104. },
  105. /**
  106. * 创建dialog
  107. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  108. * @param title (String) dialog的title
  109. * @param content(String || HTMLElement) dialog的内容
  110. * @param ok (Function) 点击确定按钮的回调函数
  111. * @param cancel(Function) 点击取消按钮的回调函数
  112. * 可以通过showModal来实现modal显示
  113. */
  114. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  115. var initDialog = dialog({
  116. id: id ? id : '',
  117. title: title,
  118. content: content,
  119. okValue: okValue ? okValue : '确定',
  120. ok: ok,
  121. cancelValue: cancelValue ? cancelValue : '取消',
  122. cancel: cancel,
  123. padding: 40,
  124. top: '150px'
  125. });
  126. initDialog.show();
  127. },
  128. /**
  129. * 创建error dialog
  130. * @param errContent
  131. */
  132. initError: function (errContent) {
  133. var error = dialog({
  134. content: errContent,
  135. padding: 20
  136. });
  137. error.show();
  138. var time = setTimeout(function () {
  139. error.remove();
  140. }, 1500);
  141. },
  142. /**
  143. * 创建dialog tips
  144. * @param tips
  145. * @param align
  146. */
  147. initTips: function (tips, align, $target, times) {
  148. var tips = dialog({
  149. content: tips,
  150. padding: 20,
  151. skin: 'min-dialog tips',
  152. quickClose: true,
  153. align: align
  154. });
  155. tips.show($target);
  156. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  157. times = 2000
  158. } else {
  159. times = times
  160. }
  161. setTimeout(function () {
  162. tips.close().remove();
  163. }, times);
  164. },
  165. /**
  166. * setCookie 设置cookie
  167. * @param cname 名称
  168. * @param cvalue 值
  169. * @param seconds 有效期
  170. */
  171. setCookie: function (cname, cvalue, seconds) {
  172. var d = new Date();
  173. d.setTime(d.getTime() + (seconds * 1000));
  174. var expires = "expires=" + d.toGMTString();
  175. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  176. },
  177. /**
  178. * getCookie 获取cookie
  179. * @param cname 名称
  180. */
  181. getCookie: function (cname) {
  182. var name = cname + "=";
  183. var ca = document.cookie.split(';');
  184. for (var i = 0; i < ca.length; i++) {
  185. var c = ca[i];
  186. while (c.charAt(0) == ' ') c = c.substring(1);
  187. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  188. }
  189. return "";
  190. },
  191. /**
  192. * removeCookie 删除cookie
  193. * @param cname 名称
  194. */
  195. removeCookie: function (cname) {
  196. var exp = new Date();
  197. exp.setTime(exp.getTime() - 1);
  198. document.cookie = cname + "=; expires=" + exp.toGMTString();
  199. },
  200. /**
  201. * getParam 获取url参数
  202. * @param key 当前url链接
  203. */
  204. getParam: function (key) {
  205. var json = {}, data;
  206. $.each(location.search.substr(1).split("&"), function (i, n) {
  207. data = n.split("=");
  208. json[data[0]] = data[1];
  209. });
  210. return key != undefined ? json[key] : json;
  211. },
  212. /**
  213. * getParam 判断对象是否有内容
  214. * @param obj 传入对象
  215. */
  216. isEmptyObject: function (obj) {
  217. for (var key in obj) {
  218. return false;
  219. }
  220. return true;
  221. },
  222. /**
  223. * skip 页面跳转
  224. * @param url 跳转页面链接
  225. */
  226. skip:function(url){
  227. if(url!=''){
  228. window.location = url
  229. }
  230. }
  231. });
  232. return Tool;
  233. });