publicFunction.ts 4.0 KB

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