filter.js 2.6 KB

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