网站
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tools.js 4.9 KiB

há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. define(['jquery', 'dialog'], function ($, dialog) {
  2. // 工具类
  3. function Tool() {
  4. this.version = "1.0.0";
  5. this.description = "这是一个工具类";
  6. }
  7. $.extend(Tool.prototype, {
  8. /**
  9. * ajax post
  10. * @param url (String)
  11. * @param data (Json) 需要提交的数据
  12. * @param cb(Function) 回调函数
  13. */
  14. doPost: function (url, data, cb) {
  15. var _this = this;
  16. $.ajax({
  17. url: url + '?=' + Math.random(),
  18. type: 'POST',
  19. data: data,
  20. beforeSend: function (xhr) {
  21. xhr.setRequestHeader("timestamp", getNowFormatDate());
  22. xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
  23. xhr.setRequestHeader("version", '1.1.6');
  24. },
  25. success: function (data) {
  26. cb(data);
  27. }
  28. });
  29. },
  30. doGetJsonp: function (url, data, cb) {
  31. var _this = this;
  32. $.ajax({
  33. url: url,
  34. dataType: "jsonp",
  35. data: data,
  36. beforeSend: function (xhr) {
  37. xhr.setRequestHeader("from", "weixin");
  38. xhr.setRequestHeader("timestamp", getNowFormatDate());
  39. xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
  40. xhr.setRequestHeader("version", '1.1.6');
  41. },
  42. jsonp: "jsonpcallback",
  43. success: function (data) {
  44. cb(data);
  45. }
  46. });
  47. },
  48. /**
  49. * ajax get
  50. * @param url(String)
  51. * @param data(Json) 需要提交的数据
  52. * @param cb(Function) 回调函数
  53. */
  54. doGet: function (uri, data, cb) {
  55. var _this = this;
  56. var url = uri + '?';
  57. if (data) {
  58. url += $.map(data, function (n, i) {
  59. if (i == data.length - 1) {
  60. return i + '=' + n;
  61. } else {
  62. return i + '=' + n + '&';
  63. }
  64. }).join('');
  65. }
  66. $.ajax({
  67. url: url,
  68. type: 'GET',
  69. beforeSend: function (xhr) {
  70. xhr.setRequestHeader("from", "weixin");
  71. xhr.setRequestHeader("timestamp", getNowFormatDate());
  72. xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
  73. xhr.setRequestHeader("version", '1.1.6');
  74. },
  75. success: function (data) {
  76. cb(data);
  77. }
  78. });
  79. },
  80. /**
  81. * 创建dialog
  82. * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
  83. * @param title (String) dialog的title
  84. * @param content(String || HTMLElement) dialog的内容
  85. * @param ok (Function) 点击确定按钮的回调函数
  86. * @param cancel(Function) 点击取消按钮的回调函数
  87. * 可以通过showModal来实现modal显示
  88. */
  89. initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
  90. var initDialog = dialog({
  91. id: id ? id : '',
  92. title: title,
  93. content: content,
  94. okValue: okValue ? okValue : '确定',
  95. ok: ok,
  96. cancelValue: cancelValue ? cancelValue : '取消',
  97. cancel: cancel,
  98. padding: 40,
  99. top: '150px'
  100. });
  101. initDialog.show();
  102. },
  103. /**
  104. * 创建error dialog
  105. * @param errContent
  106. */
  107. initError: function (errContent) {
  108. var error = dialog({
  109. content: errContent,
  110. padding: 20
  111. });
  112. error.show();
  113. var time = setTimeout(function () {
  114. error.remove();
  115. }, 1500);
  116. },
  117. /**
  118. * 创建dialog tips
  119. * @param tips
  120. * @param align
  121. */
  122. initTips: function (tips, align, $target, times) {
  123. var tips = dialog({
  124. content: tips,
  125. padding: 20,
  126. skin: 'min-dialog tips',
  127. quickClose: true,
  128. align: align
  129. });
  130. tips.show($target);
  131. if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
  132. times = 2000
  133. } else {
  134. times = times
  135. }
  136. setTimeout(function () {
  137. tips.close().remove();
  138. }, times);
  139. },
  140. /**
  141. * setCookie 设置cookie
  142. * @param cname 名称
  143. * @param cvalue 值
  144. * @param seconds 有效期
  145. */
  146. setCookie: function (cname, cvalue, seconds) {
  147. var d = new Date();
  148. d.setTime(d.getTime() + (seconds * 1000));
  149. var expires = "expires=" + d.toGMTString();
  150. document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
  151. },
  152. /**
  153. * getCookie 获取cookie
  154. * @param cname 名称
  155. */
  156. getCookie: function (cname) {
  157. var name = cname + "=";
  158. var ca = document.cookie.split(';');
  159. for (var i = 0; i < ca.length; i++) {
  160. var c = ca[i];
  161. while (c.charAt(0) == ' ') c = c.substring(1);
  162. if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  163. }
  164. return "";
  165. },
  166. /**
  167. * removeCookie 删除cookie
  168. * @param cname 名称
  169. */
  170. removeCookie: function (cname) {
  171. var exp = new Date();
  172. exp.setTime(exp.getTime() - 1);
  173. document.cookie = cname + "=; expires=" + exp.toGMTString();
  174. },
  175. /**
  176. * getParam 获取url参数
  177. * @param key 当前url链接
  178. */
  179. getParam: function (key) {
  180. var json = {}, data;
  181. $.each(location.search.substr(1).split("&"), function (i, n) {
  182. data = n.split("=");
  183. json[data[0]] = data[1];
  184. });
  185. return key != undefined ? json[key] : json;
  186. },
  187. /**
  188. * getParam 判断对象是否有内容
  189. * @param obj 传入对象
  190. */
  191. isEmptyObject: function (obj) {
  192. for (var key in obj) {
  193. return false;
  194. }
  195. return true;
  196. },
  197. });
  198. return Tool;
  199. });