自用公共js文件
// 加载配置文件
var instance = axios.create({
baseURL: 'http://zy-shop.tincent.me/Wechat/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
// withCredentials:true
});
//公共函数
function Public(){
this.get=function(){
console.log(1)
},
this.post=function(){
console.log(2)
},
//顶部提示的弹框方法
this.remind=function(that,remind){
that.remindShow=true;
that.remind=remind;
setTimeout(function(){
that.remindShow=false
},1000)
},
//60秒倒计时、
this.countdown=function(that){
var time=60;
that.downTime=time+"秒后获取";
var timer=setInterval(function(){
time--;
//当时间降低成0时
if(time==0){
that.downTime="再次获取";
clearInterval(timer);
}else{
that.downTime=time+"秒后获取";
}
},1000)
}
//前后台数据传递解析
this.json2Form=function(json) {
var str = "";
for(var p in json){
// 判断对象是否为数组
if(typeof json[p]=="object"){
var m=String(p)
for(var i=0;i<json[p].length;i++){
for(var x in json[p][i]){
str+=m+"["+i+"]"+"["+x+"]"+ "=" +json[p][i][x]+"&";
}
}
}else{
str+=p + "=" + json[p]+"&";
}
}
// 截取字符串最后一位&
var length=str.length-1;
str=str.substring(0,length);
// 返回字符串str
return str;
},
//提交表单时的加锁处理
this.getCsrfToken=function(){
instance.get('Service/getCsrfToken').then(function(res){
sessionStorage.setItem('submitKey',JSON.stringify({'submitKey':res.data.data.submitKey}))
})
},
//session存储
this.saveSession=function(key,data){
sessionStorage.setItem(key,JSON.stringify(data));
},
//session提取
this.getSession=function(key,str){
//判断取值方式
var user = JSON.parse(sessionStorage.getItem(key));
if(user){
if(str){
return user[str];
}
return user;
}
},
//session清楚
this.removeSession=function(key){
sessionStorage.removeItem(key)
}
//local存储
this.saveLocal=function(key,data){
localStorage.setItem(key,JSON.stringify(data));
},
//local提取
this.getLocal=function(key,str){
//判断取值方式
var user = JSON.parse(localStorage.getItem(key));
if(user){
if(str){
return user[str];
}
return user;
}
},
//清除local
this.removeLocal=function(key){
localStorage.removeItem(key)
},
//获取URL传递的参数
this.getUrlParam=function(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
},
//页面lodding函数
this.loadding=function(type){
//判断是否是启动loadding
if (type=='load') {
toast.loading({
title:"加载中",
duration:2000
},function(ret){
});
} else if(type='hide'){
setTimeout(function(){
toast.hide();
},500)
}
},
//定义加载中时的文字显示
this.pageWord=function(that,pageflag,title){
if (pageflag==1) {
that.pageWord=title;
}else{
that.pageWord="暂无更多"
}
}
}
var toast = new auiToast();
var public=new Public();