filter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Vue from 'vue'
  2. import { config } from '@/config/config'
  3. //保留两位小数
  4. Vue.filter('keepTwoNum',function(val){
  5. let value = Number(val)
  6. return value.toFixed(2)
  7. })
  8. //链接加时间戳
  9. Vue.filter("timestamp", function(link) {
  10. return link + '?t=' + new Date().getTime();
  11. });
  12. //截取第一张照片且判断是否有图片,没有图片按照传递类型随机输出
  13. Vue.filter("firstImg",function(arr,sizeType,imgType){
  14. let img;
  15. //图片类型判断
  16. if(arr){
  17. if(arr instanceof Array){
  18. img = arr[0]
  19. }else{
  20. img = arr.split(',')[0]
  21. }
  22. }else if(!arr){
  23. //如果没有图片则随机输出一张
  24. // if(imgType === 'farmer'){
  25. // const farmerList = ['carbon2/farmer/1.png','carbon2/farmer/2.png']
  26. // img = farmerList[Math.floor(Math.random() * farmerList.length)];
  27. // }
  28. img = '/static/img/inbuild.png'
  29. }
  30. // return config.imgUrl + img;
  31. return img;
  32. });
  33. Vue.filter("miniImg",function(img,quality){
  34. return img+'?imageMogr2/quality/'+quality
  35. });
  36. Vue.filter("formatDateTime",function(time){
  37. if (time == null || time === '') {
  38. return 'N/A';
  39. }
  40. //不足10 补0操作
  41. var addZero=function (num) {
  42. return num < 10 ? '0' + num : num;
  43. };
  44. let time1 = time.replace(/T/g, ' ');
  45. time1 = time1.replace(/\-/g, "/");
  46. console.log(time1.substring(0,19));
  47. var time2 = (new Date(time1)).getTime();
  48. console.log('a',time2);
  49. var date = new Date(time2);
  50. console.log('date',date);
  51. var Year = date.getFullYear();
  52. var Month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  53. var d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  54. var Hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  55. var Minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  56. var Seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  57. var over_time = Year + "/" + Month + "/" + d + " " + Hours + ":" + Minutes + ":" + Seconds
  58. //***至此以上是将时间转为正常时间格式,以下为将时间进行增加8小时解决时区差异的操作***
  59. var time3 = new Date(Date.parse(over_time));
  60. // time3.setTime(time3.setHours(time3.getHours()));
  61. time3.setTime(time3.setHours(time3.getHours() + 8));
  62. var Y = time3.getFullYear() + '/';
  63. var M =addZero(time3.getMonth() + 1) + '/';
  64. var D =addZero(time3.getDate()) + ' ';
  65. var h = addZero(time3.getHours()) + ':';
  66. var m =addZero(time3.getMinutes()) + ':';
  67. var s =addZero(time3.getSeconds());
  68. return (Y + M + D + " " + h + m + s)
  69. // return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
  70. });