问星数据小程序

common.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * 地址栏参数
  3. */
  4. function parseQueryString(key) {
  5. var urlObj = {};
  6. var reg = /([^?=&]+)=([^?=&]+)/g;
  7. (window.location.href).replace(reg, ($0, $1, $2) => {
  8. urlObj[$1] = decodeURIComponent($2);
  9. })
  10. return urlObj[key];
  11. }
  12. /**
  13. * 数值格式化
  14. */
  15. function NumberHandle (value,numberDigit) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
  16. if(typeof parseFloat(value) === 'number' && !isNaN(value)){
  17. //判断是否是数值类型
  18. if((value >= 10000 && value < 10000000) || (value <= -10000 && value > -10000000)){
  19. return hasDot(value/10000,numberDigit) + 'w'
  20. }else if((value >= 10000000 && value < 100000000) || (value <= -10000000 && value > -100000000)){
  21. return hasDot(value/10000000,numberDigit) + 'kw'
  22. }else if(value >= 100000000 || value <= -100000000){
  23. return hasDot(value/100000000,numberDigit) + '亿'
  24. }else{
  25. return hasDot(value,numberDigit)
  26. }
  27. }else{
  28. return value
  29. }
  30. }
  31. function hasDot(num,numberDigit){
  32. //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
  33. var num = parseFloat(num)
  34. var digit = 1;
  35. if(numberDigit){
  36. digit = numberDigit
  37. }
  38. return (num + '').indexOf('.') != -1 ? num.toFixed(digit) : num;
  39. }
  40. /**
  41. * 日期获取
  42. */
  43. function getDay (day,haveHours){
  44. //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
  45. var today = new Date();
  46. var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  47. today.setTime(targetday_milliseconds); //注意,这行是关键代码
  48. var tYear = today.getFullYear();
  49. var tMonth = today.getMonth();
  50. var tDate = today.getDate();
  51. var hours = today.getHours();
  52. var minutes = today.getMinutes(); //分
  53. var seconds = today.getSeconds(); //秒
  54. tMonth = doHandleMonth(tMonth + 1);
  55. tDate = doHandleMonth(tDate);
  56. if(haveHours){
  57. if(day == 0){
  58. return {
  59. 'now':tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds,
  60. 'zero':tYear+"-"+tMonth+"-"+tDate+' ' + '00:00:00'
  61. }
  62. }else{
  63. return tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds
  64. }
  65. }else{
  66. return tYear+"-"+tMonth+"-"+tDate
  67. }
  68. function doHandleMonth(month){
  69. var m = month;
  70. if(month.toString().length == 1){
  71. m = "0" + month;
  72. }
  73. return m;
  74. }
  75. }
  76. module.exports = {
  77. parseQueryString: parseQueryString,
  78. NumberHandle: NumberHandle,
  79. getDay: getDay
  80. }