publicFunction.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //1.格式化日期 start ---------------------------------------->
  2. //time 日期字符串 type 从什么时候开始返回 year = 返回年月日 month = 返回月日..
  3. //style 样式,比如 年-月-日 年.月.日
  4. const getTime = function (time: string, type: string, style: Number) {
  5. const date = new Date(time);
  6. const year = date.getFullYear();
  7. const month = date.getMonth() + 1;
  8. const day = date.getDate();
  9. //返回 年-月-日
  10. if (type == 'year' && style == 0) {
  11. return `${year}.${month}.${day}`;
  12. }
  13. //返回 年-月-日
  14. if (type == 'year' && style == 1) {
  15. return `${year}-${month}-${day}`;
  16. }
  17. //返回 年-月
  18. if (type == 'year' && style == 2) {
  19. return `${year}-${month}`;
  20. }
  21. //返回 年
  22. if (type == 'year' && style == 3) {
  23. return `${year}`;
  24. }
  25. //返回 月-日
  26. if (type == 'month' && style == 1) {
  27. return `${month}-${day}`;
  28. }
  29. //返回 月.日
  30. if (type == 'month' && style == 2) {
  31. return `${month}.${day}`;
  32. }
  33. //返回 日
  34. if (type == 'day' && style == 1) {
  35. return `${day}`;
  36. }
  37. }
  38. //1.格式化日期 end ---------------------------------------->
  39. //2.格式化标题长度 start ---------------------------------------->
  40. //title 标题 length 长度
  41. const getTitleLength = function (title: string, length: number) {
  42. if (title.length >= length) {
  43. return title.substring(0, length) + "...";
  44. } else {
  45. return title;
  46. }
  47. }
  48. //2.格式化标题长度 start ---------------------------------------->
  49. //3.格式化跳转路径 start ---------------------------------------->
  50. //3.1跳转到频道页面或者一级列表页
  51. const getLinkPath = (item: any) => {
  52. if (item.is_url == 1) {
  53. return `${item.web_url}`;
  54. } else if (item.children_count == 0) {
  55. //return `/newsList/${item.category_id}?page=1`;
  56. return `/${item.aLIas_pinyin}/list-1.html`;
  57. } else {
  58. //return `/primaryNavigation/${item.aLIas_pinyin}/`;
  59. return `/${item.aLIas_pinyin}/index.html`;
  60. }
  61. }
  62. const getLinkPath1 = (item: any) => {
  63. return `/${item.pinyin}/list-1.html`;
  64. }
  65. //3.2跳转到详情页
  66. const getLinkPathDetail = (item: any) => {
  67. if (item.islink == 1) {
  68. return `${item.linkurl}`;
  69. } else {
  70. return `/${item.pinyin}/${item.id}.html`;
  71. }
  72. }
  73. //3.格式化跳转路径 end ---------------------------------------->
  74. //4.获得路由路径 start ---------------------------------------->
  75. const getRoutePath = (type: Number) => {
  76. const route = useRoute();
  77. //获得当前的完整路径
  78. const fullPath = route.path;
  79. //拆分,取出来中间这一段,然后提取数字部分
  80. const segments = fullPath.split('/');
  81. const targetSegmentOne = segments[1];
  82. const targetSegmentTwo = segments[2];
  83. if (type == 1) {
  84. return targetSegmentOne;
  85. }
  86. if (type == 2) {
  87. return targetSegmentTwo;
  88. }
  89. }
  90. //4.获得路由路径 end ---------------------------------------->
  91. export { getTime, getTitleLength, getLinkPath, getLinkPathDetail, getRoutePath, getLinkPath1 };