myfun.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // 返回两个日期之间的月份
  2. export function getDiffDate(minDate, maxDate) {
  3. let startDate = new Date(minDate.replace(/-/g, '/'));
  4. let endDate = new Date(maxDate.replace(/-/g, '/'));
  5. let months = [];
  6. //把时间的天数都设置成当前月第一天
  7. startDate.setDate(1)
  8. endDate.setDate(1)
  9. // new Date(yyyy-MM-dd) 不知为何有时候小时是 08 有时候是00
  10. endDate.setHours(0)
  11. startDate.setHours(0)
  12. while (endDate.getTime() >= startDate.getTime()) {
  13. let year = startDate.getFullYear();
  14. let month = startDate.getMonth() + 1;
  15. //加一个月
  16. startDate.setMonth(month)
  17. if (month.toString().length == 1) {
  18. month = "0" + month;
  19. }
  20. months.push(year + "-" + month)
  21. }
  22. return months;
  23. }
  24. // 将rgb颜色转成hex
  25. export function RtoH(color) {
  26. var rgb = color.split(',');
  27. var r = parseInt(rgb[0].split('(')[1]);
  28. var g = parseInt(rgb[1]);
  29. var b = parseInt(rgb[2].split(')')[0]);
  30. var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  31. return hex;
  32. }
  33. // 将hex颜色转成rgb
  34. export function HtoR(hex, opacity) {
  35. var RGBA = "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
  36. // return { red: parseInt("0x" + hex.slice(1, 3)), green: parseInt("0x" + hex.slice(3, 5)), blue: parseInt("0x" + hex.slice(5, 7)), rgba: RGBA }
  37. return RGBA;
  38. }
  39. // 189 **** 0222
  40. export function phoneNumA(value) {
  41. if (value && value.length > 8) {
  42. return `${value.substring(0, 3)} ${"*".repeat(value.length - 7).replace(/(.{4})/g, "$1 ")}${value.length % 4 ? " " : ""}${value.slice(-4)}`;
  43. }
  44. return value;
  45. }
  46. // goto
  47. export function goto(url, json) {
  48. let params = "";
  49. if (json) {
  50. params = [];
  51. let da = Object.keys(json).map(function (key) {
  52. if (json[key]) return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
  53. });
  54. da.forEach(e => {
  55. if (e) params.push(e)
  56. });
  57. params = params.join("&")
  58. }
  59. uni.navigateTo({ url: url + '?' + params });
  60. }
  61. // tidyTpye
  62. export function tidyTpye(va) {
  63. let a = Number(va)
  64. switch (a) {
  65. case 1: return "零售专区";
  66. case 2: return "批发专区";
  67. case 3: return "货易优选";
  68. case 4: return "甄选好物";
  69. case 5: return "茶宝兑换";
  70. case 6: return "天天捡漏";
  71. default: return ""
  72. }
  73. }
  74. export function hotelStarf(va) {
  75. switch (va) {
  76. case 1: return "一星级";
  77. case 2: return "二星级";
  78. case 3: return "三星级";
  79. case 4: return "四星级";
  80. case 5: return "五星级";
  81. case 6: return "经济型";
  82. case 7: return "舒适型";
  83. case 8: return "高档型";
  84. case 9: return "豪华型";
  85. default: return ""
  86. }
  87. }
  88. // json对象数组按照某个属性排序:降序排列
  89. let compareDesc = (propertyName) => {
  90. return function (object1, object2) {
  91. var value1 = object1[propertyName];
  92. var value2 = object2[propertyName];
  93. if (value2 < value1) {
  94. return -1;
  95. } else if (value2 > value1) {
  96. return 1;
  97. } else {
  98. return 0;
  99. }
  100. }
  101. }
  102. // json对象数组按照某个属性排序:升序排列
  103. let compareAsc = (propertyName) => {
  104. return function (object1, object2) {
  105. var value1 = object1[propertyName];
  106. var value2 = object2[propertyName];
  107. if (value2 < value1) {
  108. return 1;
  109. } else if (value2 > value1) {
  110. return -1;
  111. } else {
  112. return 0;
  113. }
  114. }
  115. }
  116. // json排序
  117. export function Desc(arr = [], key = "id") {
  118. return arr.sort(compareDesc(key));
  119. }
  120. export function Asc(arr = [], key = "id") {
  121. return arr.sort(compareAsc(key));
  122. }
  123. // 判断今明后天
  124. export function getThatDay(d) {
  125. var td = new Date();
  126. td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
  127. var od = new Date(d);
  128. od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
  129. var xc = (od - td) / 1000 / 60 / 60 / 24;
  130. if (xc == 0) {
  131. return "今天";
  132. } else if (xc < 2) {
  133. return "明天";
  134. } else if (xc < 3) {
  135. return "后天";
  136. } else {
  137. return "";
  138. }
  139. }
  140. // 计算两个日期之间的天数
  141. export function getDaysBetween(dateString1, dateString2) {
  142. var startDate = Date.parse(dateString1);
  143. var endDate = Date.parse(dateString2);
  144. if (startDate > endDate) {
  145. return 0;
  146. }
  147. if (startDate == endDate) {
  148. return 1;
  149. }
  150. var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
  151. return days;
  152. }