| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- // 验证手机号码
- export function validatePhoneNumber(data) {
- var reg = /^1[3-9]\d{9}$/
- return reg.test(data)
- }
-
- export function lastfour(num) {
- // 截取后4位
- let lastFourDigits = num.slice(-4);
- return lastFourDigits
- }
- export function isValidBankCard(cardNumber) {
- const reg = /^(\d{16}|\d{19})$/
- // 使用正则表达式测试银行卡号
- return reg.test(cardNumber)
- }
- /**
- * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
- * @param param 当前时间,new Date() 格式或者字符串时间格式
- * @param format 需要转换的时间格式字符串
- * @description param 10秒: 10 * 1000
- * @description param 1分: 60 * 1000
- * @description param 1小时: 60 * 60 * 1000
- * @description param 24小时:60 * 60 * 24 * 1000
- * @description param 3天: 60 * 60* 24 * 1000 * 3
- * @returns 返回拼接后的时间字符串
- */
- export function formatPast(param, format = 'YYYY-mm-dd HH:MM:SS') {
- if(!param) return '从未下单'
- // 传入格式处理、存储转换值
- let t, s
- // 获取js 时间戳
- let time = new Date().getTime()
- // 是否是对象
- typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
- // 当前时间戳 - 传入时间戳
- time = Number.parseInt(`${time - t}`)
- if (time < 10000) {
- // 10秒内
- return '刚刚' + '下单'
- } else if (time < 60000 && time >= 10000) {
- // 超过10秒少于1分钟内
- s = Math.floor(time / 1000)
- return `${s}秒前` + '下单'
- } else if (time < 3600000 && time >= 60000) {
- // 超过1分钟少于1小时
- s = Math.floor(time / 60000)
- return `${s}分钟前` + '下单'
- } else if (time < 86400000 && time >= 3600000) {
- // 超过1小时少于24小时
- s = Math.floor(time / 3600000)
- return `${s}小时前` + '下单'
- } else if (time < 259200000 && time >= 86400000) {
- // 超过1天少于3天内
- s = Math.floor(time / 86400000)
- return `${s}天前` + '下单'
- } else {
- // 超过3天
- const date = typeof param === 'string' || 'object' ? new Date(param) : param
- return formatDateTime(param) + '下单'
- }
- }
- export function formatTime(time) {
- if (typeof time !== 'number' || time < 0) {
- return time
- }
- var hour = parseInt(time / 3600)
- time = time % 3600
- var minute = parseInt(time / 60)
- time = time % 60
- var second = time
- return ([hour, minute, second]).map(function(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }).join(':')
- }
- export function formatDateTime(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
- if(!date) {
- return ''
- }
- if (typeof (date) === 'number') {
- date = new Date(date)
- }
- var o = {
- "M+": date.getMonth() + 1, //月份
- "d+": date.getDate(), //日
- "h+": date.getHours(), //小时
- "m+": date.getMinutes(), //分
- "s+": date.getSeconds(), //秒
- "q+": Math.floor((date.getMonth() + 3) / 3), //季度
- "S": date.getMilliseconds() //毫秒
- }
- if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
- for (var k in o)
- if (new RegExp("(" + k + ")").test(fmt))
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
- return fmt
- }
- export function formatLocation(longitude, latitude) {
- if (typeof longitude === 'string' && typeof latitude === 'string') {
- longitude = parseFloat(longitude)
- latitude = parseFloat(latitude)
- }
- longitude = longitude.toFixed(2)
- latitude = latitude.toFixed(2)
- return {
- longitude: longitude.toString().split('.'),
- latitude: latitude.toString().split('.')
- }
- }
- var dateUtils = {
- UNITS: {
- '年': 31557600000,
- '月': 2629800000,
- '天': 86400000,
- '小时': 3600000,
- '分钟': 60000,
- '秒': 1000
- },
- humanize: function(milliseconds) {
- var humanize = '';
- for (var key in this.UNITS) {
- if (milliseconds >= this.UNITS[key]) {
- humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
- break;
- }
- }
- return humanize || '刚刚';
- },
- format: function(dateStr) {
- var date = this.parse(dateStr)
- var diff = Date.now() - date.getTime();
- if (diff < this.UNITS['天']) {
- return this.humanize(diff);
- }
- var _format = function(number) {
- return (number < 10 ? ('0' + number) : number);
- };
- return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
- _format(date.getHours()) + ':' + _format(date.getMinutes());
- },
- parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
- var a = str.split(/[^0-9]/);
- return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
- }
- };
- // 返回上一页
- export function prePage(page = null){
- let pages = getCurrentPages();
- //console.log('pages:',pages);
- let prePage = pages[pages.length - 2];
- if (page !== null) {
- prePage = pages[page];
- }
- // #ifdef H5
- return prePage;
- // #endif
- return prePage.$vm;
- }
- export function kmUnit(m){
- var v;
- if(typeof m != 'number'){
- m = Number(m)
- }
- if(typeof m === 'number' && !isNaN(m)){
- if (m >= 1000) {
- v = (m / 1000).toFixed(2) + 'km'
- } else {
- v = m + 'm'
- }
- }else{
- v = '0m'
- }
- return v;
- }
- export function isWeixin() {
- if (navigator && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1) {
- return true
- }
- return false
- }
-
- export function parseQuery() {
- let res = {}
-
- // #ifdef H5
- const query = (location.href.split('?')[1] || '').trim().replace(/^(\?|#|&)/, '')
-
- if (!query) {
- return res
- }
-
- query.split('&').forEach(param => {
- const parts = param.replace(/\+/g, ' ').split('=')
- const key = decodeURIComponent(parts.shift())
- const val = parts.length > 0 ? decodeURIComponent(parts.join('=')) : null
-
- if (res[key] === undefined) {
- res[key] = val
- } else if (Array.isArray(res[key])) {
- res[key].push(val)
- } else {
- res[key] = [res[key], val]
- }
- })
- // #endif
- // #ifndef H5
- var pages = getCurrentPages() //获取加载的页面
- var currentPage = pages[pages.length - 1] //获取当前页面的对象
- var url = currentPage.route //当前页面url
- res = currentPage.options //如果要获取url中所带的参数可以查看options
- // #endif
-
- return res
- }
-
- export function urlDecode(query) {
- if (!query) return null
- let hash, object = {}
- const hashes = query.slice(query.indexOf('?') + 1).split('&')
- for (let i = 0; i < hashes.length; i++) {
- hash = hashes[i].split('=')
- object[hash[0]] = hash[1]
- }
- return object;
- }
- /**
- * 将值复制到目标对象,且以目标对象属性为准,例:target: {a:1} source:{a:2,b:3} 结果为:{a:2}
- * @param target 目标对象
- * @param source 源对象
- */
- export const copyValueToTarget = (target, source) => {
- const newObj = Object.assign({}, target, source);
- // 删除多余属性
- Object.keys(newObj).forEach((key) => {
- // 如果不是target中的属性则删除
- if (Object.keys(target).indexOf(key) === -1) {
- delete newObj[key];
- }
- });
- // 更新目标对象值
- Object.assign(target, newObj);
- };
|