publicFunction.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.alias == '招工招聘') { //招工招聘
  55. return `/${item.aLIas_pinyin}/index.html`;
  56. } else if (item.alias == '农民工求职') { //农民工求职
  57. return `/${item.aLIas_pinyin}/index.html`;
  58. } else if (item.children_count == 0) {
  59. //return `/newsList/${item.category_id}?page=1`;
  60. return `/${item.aLIas_pinyin}/list-1.html`;
  61. } else {
  62. //return `/primaryNavigation/${item.aLIas_pinyin}/`;
  63. return `/${item.aLIas_pinyin}/index.html`;
  64. }
  65. }
  66. const getLinkPath1 = (item: any) => {
  67. return `/${item.pinyin}/list-1.html`;
  68. }
  69. //3.2跳转到详情页
  70. const getLinkPathDetail = (item: any) => {
  71. if (item.islink == 1) {
  72. return `${item.linkurl}`;
  73. } else {
  74. return `/${item.pinyin}/${item.id}.html`;
  75. }
  76. }
  77. //3.格式化跳转路径 end ---------------------------------------->
  78. //4.获得路由路径 start ---------------------------------------->
  79. const getRoutePath = (type: Number) => {
  80. const route = useRoute();
  81. //获得当前的完整路径
  82. const fullPath = route.path;
  83. //拆分,取出来中间这一段,然后提取数字部分
  84. const segments = fullPath.split('/');
  85. const targetSegmentOne = segments[1];
  86. const targetSegmentTwo = segments[2];
  87. if (type == 1) {
  88. return targetSegmentOne;
  89. }
  90. if (type == 2) {
  91. return targetSegmentTwo;
  92. }
  93. }
  94. //4.获得路由路径 end ---------------------------------------->
  95. export { getTime, getTitleLength, getLinkPath, getLinkPathDetail, getRoutePath, getLinkPath1 };