publicFunction.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.is_url == 1) {
  49. return `${item.web_url}`;
  50. } else if (item.children_count == 0) {
  51. //return `/newsList/${item.category_id}?page=1`;
  52. return `/${item.aLIas_pinyin}/list-1.html`;
  53. } else {
  54. //return `/primaryNavigation/${item.aLIas_pinyin}/`;
  55. return `/${item.aLIas_pinyin}/index.html`;
  56. }
  57. }
  58. const getLinkPath1 = (item: any) => {
  59. return `/${item.aLIas_pinyin}/list-1.html`;
  60. }
  61. //3.2跳转到详情页
  62. const getLinkPathDetail = (item: any) => {
  63. if (item.islink == 1) {
  64. return `${item.linkurl}`;
  65. } else {
  66. return `/${item.pinyin}/${item.id}.html`;
  67. }
  68. }
  69. const getLinkPathDetail1 = (item: any) => {
  70. if (item.islink == 1) {
  71. return `${item.linkurl}`;
  72. } else {
  73. return `/${item.aLIas_pinyin}/${item.id}.html`;
  74. }
  75. }
  76. //3.2商城跳转
  77. //3.2.1 菜单
  78. const getShopLinkPath = (item: any) => {
  79. if (item.type_id == 1) {
  80. //1=供应
  81. return `/${item.pinyin}/gongying/${item.good_id}.html`;
  82. } else {
  83. //2=求购
  84. return `/${item.pinyin}/qiugou/${item.good_id}.html`;
  85. }
  86. }
  87. //3.2.2 商品详情
  88. const getShopPathDetail1 = (item: any) => {
  89. if (item.type_id == 1) {
  90. //1=供应
  91. return `/${item.pinyin}/gongying/${item.id}.html`;
  92. } else {
  93. //2=求购
  94. return `/${item.pinyin}/qiugou/${item.id}.html`;
  95. }
  96. }
  97. //3.2.3 商品属于求购还是供应
  98. const getShopType = (item: any) => {
  99. if (item.type_id == 1) {
  100. return '供应';
  101. } else {
  102. return '求购';
  103. }
  104. }
  105. //3.格式化跳转路径 end ---------------------------------------->
  106. //4.获得路由路径 start ---------------------------------------->
  107. const getRoutePath = (type: Number) => {
  108. const route = useRoute();
  109. //获得当前的完整路径
  110. const fullPath = route.path;
  111. //拆分,取出来中间这一段,然后提取数字部分
  112. const segments = fullPath.split('/');
  113. const targetSegmentOne = segments[1];
  114. const targetSegmentTwo = segments[2];
  115. const targetSegmentThree = segments[3];
  116. const targetSegmentFour = segments[4];
  117. if (type == 1) {
  118. return targetSegmentOne;
  119. }
  120. if (type == 2) {
  121. return targetSegmentTwo;
  122. }
  123. if (type == 3) {
  124. return targetSegmentThree;
  125. }
  126. if (type == 4) {
  127. return targetSegmentFour;
  128. }
  129. }
  130. //4.获得路由路径 end ---------------------------------------->
  131. export { getTime, getTitleLength, getLinkPath, getLinkPathDetail,
  132. getRoutePath, getLinkPath1, getLinkPathDetail1,getShopLinkPath,
  133. getShopPathDetail1,getShopType
  134. };