做项目是遇到一个封装的不错的Util类,写一下,长期备用:
CMD标准
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
define(function(require, exports, module) { "use strict"; function Util() {} (function() { Util.isWindow = function(obj) { return obj !== null && obj === obj.window; }; Util.isFunction = function(obj) { return typeof obj === 'function'; }; Util.isObject = function(obj) { return typeof obj === 'object'; }; Util.isArray = function(obj) { return obj instanceof Array; }; Util.isPlainObject = function(obj) { return Util.isObject(obj) && !Util.isWindow(obj); }; Util.isString = function(obj) { return typeof obj === 'string'; }; // 将其他的object统统搞到一个object中去 // 这个函数不如直接写两个参数看着舒服 target objects,就不用各种slice shift了 Util.extend = function(target) { var deep; var args = [].slice.call(arguments, 1); if (typeof target === 'boolean') { deep = target; target = args.shift(); } args.forEach(function(arg) { extend(target, arg, deep); }); return target; // 这个函数就是把source里面的一层一层往下展开给target,object 和 array会一直展开 function extend(target, source, deep) { for (var key in source) { if (deep && (Util.isPlainObject(source[key]) || Array.isArray(source[key]))) { if (Util.isPlainObject(source[key]) && !Util.isPlainObject(source[key])) { target[key] = {}; } if (Array.isArray(source[key]) && !Array.isArray(target[key])) { target[key] = []; } extend(target[key], source[key], deep); } else { target[key] = source[key]; } } } }; Util.ajaxGet = function(url, data, callback) { // 简单学习一下XMLHttpRequest,一个JS对象,提供了封装的获得url上资源数据的方法,支持xml http ftp file // 步骤简单: open(method, url, sync), send(), 如果是异步的话 就调用onreadystatechange方法 var xhr = new XMLHttpRequest(); // 设置超时时间为30s xhr.timeout = 30000; xhr.onreadystatechange = function() { console.log("xhr.status is " + xhr.readyState); if (xhr.readyState === 4) { var status = 0; try { status = xhr.status; } catch(e) { // eat and ignore the exception, since access the status of XHR might cause a exception after timeout occurs; return; } if (status === 200) { var result = null; try { result = JSON.parse(xhr.responseText.replace(/\n|\r|\t|\b|\f/g, '')); } catch(e) { callback(null); return; } callback(result); } else { callback(null); } xhr.onreadystatechange = null; xhr = null; } }; xhr.withCredentials = true; xhr.open("GET", url + "?" + data, true); xhr.send(null); return xhr; }; Util.ajaxPost = function(url, data, callback) { var xhr = new XMLHttpRequest(); // 原来navigator除了online,还有个connection // 这个connection,而且这个connection很刁,何以拿到type,还有change事件 // 关键是一大堆浏览器都TM的不支持 var connection = navigator.connection; switch(connection.type) { case connection.WIFI : case connection.ETHERNET: xhr.timeout = 3000; break; case connection.CELL_3G: case connection.CELL_4G: xhr.timeout = 5000; break; case connection.CELL_2G: xhr.timeout = 30000; break; default : xhr.timeout = 30000; } // POST比GET复杂,首先不同网络timeout不一样,其次有很多事件需要监听 xhr.addEventListener("error", function(e) { if (e && e.loaded === 60) { // https error if (e.total === 9) { callback("CERTIFICATE TIME ERROR"); } else { callback("CERTIFICATE ERROR"); } } else { callback("NETWORK_ERROR"); } }); xhr.addEventListener("abort", function() { console.log("request abort!"); }); xhr.addEventListener("timeout", function() { callback("TIMEOUT"); }); xhr.addEventListener("readystatechange", function() { if (xhr.readyState === 4) { if (xhr.status === 200) { var result; try { result = JSON.parse(xhr.responseText.replace(/\n|\r|\t|\b|\f/g, '')); } catch(e) { callback("DATA_INVALID"); return; } callback(result); } else if (xhr.status !== 0) { callback("HTTP_ERROR"); } } }); // withCredentials = true 将使得请求会带上cookie xhr.withCredentials = true; xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.send(data); return xhr; }; Util.abort = function(xhr) { xhr.abort; }; Util.getGeoLocation = function(callback) { var timeout = 30000; if (navigator.connection && navigator.connection.type) { switch (navigator.connection.type) { // wifi case 2: timeout = 8000; break; // 2G case 3: timeout = 30000; break; // 3g: case 4: timeout = 20000; break; default: timeout = 30000; break; } } // 这里就是浏览器获取地理位置的用法 navigator.geolocation.getCurrentPosition( function(position) { callback.call(this, position); }.bind(this), function(error) { callback.call(this, null); }.bind(this), { enableHighAccuracy: true, maximumAge: 180000, timeout: timeout }); }; // 实时定位, 返回一个ID,用来取消监听 Util.startWatchGeoLocation = function(callback) { return navigator.geolocation.watchPosition( function(position) { callback.call(this, position); }.bind(this), function() { callback.call(this, null); }.bind(this), { maximumAge: 600000, timeout: 20000 } ); }; Util.stopWatchGeoLocation = function(geoId) { navigator.geolocation.clearWatch(geoId); }; // 主要用来 ajax request中把object转为字符串 Util.paramToStr = function(params) { var data = ''; var isFirstChar = true; for (var key in params) { if (params.hasOwnProperty(key)) { if (isFirstChar === true) { isFirstChar = false; data += key + "=" + params[key]; } else { data += "&" + key + "=" + params[key]; } } } return data; }; // 预加载一个Image Util.preloadImage = function(images) { if (images && images.length) { for(var i = 0; i < images.length; i++) { var imgContainer = new Image(); imgContainer.src = images[i]; imgContainer = null; } } }; // 就是把 rgb(,,)换成了rgba(,,,) Util.addOpacity = function(color, opacity) { var newColor = color.replace(/rgb/g, "rgba").replace(/\)/g, "," + opacity + ")"); return newColor; }; Util.isOffline = function() { return !navigator.onLine; }; Util.getStorageValue = function(key) { var item = window.localStorage.getItem(key); if (!item) { return; } item = JSON.parse(item); var data = item.data; var type = item.type; var value = null; switch(type) { case "Boolean": value = Boolean(data); break; case "String": value = String(data); break; case "Number": value = Number(data); break; case "JSON": value = JSON.parse(data); break; } return value; }; Util.setStorageValue = function(key, value) { var type = null; var data = value; if (typeof value === "boolean") { type = "Boolean"; } else if (typeof value === "string") { type = "String"; } else if (typeof value === "number") { type = "Number"; } else if (typeof value === "object") { type = "JSON"; data = JSON.stringify(value); } window.localStorage.setItem(key, JSON.stringify({data: data, type: type})); } }).(); module.exports = Util; }); |