myfun.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  89. * json对象数组按照某个属性排序:降序排列
  90. * @param {Object} propertyName
  91. */
  92. let compareDesc = (propertyName) => {
  93. return function(object1, object2) {
  94. var value1 = object1[propertyName];
  95. var value2 = object2[propertyName];
  96. if (value2 < value1) {
  97. return -1;
  98. } else if (value2 > value1) {
  99. return 1;
  100. } else {
  101. return 0;
  102. }
  103. }
  104. }
  105. /**
  106. * json对象数组按照某个属性排序:升序排列
  107. * @param {Object} propertyName
  108. */
  109. let compareAsc = (propertyName) => {
  110. return function(object1, object2) {
  111. var value1 = object1[propertyName];
  112. var value2 = object2[propertyName];
  113. if (value2 < value1) {
  114. return 1;
  115. } else if (value2 > value1) {
  116. return -1;
  117. } else {
  118. return 0;
  119. }
  120. }
  121. }
  122. // json排序
  123. export function Desc(arr = [], key = "id") {
  124. return arr.sort(compareDesc(key));
  125. }
  126. export function Asc(arr = [], key = "id") {
  127. return arr.sort(compareAsc(key));
  128. }