Skip to content

Instantly share code, notes, and snippets.

@qubaomingg
Last active May 28, 2017 07:58
Show Gist options
  • Save qubaomingg/10160347 to your computer and use it in GitHub Desktop.
Save qubaomingg/10160347 to your computer and use it in GitHub Desktop.
一些工具函数
var util = {
loadCss: function(src) {
var style = document.createElement('link');
style.href = src;
style.rel = 'stylesheet';
style.type = 'text/css';
document.getElementsByTagName('head').item(0).appendChild(style);
},
loadJs: function(src, callback) {
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= src;
script.async = true;
if (callback != null) {
script.onload = script.onreadystatechange = function(){
if (script.ready) {
return false;
}
if (!script.readyState || script.readyState == "loaded" || script.readyState == 'complete') {
script.ready = true;
callback();
}
};
}
head.appendChild(script);
},
getCookie: function(name){
var strCookie=document.cookie;
var arrCookie=strCookie.split("; ");
for(var i=0;i<arrCookie.length;i++){
var arr=arrCookie[i].split("=");
if(arr[0]==name)return arr[1];
}
return "";
},
deleteCookie: function (name){
var date=new Date();
date.setTime(date.getTime()-10000);
document.cookie=name+"=v; expire="+date.toGMTString();
},
mergeJson: function(oldJson, newJson) {
// 合并两个json对象,对比old和new,如果不一样,则用new的。new没有的用new。最后返回old样式old json
for(var name in oldJson) {
if(!newJson[name]) {
newJson[name] = oldJson[name];
}
}
return newJson;
},
scrollBottomMore: function() {
$(window.document).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height() - 90) {
// doSomething();
}
});
}
};
/**
* 在本地进行文件保存
* @param {String} data 要保存到本地的图片数据
* @param {String} filename 文件名
* @param callback{Function}
*/
export function saveFile(filename, callback){
var save_link = document.createElement('a');
save_link.href = '/express/' + filename
save_link.download = filename.replace('exports/', '')
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
callback && callback();
}
export function downloadHttp(config = {
url: '',
param: null,
callback: null
}, httpType = 'GET') {
let url = config.url
let fetchConfig = {
method: httpType,
credentials: 'same-origin'
}
if(httpType === 'GET') {
if(config.param) {
url = config.url + '?' + qs.stringify(config.param, {
arrayFormat: 'brackets'
})
}
} else if(httpType === 'POST' || httpType === 'PUT' || httpType === 'PATCH') {
fetchConfig.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
if(config.param) {
fetchConfig.body = JSON.stringify(config.param)
}
}
fetch(url, fetchConfig)
.then(res => {
// node返回二进制数据
return res.blob()
})
.then(blob => {
var a = document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.download = config.param && config.param.title || 'NoName.txt';
a.click()
config.callback && config.callback(blob)
})
.catch((errors) => {
console.error(errors)
config.error && config.error(errors)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment