publicFunction.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 == 1){
  11. return `${year}-${month}-${day}`;
  12. }
  13. //返回 年-月
  14. if(type == 'year'&& style == 2){
  15. return `${year}-${month}`;
  16. }
  17. //返回 年
  18. if(type == 'year'&& style == 3){
  19. return `${year}`;
  20. }
  21. //返回 月-日
  22. if(type == 'month'&& style == 1){
  23. return `${month}-${day}`;
  24. }
  25. //返回 月.日
  26. if(type == 'month'&& style == 2){
  27. return `${month}.${day}`;
  28. }
  29. //返回 日
  30. if(type == 'day'&& style == 1){
  31. return `${day}`;
  32. }
  33. }
  34. //1.格式化日期 end ---------------------------------------->
  35. //2.格式化标题长度 start ---------------------------------------->
  36. //title 标题 length 长度
  37. const getTitleLength = function(title:string,length:number){
  38. if(title.length >= length){
  39. return title.substring(0, length) + "...";
  40. }else{
  41. return title;
  42. }
  43. }
  44. //2.格式化标题长度 start ---------------------------------------->
  45. //3.格式化跳转路径 start ---------------------------------------->
  46. //3.1跳转到频道页面或者一级列表页
  47. const getLinkPath = (item:any) => {
  48. if (item.children_count == 0) {
  49. return `/${item.aLIas_pinyin}/list-1.html`;
  50. } else {
  51. return `/${item.aLIas_pinyin}/index.html`;
  52. }
  53. }
  54. //3.2跳转到详情页
  55. const getLinkPathDetail = (item:any) => {
  56. if (item.islink == 1) {
  57. return `${item.linkurl}`;
  58. } else {
  59. return `/${item.pinyin}/${item.id}.html`;
  60. }
  61. }
  62. //3.格式化跳转路径 end ---------------------------------------->
  63. //4.获得路由路径 start ---------------------------------------->
  64. const getRoutePath = (type:Number) => {
  65. const route = useRoute();
  66. //获得当前的完整路径
  67. const fullPath = route.path;
  68. //拆分,取出来中间这一段,然后提取数字部分
  69. const segments = fullPath.split('/');
  70. const targetSegmentOne = segments[1];
  71. const targetSegmentTwo = segments[2];
  72. if(type == 1){
  73. return targetSegmentOne;
  74. }
  75. if(type == 2){
  76. return targetSegmentTwo;
  77. }
  78. }
  79. //4.获得路由路径 end ---------------------------------------->
  80. export { getTime,getTitleLength,getLinkPath,getLinkPathDetail,getRoutePath};