common.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import Vue from 'vue'
  2. import Cookies from 'js-cookie'
  3. let moment = require('moment-timezone')
  4. require('moment/locale/cs')
  5. // 用户登录校验
  6. Vue.prototype.getCookie = function() {
  7. return Vue.prototype.$Cookies.get('token');
  8. }
  9. // 用户是否登录验证
  10. Vue.prototype.getCookieReturnType = function() {
  11. if(Vue.prototype.$Cookies.get('token')){
  12. return true
  13. }else {
  14. return false
  15. }
  16. }
  17. // 获取用户信息
  18. Vue.prototype.getUserInfo = function() {
  19. return Vue.prototype.$Cookies.get('userInfo');
  20. }
  21. Vue.prototype.handlePublishTimeDesc = function(curTime,postModified,language){
  22. // 计算两个时间戳
  23. const NewcurTime=moment(curTime).valueOf();
  24. const NewpostModified=moment(postModified).valueOf();
  25. const timeDiff=moment(NewcurTime).diff(moment(NewpostModified))
  26. // 单位换算
  27. var min = 60 * 1000;
  28. var hour = min * 60;
  29. var day = hour * 24;
  30. var week = day * 7;
  31. var month = week*4;
  32. var year = month*12;
  33. // 计算发布时间距离当前时间的周、天、时、分
  34. var exceedyear = Math.floor(timeDiff/year);
  35. var exceedmonth = Math.floor(timeDiff/month);
  36. var exceedWeek = Math.floor(timeDiff/week);
  37. var exceedDay = Math.floor(timeDiff/day);
  38. var exceedHour = Math.floor(timeDiff/hour);
  39. var exceedMin = Math.floor(timeDiff/min);
  40. // debugger;
  41. // 最后判断时间差到底是属于哪个区间,然后return
  42. if(exceedyear<100&&exceedyear>0){
  43. return language=='zh'?exceedyear + '年前':exceedyear + ' years ago';
  44. }else{
  45. if(exceedmonth<12&&exceedmonth>0){
  46. return language=='zh'?exceedmonth + '月前':exceedmonth + ' months ago';
  47. }else{
  48. if(exceedWeek<4&&exceedWeek>0){
  49. return language=='zh'?exceedWeek + '星期前':exceedWeek + ' weeks ago';
  50. }else{
  51. if(exceedDay < 7 && exceedDay > 0){
  52. return language=='zh'?exceedDay + '天前':exceedDay + ' days ago'
  53. }else {
  54. if (exceedHour < 24 && exceedHour > 0) {
  55. return language=='zh'?exceedHour + '小时前':exceedHour + ' hours ago'
  56. }else {
  57. return language=='zh'?exceedMin + '分钟前':exceedMin + ' minutes ago'
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. Vue.prototype.formatDate =function (value, formatString, defaultString){
  65. if(value == null){
  66. return defaultString ? defaultString : "";
  67. }
  68. formatString = formatString || 'YYYY-MM-DD';
  69. return moment(value).tz('Asia/Shanghai').format(formatString);
  70. }
  71. Vue.prototype.formatDateAndHouse =function (value, formatString, defaultString){
  72. if(value == null){
  73. return defaultString ? defaultString : "";
  74. }
  75. formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  76. return moment(value).tz('Asia/Shanghai').format(formatString,"YYYY-MM-DD HH:mm:ss");
  77. }