import Vue from 'vue' import Cookies from 'js-cookie' let moment = require('moment-timezone') require('moment/locale/cs') // 用户登录校验 Vue.prototype.getCookie = function() { return Vue.prototype.$Cookies.get('token'); } // 用户是否登录验证 Vue.prototype.getCookieReturnType = function() { if(Vue.prototype.$Cookies.get('token')){ return true }else { return false } } // 获取用户信息 Vue.prototype.getUserInfo = function() { return Vue.prototype.$Cookies.get('userInfo'); } Vue.prototype.handlePublishTimeDesc = function(curTime,postModified,language){ // 计算两个时间戳 const NewcurTime=moment(curTime).valueOf(); const NewpostModified=moment(postModified).valueOf(); const timeDiff=moment(NewcurTime).diff(moment(NewpostModified)) // 单位换算 var min = 60 * 1000; var hour = min * 60; var day = hour * 24; var week = day * 7; var month = week*4; var year = month*12; // 计算发布时间距离当前时间的周、天、时、分 var exceedyear = Math.floor(timeDiff/year); var exceedmonth = Math.floor(timeDiff/month); var exceedWeek = Math.floor(timeDiff/week); var exceedDay = Math.floor(timeDiff/day); var exceedHour = Math.floor(timeDiff/hour); var exceedMin = Math.floor(timeDiff/min); // debugger; // 最后判断时间差到底是属于哪个区间,然后return if(exceedyear<100&&exceedyear>0){ return language=='zh'?exceedyear + '年前':exceedyear + ' years ago'; }else{ if(exceedmonth<12&&exceedmonth>0){ return language=='zh'?exceedmonth + '月前':exceedmonth + ' months ago'; }else{ if(exceedWeek<4&&exceedWeek>0){ return language=='zh'?exceedWeek + '星期前':exceedWeek + ' weeks ago'; }else{ if(exceedDay < 7 && exceedDay > 0){ return language=='zh'?exceedDay + '天前':exceedDay + ' days ago' }else { if (exceedHour < 24 && exceedHour > 0) { return language=='zh'?exceedHour + '小时前':exceedHour + ' hours ago' }else { return language=='zh'?exceedMin + '分钟前':exceedMin + ' minutes ago' } } } } } } Vue.prototype.formatDate =function (value, formatString, defaultString){ if(value == null){ return defaultString ? defaultString : ""; } formatString = formatString || 'YYYY-MM-DD'; return moment(value).tz('Asia/Shanghai').format(formatString); } Vue.prototype.formatDateAndHouse =function (value, formatString, defaultString){ if(value == null){ return defaultString ? defaultString : ""; } formatString = formatString || 'YYYY-MM-DD HH:mm:ss'; return moment(value).tz('Asia/Shanghai').format(formatString,"YYYY-MM-DD HH:mm:ss"); }