publicFunction.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //1.格式化日期
  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. //2.格式化标题长度
  39. //title 标题 length 长度
  40. const getTitleLength = function (title: string, length: number) {
  41. if (title.length >= length) {
  42. return title.substring(0, length) + "...";
  43. } else {
  44. return title;
  45. }
  46. }
  47. //3.格式化跳转路径 start ---------------------------------------->
  48. //3.1跳转到频道页面或者一级列表页
  49. const getLinkPath = (item: any) => {
  50. if (item.is_url == 1) {
  51. return `${item.web_url}`;
  52. } else if (item.alias == "供求信息") {
  53. return `/gongqiuxinxi/gongyingxinxi/index.html`;
  54. } else if (item.children_count == 0) {
  55. return `/${item.aLIas_pinyin}/list-1.html`;
  56. } else {
  57. return `/${item.aLIas_pinyin}/index.html`;
  58. }
  59. }
  60. const getLinkPath1 = (item: any) => {
  61. return `/${item.aLIas_pinyin}/list-1.html`;
  62. }
  63. //3.2跳转到详情页
  64. const getLinkPathDetail = (item: any) => {
  65. if (item.islink == 1) {
  66. return `${item.linkurl}`;
  67. } else {
  68. return `/${item.pinyin}/${item.id}.html`;
  69. }
  70. }
  71. //3.格式化跳转路径 end ---------------------------------------->
  72. //4.获得路由路径 start ---------------------------------------->
  73. const getRoutePath = (type: Number) => {
  74. const route = useRoute();
  75. //获得当前的完整路径
  76. const fullPath = route.path;
  77. //拆分,取出来中间这一段,然后提取数字部分
  78. const segments = fullPath.split('/');
  79. const targetSegmentOne = segments[1];
  80. const targetSegmentTwo = segments[2];
  81. const targetSegmentThree = segments[3];
  82. if (type == 1) {
  83. return targetSegmentOne;
  84. }
  85. if (type == 2) {
  86. return targetSegmentTwo;
  87. }
  88. if (type == 3) {
  89. return targetSegmentThree;
  90. }
  91. }
  92. //4.获得路由路径 end ---------------------------------------->
  93. export { getTime, getTitleLength, getLinkPath, getLinkPath1, getLinkPathDetail, getRoutePath };
  94. // export { getTime, getTitleLength };