publicFunction.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 == 3) {
  23. return `${month}`;
  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.children_count == 0 || item.children_count == null) {
  55. //return `/newsList/${item.category_id}?page=1`;
  56. return `/${item.aLIas_pinyin}/list-1.html`;
  57. } else {
  58. //return `/primaryNavigation/${item.aLIas_pinyin}/`;
  59. return `/${item.aLIas_pinyin}/index.html`;
  60. }
  61. }
  62. //3.2跳转到频道页
  63. const getLinkPath1 = (item: any) => {
  64. return `/${item.aLIas_pinyin}/list-1.html`;
  65. }
  66. //3.3跳转到详情页
  67. const getLinkPathDetail = (item: any) => {
  68. if (item.islink == 1) {
  69. return `${item.linkurl}`;
  70. } else {
  71. return `/${item.pinyin}/${item.id}.html`;
  72. }
  73. }
  74. //3.3特殊跳转到详情页
  75. const getLinkPathDetail1 = (item: any) => {
  76. if (item.islink == 1) {
  77. return `${item.linkurl}`;
  78. } else {
  79. return `/${item.aLIas_pinyin}/${item.id}.html`;
  80. }
  81. }
  82. //3.格式化跳转路径 end ---------------------------------------->
  83. //4.获得路由路径 start ---------------------------------------->
  84. const getRoutePath = (type:number) => {
  85. const route = useRoute();
  86. //获得当前的完整路径
  87. const fullPath = route.path;
  88. //拆分,取出来中间这一段,然后提取数字部分
  89. const segments = fullPath.split('/');
  90. const targetSegmentOne = segments[1];
  91. const targetSegmentTwo = segments[2];
  92. if (type == 1) {
  93. return targetSegmentOne;
  94. }
  95. if (type == 2) {
  96. return targetSegmentTwo;
  97. }
  98. }
  99. //4.获得路由路径 end ---------------------------------------->
  100. //5.从广告池获得对应标识的广告 start ---------------------------------------->
  101. interface IAdList {
  102. ad_tag: string;
  103. height: number;
  104. introduce: string;
  105. name: string;
  106. price: string;
  107. thumb: string;
  108. typeid: number;
  109. website_id: number;
  110. width: number;
  111. }
  112. const requestAd = (adList:IAdList[],adTag:string) => {
  113. for(let item of adList){
  114. if(item.ad_tag == adTag){
  115. return item;
  116. }
  117. }
  118. return null;
  119. }
  120. //5.从广告池获得对应标识的广告 end ---------------------------------------->
  121. //6.提取请求数据 start ---------------------------------------->
  122. const getTransformArray = (arr:any[]) => {
  123. return arr
  124. .filter(item => item.child !== '') //过滤child为空
  125. .map(item => ({
  126. parent: item.parent,
  127. child: `${item.child},${item.imgnum},${item.textnum}`
  128. }));
  129. }
  130. //6.提取请求数据 end ---------------------------------------->
  131. export { getTime, getTitleLength, getLinkPath, getLinkPathDetail,getLinkPathDetail1, getRoutePath, getLinkPath1,requestAd,getTransformArray};