123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- const browser = () => {
- var u = navigator.userAgent
- // app = navigator.appVersion;
- return {
- trident: u.indexOf('Trident') > -1, //IE内核
- presto: u.indexOf('Presto') > -1, //opera内核
- webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
- gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
- mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
- ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
- // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
- android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
- iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器
- iPad: u.indexOf('iPad') > -1, //是否iPad
- webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
- weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
- qq: u.match(/\sQQ/i) == " qq" //是否QQ
- }
- }
- /**
- * 小于10的数变成 (0x)
- * @param {数值} num
- */
- const getSTD = (num) => {
- return Number(num) >= 10 ? num : '0' + num
- }
- /**
- * 获取格式化的年月日
- * @param {日期} time
- * @param {不是IOS里使用(多用于接口参数)} noIos
- */
- const getYMD = (time, noIos) => {
- let tempDate = time || new Date()
- if (typeof (tempDate) == 'string') {
- tempDate = new Date(time.replace(/-/ig, '/'))
- }
- let t = noIos ? '-' : '/'
- let month = getSTD(tempDate.getMonth() + 1)
- let day = getSTD(tempDate.getDate())
- return tempDate.getFullYear() + t + month + t + day
- }
- /**
- *
- * @param {周几的索引值} index
- */
- const getWeekString = (index) => {
- let weekText = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
- return weekText[index]
- }
- /**
- * 计算课程的结束时间
- * @param {课程组开始时间} startDate
- * @param {购买几个月时间} buyMonths
- */
- const courseExpireDateArray = (startDate, buyMonths) => {
- let resultDate = new Date(startDate)
- let lastDayNum = nextMonthLastDay(resultDate.getFullYear(), (resultDate.getMonth() + 1 + buyMonths))
- if (lastDayNum[2] >= resultDate.getDate()) {
- resultDate.setMonth(resultDate.getMonth() + buyMonths)
- } else {
- resultDate = new Date(lastDayNum.join('/'))
- }
- resultDate.setDate(resultDate.getDate() - 1)
- return resultDate
- }
- const nextMonthLastDay = (year, month) => { //日期显示为次月最后一天
- if (month > 12) {
- month = month - 12;
- year = year + 1;
- }
- var day = nextMonthDay(year, month);
- return [year, month, day];
- }
- const nextMonthDay = (year, month) => { //判断每月多少天
- var day31 = [1, 3, 5, 7, 8, 10, 12];
- var day30 = [4, 6, 9, 11];
- if (day31.indexOf(month) > -1) {
- return 31;
- } else if (day30.indexOf(month) > -1) {
- return 30;
- } else {
- if (isLeapYear(year)) {
- return 29;
- } else {
- return 28;
- }
- }
- }
- /**
- * 判断是否为闰年
- */
- const isLeapYear = (year) => {
- return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
- }
- /**
- * 检测是否为手机号
- * @param {手机号} phoneNumber
- */
- const checkPhone = (phoneNumber) => {
- let result = true
- if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phoneNumber))) {
- result = false
- }
- return result
- }
- export {
- browser,
- getSTD,
- getYMD,
- getWeekString,
- nextMonthLastDay,
- nextMonthDay,
- isLeapYear,
- courseExpireDateArray,
- checkPhone
- }
|