TD.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var TD = {};
  2. TD.ajax = function (pm, succback, errorback) {
  3. // $.ajax({
  4. // type: pm.type || 'GET',
  5. // url: pm.url,
  6. // dataType: 'json',
  7. // data: pm.data || '',
  8. // success: function (data) {
  9. // if (data.status === 1) {
  10. // succback && succback(data.data);
  11. // } else {
  12. // errorback && errorback(data.message);
  13. // }
  14. // },
  15. // error: function () {
  16. // errorback && errorback('网络连接不稳定,请重试或刷新页面!');
  17. // }
  18. // });
  19. // ajax({
  20. // url: "./TestXHR.aspx", //请求地址
  21. // type: "POST", //请求方式
  22. // data: { name: "super", age: 20 }, //请求参数
  23. // dataType: "json",
  24. // success: function (response, xml) {
  25. // // 此处放成功后执行的代码
  26. // },
  27. // fail: function (status) {
  28. // // 此处放失败后执行的代码
  29. // }
  30. // });
  31. function ajax (options) {
  32. options = options || {};
  33. options.type = (options.type || 'GET').toUpperCase();
  34. options.dataType = options.dataType || 'json';
  35. var params = formatParams(options.data);
  36. var xhr = null;
  37. // 创建 - 非IE6 - 第一步
  38. if (window.XMLHttpRequest) {
  39. xhr = new XMLHttpRequest();
  40. } else { // IE6及其以下版本浏览器
  41. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  42. }
  43. // 接收 - 第三步
  44. xhr.onreadystatechange = function () {
  45. if (xhr.readyState === 4) {
  46. var status = xhr.status;
  47. if (status >= 200 && status < 300) {
  48. options.success && options.success(xhr.responseText, xhr.responseXML);
  49. } else {
  50. options.fail && options.fail(status);
  51. }
  52. }
  53. };
  54. // 连接 和 发送 - 第二步
  55. if (options.type === 'GET') {
  56. xhr.open('GET', options.url + '?' + params, true);
  57. xhr.send(null);
  58. } else if (options.type === 'POST') {
  59. xhr.open('POST', options.url, true);
  60. // 设置表单提交时的内容类型
  61. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  62. xhr.send(params);
  63. }
  64. }
  65. // 格式化参数
  66. function formatParams (data) {
  67. var arr = [];
  68. for (var name in data) {
  69. arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
  70. }
  71. arr.push(('v=' + Math.random()).replace('.', ''));
  72. return arr.join('&');
  73. }
  74. ajax({
  75. type: pm.type || 'GET',
  76. url: pm.url,
  77. dataType: 'json',
  78. data: pm.data || '',
  79. success: function (data) {
  80. if (data.status === 1) {
  81. succback && succback(data.data);
  82. } else {
  83. errorback && errorback(data.message);
  84. }
  85. },
  86. error: function () {
  87. errorback && errorback('网络连接不稳定,请重试或刷新页面!');
  88. }
  89. });
  90. };
  91. // 网络工具包
  92. TD.util = {};
  93. TD.util.getQuery = function (name) {
  94. var m = window.location.search.match(new RegExp('(\\?|&)' + name + '=([^&]*)(&|$)'));
  95. return !m ? '' : decodeURIComponent(m[2]);
  96. };
  97. TD.util.setCookie = function (name, value, expiredays) {
  98. var exdate = new Date();
  99. document.cookie = name + '=' + value + ';expires=' +
  100. ((expiredays === null) ? exdate.setDate(exdate.getDate() + expiredays) : exdate.toGMTString());
  101. };
  102. TD.util.getCookie = function (name) {
  103. var cStart, cEnd;
  104. if (document.cookie.length > 0) {
  105. cStart = document.cookie.indexOf(name + '=');
  106. if (cStart !== -1) {
  107. cStart = cStart + name.length + 1;
  108. cEnd = document.cookie.indexOf(';', cStart);
  109. if (cEnd === -1) cEnd = document.cookie.length;
  110. return unescape(document.cookie.substring(cStart, cEnd));
  111. }
  112. }
  113. return '';
  114. };
  115. TD.util.browserType = function () {
  116. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  117. var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
  118. var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
  119. var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器
  120. var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
  121. var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
  122. var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
  123. if (isIE)
  124. {
  125. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  126. reIE.test(userAgent);
  127. var fIEVersion = parseFloat(RegExp["$1"]);
  128. if(fIEVersion == 7)
  129. { return "IE7";}
  130. else if(fIEVersion == 8)
  131. { return "IE8";}
  132. else if(fIEVersion == 9)
  133. { return "IE9";}
  134. else if(fIEVersion == 10)
  135. { return "IE10";}
  136. else if(fIEVersion == 11)
  137. { return "IE11";}
  138. else
  139. { return "0"}//IE版本过低
  140. }
  141. if (isFF) { return "FF";}
  142. if (isOpera) { return "Opera";}
  143. if (isSafari) { return "Safari";}
  144. if (isChrome) { return "Chrome";}
  145. if (isEdge) { return "Edge";}
  146. }
  147. TD.imgPreload = function (callback) {
  148. // 优先加载loading图片
  149. var loadingInit = function () {
  150. var loadingStep = 0;
  151. var loadingBG = new Image();
  152. var loadingSprite = new Image();
  153. loadingBG.src = 'http://airsmart-web.oss-cn-shanghai.aliyuncs.com/upload/images/%E5%85%B1%E9%80%9A/loading_bg.jpg';
  154. loadingSprite.src = 'http://airsmart-web.oss-cn-shanghai.aliyuncs.com/upload/images/%E5%85%B1%E9%80%9A/loading_sprite2.png';
  155. loadingBG.onerror = function () {
  156. console.log('loading error');
  157. loadingMain();
  158. }
  159. loadingSprite.onerror = function () {
  160. console.log('loading error');
  161. loadingMain();
  162. }
  163. if (!loadingBG.complete) {
  164. loadingBG.onload = function () {
  165. loadingStep++;
  166. if (loadingStep === 2) {
  167. loadingMain();
  168. }
  169. }
  170. } else {
  171. loadingStep++;
  172. }
  173. if (!loadingSprite.complete) {
  174. loadingSprite.onload = function () {
  175. loadingStep++;
  176. if (loadingStep === 2) {
  177. loadingMain();
  178. }
  179. }
  180. } else {
  181. loadingStep++;
  182. }
  183. loadingBG.complete && loadingSprite.complete && loadingMain();
  184. }
  185. //获取所有img标签的url
  186. var getImgsUrl = function () {
  187. var imgsArr = [];
  188. var allImgs = $('img');
  189. for (var i = 0; i < allImgs.length; i++) {
  190. imgsArr.push(allImgs[i].src);
  191. }
  192. return imgsArr;
  193. }
  194. var loadingMain = function () {
  195. // 检查加载进度
  196. var checkLoad = function () {
  197. var progress = Math.ceil(count/urls.length*100) + '%';
  198. if (count >= urls.length) {
  199. callback && callback();
  200. }
  201. console.log(progress);
  202. console.log(count);
  203. $('.loading-text').text(progress);
  204. }
  205. var count = 0;
  206. var imgs = [];
  207. var urls = [];
  208. urls = getImgsUrl();
  209. for (var i = 0; i < urls.length; i++) {
  210. imgs[i] = new Image();
  211. imgs[i].src = urls[i];
  212. if (imgs[i].complete) {
  213. count++;
  214. checkLoad();
  215. continue;
  216. }
  217. imgs[i].onload = function () {
  218. count++;
  219. checkLoad();
  220. }
  221. imgs[i].onerror = function () {
  222. console.log('some image load failed');
  223. count++;
  224. checkLoad();
  225. }
  226. }
  227. }
  228. loadingInit();
  229. }