码迷,mamicode.com
首页 > 其他好文 > 详细

翻看本次项目看同事负责部分代码 整理了一下

时间:2017-12-04 17:34:01      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:[1]   date()   ret   time   路由   xpl   bre   else   instance   

这次项目是vue的路由用的是nuxt比较官方相对来说。

以前项目都是 写完了没有总结的好习惯  偶的空闲 整理一波 
也利于自己以后项目开发进度。

1 获取登录浏览器的类型

此函数可区分ie5.5~ie8、Chrome、Opera、Safair、Firefox这5种常见浏览器。 

function browser (){ 
var ua=window.navigator.userAgent, 
ret=""; 
if(/Firefox/g.test(ua)){ 
ua=ua.split(" "); 
ret="Firefox|"+ua[ua.length-1].split("/")[1]; 
}else if(/MSIE/g.test(ua)){ 
ua=ua.split(";"); 
ret="IE|"+ua[1].split(" ")[2]; 
}else if(/Opera/g.test(ua)){ 
ua=ua.split(" "); 
ret="Opera|"+ua[ua.length-1].split("/")[1]; 
}else if(/Chrome/g.test(ua)){ 
ua=ua.split(" "); 
ret="Chrome|"+ua[ua.length-2].split("/")[1]; 
}else if(/^apple\s+/i.test(navigator.vendor)){ 
ua=ua.split(" "); 
ret="Safair|"+ua[ua.length-2].split("/")[1]; 
}else{ 
ret="未知浏览器"; 

return ret.split("|"); 

//调用函数,返回一个数组,r[0]是浏览器名称,r[1]是版本号 
var r=browser(); 
alert(r); 

 

2。token cookie  date 

export const setToken = (name, data) => {
if (process.SERVER_BUILD) return;
window.localStorage.setItem(name, data);
Cookie.set(name, data)
};
 
export const unsetToken = (name) => {
if (process.SERVER_BUILD) return;
window.localStorage.removeItem(name);
Cookie.remove(name);
window.localStorage.setItem(‘logout‘, Date.now())
};
export const getCookie = (name) => {
if (process.SERVER_BUILD) return;
return Cookie.get(name);
};

export const getTime = (option) => {
var date = new Date();
var Year = date.getFullYear();
var Month = date.getMonth() + 1;
var Day = date.getDate();
switch (option) {
case 0:
return Year;
break;
case 1:
return Month;
break;
case 2:
return Day;
break;
}
};
export const getDate = (date, boolean) => {
var Year = date.getFullYear();
var Month = (date.getMonth() + 1 < 10 ? ‘0‘ + (date.getMonth() + 1) : date.getMonth() + 1);
var Day = date.getDate() < 10 ? ‘0‘ + date.getDate() : date.getDate();
if (boolean == true) {
var Hour = date.getHours() < 10 ? ‘0‘ + date.getHours() : date.getHours();
var Minute = date.getMinutes() < 10 ? ‘0‘ + date.getMinutes() : date.getMinutes();
var Second = date.getSeconds() < 10 ? ‘0‘ + date.getSeconds() : date.getSeconds();
return (Year + ‘-‘ + Month + ‘-‘ + Day + ‘ ‘ + Hour + ‘:‘ + Minute + ‘:‘ + Second)
} else {
return (Year + ‘-‘ + Month + ‘-‘ + Day)
}

};

/*根据日期字符串,返回Date对象,兼容safari*/
export const returnDate = (dateTimeString) => {
if (typeof(dateTimeString) != "string") {
return new Date(dateTimeString)
}
var datetime;
if (isNaN(Date.parse(dateTimeString))) {
datetime = new Date(Date.parse(dateTimeString.replace(/-/g, ‘/‘).replace(/T/g, ‘‘)));
} else {
datetime = new Date(Date.parse(dateTimeString))
}
return datetime
};

export const getYesterday = (time) => {
var date = time || new Date(),
timestamp,
newDate;
if (!(date instanceof Date)) {
date = new Date(date.replace(/-/g, ‘/‘));
}
timestamp = date.getTime();
newDate = new Date(timestamp - 1 * 24 * 3600 * 1000);
return [newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate()].join(‘-‘);
};

export const getSevenDaysBefore = (time) => {
var date = time || new Date(),
timestamp,
newDate;
if (!(date instanceof Date)) {
date = new Date(date.replace(/-/g, ‘/‘));
}
timestamp = date.getTime();
newDate = new Date(timestamp - 7 * 24 * 3600 * 1000);
return [newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate()].join(‘-‘);
}

export const browserType = (from) => {
var explorer = from.toLowerCase();
//ie
var isIe8 = false;
if (explorer.indexOf("msie") >= 0) {
var ver = explorer.match(/msie ([\d.]+)/)[1];
if (ver <= ‘9.0‘) {
isIe8 = true;
}
}
return isIe8;
};

3  正则表达 

export const validTel = (phone) => {
var pattern = /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
if (pattern.test(phone)) {
return true;
} else {
return false;
}
};
export const validFixedTel = (tel) => {
var reg = /^0\d{2,3}-\d{7,8}(-\d{1,6})?$/;
if (reg.test(tel)) {
return true;
} else {
return false;
}
}
 
export const validEmail = (email) => {
var pattern = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/;
if (pattern.test(email)) {
return true;
} else {
return false;
}
};
export const checkQQ = (qq) => {
var reQQ = /^[1-9]\d{4,9}$/;
if (!reQQ.test(qq)) {
return false;
} else {
return true;
}
}
 
export const checkeurl = (url) => {
var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*‘().&=+$%-]+: )?[0-9a-z_!~*‘().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*‘()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn‘t required if there is no file name
+ "(/[0-9a-z_!~*‘().;?:@&=+$,%#-]+)+/?)$";
var re = new RegExp(strRegex);
if (re.test(url)) {
return (true);
} else {
return (false);
}
};
 
4
//数组去重
export const uniqueArr = (array) => {
var r = [];
for (var i = 0, l = array.length; i < l; i++) {
for (var j = i + 1; j < l; j++)
if (array[i] === array[j]) j = ++i;
r.push(array[i]);
}
return r;
}
 

翻看本次项目看同事负责部分代码 整理了一下

标签:[1]   date()   ret   time   路由   xpl   bre   else   instance   

原文地址:http://www.cnblogs.com/honeywei/p/7977921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!