'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<template>
  <uni-progress
    class="uni-progress"
    v-on="$listeners"
  >
    <div
      :style="outerBarStyle"
      class="uni-progress-bar"
    >
      <div
        :style="innerBarStyle"
        class="uni-progress-inner-bar"
      />
    </div>
    <template v-if="showInfo">
      <p class="uni-progress-info">
        {{ currentPercent }}%
      </p>
    </template>
  </uni-progress>
</template>
<script>
const VALUES = {
  activeColor: '#007AFF',
  backgroundColor: '#EBEBEB',
  activeMode: 'backwards'
}
export default {
  name: 'Progress',
  props: {
    percent: {
      type: [Number, String],
      default: 0,
      validator (value) {
        return !isNaN(parseFloat(value, 10))
      }
    },
    showInfo: {
      type: [Boolean, String],
      default: false
    },
    strokeWidth: {
      type: [Number, String],
      default: 6,
      validator (value) {
        return !isNaN(parseFloat(value, 10))
      }
    },
    color: {
      type: String,
      default: VALUES.activeColor
    },
    activeColor: {
      type: String,
      default: VALUES.activeColor
    },
    backgroundColor: {
      type: String,
      default: VALUES.backgroundColor
    },
    active: {
      type: [Boolean, String],
      default: false
    },
    activeMode: {
      type: String,
      default: VALUES.activeMode
    },
    duration: {
      type: [Number, String],
      default: 30,
      validator (value) {
        return !isNaN(parseFloat(value, 10))
      }
    }
  },
  data () {
    return {
      currentPercent: 0,
      strokeTimer: 0,
      lastPercent: 0
    }
  },
  computed: {
    outerBarStyle () {
      return `background-color: ${this.backgroundColor}; height: ${this.strokeWidth}px;`
    },
    innerBarStyle () {
      // 兼容下不推荐的属性,activeColor 优先级高于 color。
      let backgroundColor = ''
      if (this.color !== VALUES.activeColor && this.activeColor === VALUES.activeColor) {
        backgroundColor = this.color
      } else {
        backgroundColor = this.activeColor
      }
      return `width: ${this.currentPercent}%;background-color: ${backgroundColor}`
    },
    realPercent () {
      // 确保最终计算时使用的是 Number 类型的值,并且在有效范围内。
      let realValue = parseFloat(this.percent, 10)
      realValue < 0 && (realValue = 0)
      realValue > 100 && (realValue = 100)
      return realValue
    }
  },
  watch: {
    realPercent (newValue, oldValue) {
      this.strokeTimer && clearInterval(this.strokeTimer)
      this.lastPercent = oldValue || 0
      this._activeAnimation()
    }
  },
  created () {
    this._activeAnimation()
  },
  methods: {
    _activeAnimation () {
      if (this.active) {
        this.currentPercent = this.activeMode === VALUES.activeMode ? 0 : this.lastPercent
        this.strokeTimer = setInterval(() => {
          if (this.currentPercent + 1 > this.realPercent) {
            this.currentPercent = this.realPercent
            this.strokeTimer && clearInterval(this.strokeTimer)
          } else {
            this.currentPercent += 1
          }
        }, parseFloat(this.duration))
      } else {
        this.currentPercent = this.realPercent
      }
    }
  }
}
</script>
<style>
    uni-progress {
        display: -webkit-flex;
        display: flex;
        -webkit-align-items: center;
        align-items: center;
    }
 
    uni-progress[hidden] {
        display: none;
    }
 
    .uni-progress-bar {
        -webkit-flex: 1;
        flex: 1;
    }
 
    .uni-progress-inner-bar {
        width: 0;
        height: 100%;
    }
 
    .uni-progress-info {
        margin-top: 0;
        margin-bottom: 0;
        min-width: 2em;
        margin-left: 15px;
        font-size: 16px;
    }
</style>