// 判断手机号是否正确
export function isMobile (mobile) {
let reg = /^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$/
return reg.test(mobile)
}
// 获取cookie
export function getCookie (name) {
let arr = document.cookie.replace(/\s/g, '').split(';')
for (let i = 0; i < arr.length; i++) {
let tempArr = arr[i].split('=')
if (tempArr[0] == name) {
return decodeURIComponent(tempArr[1])
}
}
return ''
}
// 设置cookie
export function setCookie (name, value, days) {
let date = new Date()
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000))
let expires = date.toUTCString()
document.cookie = name + '=' + value + ';expires=' + expires + ';path=/;domain=.baidu.com'
}
// 设置cookie
export function setCookiesecond (name, value, second) {
let date = new Date()
date.setTime(date.getTime() + ( second* 1000))
let expires = date.toUTCString()
document.cookie = name + '=' + value + ';expires=' + expires + ';path=/;domain=.baidu.com'
}
// 移除cookie
export function removeCookie (name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT'
}
// 时间格式化
export function formatDate (date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1,
(date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1,
(RegExp.$1.length === 1) ? str : padLeftZero(str))
}
}
return fmt
}
function padLeftZero (str) {
return ('00' + str).substr(str.length)
}
/**
* 格式化时间字符串,防止解析出错
* @param str
* @returns {*}
*/
export function formatDateStr (str) {
if (typeof (str) === 'string') {
return str.replace(/-/g, '/')
} else {
return 0
}
}
/**
* 判断是否为微信
* @returns {boolean}
*/
export function isWeiXin (req = '') {
let ua
if (req) {
ua = req.headers['user-agent'].toLowerCase()
} else {
ua = navigator.userAgent.toLowerCase()
}
return ua.match(/MicroMessenger/i) == 'micromessenger'
}
/**
* 判断是否是微信
*/
export function isQQ () {
var ua = navigator.userAgent.toLowerCase()
return ua.match(/QQ/i) + '' === 'qq'
}
/**
* 解析url参数
* @param param
* @returns {*}
*/
export function getUrlParam (param) {
let url = location.search // 获取url中"?"符后的字串
let theRequest = {}
if (url.indexOf('?') !== -1) {
let str = url.substr(1)
let strs = str.split('&')
for (let i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = decodeURIComponent(
strs[i].split('=')[1])
}
}
if (theRequest[param]) {
return theRequest[param]
} else {
return ''
}
}
/**
* 判断手机系统
* @returns {string}
*/
export function mobileSystem () {
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
return 'ios'
} else if (/(Android)/i.test(navigator.userAgent)) {
return 'android'
} else {
return ''
}
}
/**
* 判断是否为Pc
* @returns {boolean}
*/
export function isPc () {
return !navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
}
export function isUCBrowser() {
return navigator.userAgent.includes('UCBrowser')
}
/**
* 判断是否为蓝陌app
* @returns {boolean}
*/
export function isMosoApp () {
let ua = navigator.userAgent
if (ua.match(/mosoteach-android/i)) {
return true
} else if (ua.match(/mosoteach-ios/i)) {
return true
} else {
return false
}
}
网友评论