UNPKG

h5-cli

Version:

hello

126 lines (100 loc) 3.55 kB
"use strict"; /** lib.io.js \u6240\u6709 \u5f02\u6b65 ajax \u8bf7\u6c42\u96c6\u6563\u5730 */ define("libs/io", function (require, exports, module) { var cache = require("libs/cache"); var md5 = require("libs/md5"); // var maxExpire = 1000*60*10; //\u7f13\u5b58\u5f02\u6b65\u63a5\u53e3 10 \u5206\u949f\u7f13\u5b58 var AsynDataCache = cache.getCacheModule("AsynDataCache", 1000 * 60 * 10); var config = window.config; module.exports = { get: function get(path, param, callback, errorCallback, option) { //\u5982\u679c\u6ca1\u6709domain\u5c31\u6dfb\u52a0 path = path.indexOf("//") == -1 ? config.domainName + path : path; option = $.extend({ dataType: "jsonp", type: "GET" }, option || {}); //console.log( isSameDomain ) param = param || {}; if (!param.ver) { param.ver = "1.0"; } //\u5220\u9664\u660e\u663e\u591a\u4f59\u7684\u53c2\u6570\u4f20\u9012 if (param) { for (var key in param) { if (param[key] === undefined || param[key] === null || param[key] === "undefined") { delete param[key]; } } } //\u4e0d\u8981\u7f13\u5b58 param.t = parseInt(Math.random() * 100000); // console.log( param ) $.ajax({ url: path, //\u5bf9\u8bf7\u6c42\u8fdb\u884c\u7b80\u5355\u7684\u52a0\u5bc6 data: param, //this.md5Param(param), type: option.type, dataType: option.dataType, timeout: config.daily ? 60000 : 60000, //\u4fee\u6539\u523060\u79d2 // withCredentials:true, success: callback, error: function error(result, status, c) { console.log(result, status, c); result = result.msg || { msg: "\u7f51\u7edc\u9519\u8bef\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01" }; errorCallback(result); } }); }, //\u4ece\u7f13\u5b58\u4e2d\u62ff\u6570\u636e\uff0c\u4e5f\u4f1a\u628a\u6570\u636e\u7f13\u5b58\u8d77\u6765 getCache: function getCache(path, param, callback, errorCallback, isMultipleCallback) { var cacheKey = this.generateCacheKey(path, param); var cacheData = null; //\u5982\u679c\u6709\u7f13\u5b58\u5c31\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58 if ((cacheData = AsynDataCache.get(cacheKey)) && cacheData.value) { console.log("\u6765\u81ea\u7f13\u5b58"); callback(cacheData.value); //\u652f\u6301\u4e8c\u6b21\u56de\u8c03\u7684\u4e0d\u8981\u628acallback \u5236\u7a7a if (!isMultipleCallback) { callback = null; errorCallback = null; } } this.get(path, param, function (result) { callback && callback(result); //\u5bf9data\u672a\u7a7a\u7684\u6570\u636e\u4e0d\u505a\u7f13\u5b58 if (result.data && (result.data.length || Object.keys(result.data).length)) { AsynDataCache.set(cacheKey, result, Date.now()); } else { AsynDataCache.remove(cacheKey); } }, function (result) { try { AsynDataCache.remove(cacheKey); } catch (e) {} errorCallback && errorCallback(result); }); }, //\u52a0\u5bc6\u53c2\u6570 md5Param: function md5Param(param) { var paramList = []; //\u56fa\u5b9a\u52a0\u5bc6\u53c2\u6570 param["__sign__"] = "39a7daceb6a952257bc874f30553f8eb"; for (var key in param) { paramList.push(key + "=" + param[key]); } param.md5 = md5(paramList.sort().join("")); delete param["__sign__"]; return param; }, //\u6587\u5177\u8bf7\u6c42 api \u548c \u8bf7\u6c42\u53c2\u6570 \u5236\u4f5c\u552f\u4e00\u7684\u7f13\u5b58 key generateCacheKey: function generateCacheKey(api, param) { var key = md5(api + JSON.stringify(param || {})); return key; } }; });