dashboard.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // components/dashboard/dashboard.js
  2. const app = getApp();
  3. import {
  4. linearColors,
  5. ladderOptions
  6. } from './baseData';
  7. Component({
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. params: {
  13. type: Object,
  14. value: {
  15. waterStages: []
  16. }
  17. }
  18. },
  19. observers: {
  20. params(params) {
  21. let {
  22. username,
  23. userNumber,
  24. useAmount,
  25. laddertype,
  26. waterStages = [],
  27. ladderlevel
  28. } = params;
  29. const newAmount = app.$util.numberFormat(useAmount, 1);
  30. params.intNum = newAmount.split('.')[0] || 0;
  31. params.floatNum = newAmount.split('.')[1] || 0;
  32. const arr = [];
  33. params.style = linearColors[ladderlevel];
  34. if (userNumber && username) {
  35. if (laddertype && waterStages.length) {
  36. waterStages.forEach((v, k) => {
  37. arr.unshift(Object.assign({}, v, ladderOptions[k]));
  38. })
  39. const baseRate = (ladderlevel - 1) * (1 / waterStages.length);
  40. const {
  41. minValue,
  42. maxValue
  43. } = waterStages[ladderlevel - 1];
  44. const otherRate = (useAmount - minValue) / (maxValue - minValue) * 0.25;
  45. const rate = parseFloat(baseRate + otherRate);
  46. params.percentage = rate > 1 ? 100 : rate < 0 ? 0 : parseFloat(rate * 100 + (rate ? 1 : 3)).toFixed(2);
  47. this.setData({
  48. userInfo: params,
  49. waterStages: arr
  50. })
  51. } else {
  52. const scale = useAmount ? Math.ceil(useAmount/15)*15 : 10;
  53. for (let i = 3; i > 0; i--) {
  54. arr.push({
  55. maxValue: (scale * i),
  56. minValue: (scale * (i - 1)),
  57. color: '#0091FF',
  58. name: '用水量',
  59. background: 'rgba(0, 145, 255, 0.1)'
  60. })
  61. }
  62. const rate = useAmount ? parseFloat(useAmount / (scale * 3)) : 0;
  63. params.percentage = parseFloat(rate * 100 + (rate ? 1 : 3)).toFixed(2);
  64. this.setData({
  65. userInfo: params,
  66. waterStages: arr
  67. })
  68. }
  69. }
  70. }
  71. },
  72. /**
  73. * 组件的初始数据
  74. */
  75. data: {
  76. ec: {
  77. lazyLoad: true
  78. },
  79. userInfo: {},
  80. waterStages: []
  81. },
  82. /**
  83. * 组件的方法列表
  84. */
  85. methods: {
  86. }
  87. })