-
zhangwei
2024-08-29 447e19fa298ae11a1c3a3fa2d2ff3a1a8dba0501
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID: 16 3,营业执照号:91  51  0131  33  200  6 19      3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-rate__wrap" ref="fui_rate_wrap">
        <view class="fui-rate__item" :class="{'fui-rate__not-allowed':disabled}" :style="{paddingRight:spacing + 'rpx'}"
            v-for="(item,index) in stars" :key="index" @touchstart.stop="touchstart" @touchmove.stop="touchmove"
            @mousedown.stop="mousedown" @mousemove.stop="mousemove" @mouseup="mouseup">
            <fui-icon :disabled="disabled" :name="getName(index,intScore,decimalScore)" :size="size"
                :color="index < intScore || (index == intScore && decimalScore > 0) ? getActiveColor : color"></fui-icon>
        </view>
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = uni.requireNativePlugin('dom');
    // #endif
    //非easycom模式取消注释引入字体组件,按实际路径进行调整
    // import fuiIcon from "@/components/firstui/fui-icon/fui-icon.vue"
    export default {
        emits: ['change'],
        name: "fui-rate",
        // components:{
        //     fuiIcon
        // },
        props: {
            max: {
                type: [Number, String],
                default: 5
            },
            score: {
                type: [Number, String],
                default: 0
            },
            color: {
                type: String,
                default: "#CCCCCC"
            },
            activeColor: {
                type: String,
                default: ""
            },
            disabled: {
                type: Boolean,
                default: false
            },
            size: {
                type: [Number, String],
                default: 56
            },
            spacing: {
                type: [Number, String],
                default: 0
            },
            allowHalf: {
                type: Boolean,
                default: false
            },
            halfRate: {
                type: [Number, String],
                default: 0.2
            },
            touchable: {
                type: Boolean,
                default: true
            },
            param: {
                type: [Number, String],
                default: 0
            }
        },
        watch: {
            max(newValue, oldValue) {
                this.initData(newValue)
            },
            score(newValue, oldValue) {
                this.initRateScore(newValue)
            }
        },
        computed: {
            getActiveColor() {
                let color = this.activeColor;
                if (!color || color === true) {
                    const app = uni && uni.$fui && uni.$fui.color;
                    color = (app && app.warning) || '#FFB703';
                }
                return color;
            }
        },
        data() {
            return {
                stars: [],
                pageX: 0,
                parentWidth: 0,
                intScore: 0,
                decimalScore: 0,
                isPC: false,
                rated: false,
                isMounted: false
            };
        },
        created() {
            this.initData(this.max)
            this.initRateScore(this.score)
            this.drawer = this.getParent()
        },
        mounted() {
            this.isMounted = true;
            this.$nextTick(() => {
                setTimeout(() => {
                    this._getSize()
                }, 100)
            })
            // #ifdef H5
            this.isPC = this.IsPC()
            // #endif
        },
        // #ifdef H5
        updated() {
            if (!this.isMounted && !this.pageX) {
                setTimeout(() => {
                    this._getSize()
                }, 50)
            }
        },
        // #endif
        methods: {
            getName(index, iScore, dScore) {
                let name = 'star'
                if (index < iScore) {
                    name += '-fill'
                }
                if (index === iScore && dScore > 0) {
                    name += 'half'
                }
                return name;
            },
            initData(vals) {
                vals = Number(vals)
                if (isNaN(vals)) {
                    vals = 5
                }
                vals = Math.ceil(vals)
                this.stars = Array.from(new Array(vals + 1).keys()).slice(1)
            },
            initRateScore(val) {
                val = Number(val);
                let intVal = parseInt(val);
                let decimalVal = val % 1;
                if (!this.allowHalf) {
                    intVal = decimalVal > 0 ? intVal + 1 : intVal;
                    decimalVal = 0;
                }
                this.intScore = intVal;
                this.decimalScore = decimalVal;
            },
            // #ifdef H5
            IsPC() {
                var userAgentInfo = navigator.userAgent;
                var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
                var flag = true;
                for (let v = 0; v < Agents.length - 1; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                        flag = false;
                        break;
                    }
                }
                return flag;
            },
            // #endif
            _setWidth(width) {
                const sys = uni.getSystemInfoSync()
                if (this.pageX < 0) {
                    this.parentWidth = width
                } else if (this.pageX > sys.windowWidth) {
                    this.parentWidth = -width
                }
            },
            setParentWidth(width) {
                setTimeout(() => {
                    this._setWidth(width)
                }, 100)
            },
            initParentWidth() {
                if (this.drawer) {
                    const sys = uni.getSystemInfoSync()
                    let pWidth = this.drawer.width
                    if (!pWidth) {
                        this.drawer._getSize((width) => {
                            this._setWidth(width)
                        })
                    } else {
                        this._setWidth(pWidth)
                    }
                }
            },
            _getSize() {
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select('.fui-rate__wrap')
                    .boundingClientRect()
                    .exec(ret => {
                        if (ret) {
                            this.pageX = ret[0].left || 0
                            this.initParentWidth()
                        }
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_rate_wrap'], (ret) => {
                    const size = ret.size
                    if (size) {
                        this.pageX = size.left
                        this.initParentWidth()
                    }
                })
                // #endif
            },
            _getRateScore(clientX) {
                const distance = clientX - (this.pageX + this.parentWidth);
                let score = 0;
                if (distance > 0) {
                    let width = uni.upx2px((Number(this.size) + Number(this.spacing)));
                    score = distance / width;
                    let decimalScore = score % 1;
                    if (!this.allowHalf) {
                        decimalScore = decimalScore > 0 ? 1 : 0;
                    } else {
                        if (decimalScore > Number(this.halfRate)) {
                            decimalScore = decimalScore <= 0.5 ? 0.5 : 1;
                        } else {
                            decimalScore = 0;
                        }
                    }
                    score = parseInt(score) + decimalScore;
                    let max = Number(this.max)
                    score = score > max ? max : score;
                }
                this.initRateScore(score)
                this.$emit('change', {
                    score: score,
                    param: this.param
                });
            },
            touchstart(e) {
                // #ifdef H5
                if (this.isPC) return
                // #endif
                if (this.disabled) return
                const {
                    clientX,
                    screenX
                } = e.changedTouches[0]
                // Nvue下为 screenX,其他平台为clientX
                this._getRateScore(clientX || screenX)
            },
            touchmove(e) {
                // #ifdef H5
                if (this.isPC) return
                // #endif
                if (this.disabled || !this.touchable) return
                const {
                    clientX,
                    screenX
                } = e.changedTouches[0]
                this._getRateScore(clientX || screenX)
            },
            mousedown(e) {
                // #ifdef H5
                if (!this.isPC || this.disabled) return
                const {
                    clientX,
                } = e
                this._getRateScore(clientX)
                this.rated = true
                // #endif
            },
            mousemove(e) {
                // #ifdef H5
                if (!this.isPC || !this.rated || this.disabled || !this.touchable) return
                const {
                    clientX
                } = e
                this._getRateScore(clientX)
                // #endif
            },
            mouseup(e) {
                // #ifdef H5
                if (!this.isPC || this.disabled || !this.touchable) return
                this.rated = false
                // #endif
            },
            getParent(name = 'fui-drawer') {
                let parent = this.$parent;
                let parentName = parent.$options.name;
                while (parentName !== name) {
                    parent = parent.$parent;
                    if (!parent) return false;
                    parentName = parent.$options.name;
                }
                return parent;
            },
            reset() {
                this._getSize();
            }
        }
    }
</script>
 
<style scoped>
    .fui-rate__wrap {
        /* #ifndef APP-NVUE */
        display: inline-flex;
        /* #endif */
        line-height: 1;
        font-size: 0;
        flex-direction: row;
        /* #ifdef H5 */
        cursor: pointer;
        /* #endif */
    }
 
    .fui-rate__not-allowed {
        /* #ifdef H5 */
        cursor: not-allowed !important;
        /* #endif */
    }
</style>