zhangwei
2024-08-14 9d8994790fe403935ed46ad478f83ae110bb7a01
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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID: 16 3,营业执照号:9   1510  13 133     200 6 1    93K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <text
        :class="[dot?'fui-badge__dot':'fui-badge__wrap',background?'':('fui-badge__bg-'+type),absolute?'fui-badge__absolute':'',scaleRatio!=1 && isNvue?'fui-badge__trans-origin':'',!background && type==='white'?'fui-badge__text-color':'']"
        :style="{top:absolute?top:'auto',right:absolute?right:'auto',zoom:scaleRatio,transform:isNvue?`scale(${scaleRatio})`:'scale(1)',marginTop:marginTop+'rpx',marginLeft:marginLeft+'rpx',marginRight:marginRight+'rpx',width:width,color:color,background:getBgColor}"
        @tap="handleClick" v-if="showValue || dot">{{dot?'':showValue}}</text>
</template>
 
<script>
    const sys = uni.getSystemInfoSync()
    export default {
        name: "fui-badge",
        emits: ['click'],
        props: {
            value: {
                type: [Number, String],
                default: ''
            },
            max: {
                type: [Number, String],
                default: -1
            },
            //类型:primary,success,warning,danger,purple,white
            type: {
                type: String,
                default: 'primary'
            },
            //背景色,如果设置背景则type失效
            background: {
                type: String,
                default: ''
            },
            //字体颜色
            color: {
                type: String,
                default: '#FFFFFF'
            },
            //是否显示为圆点
            dot: {
                type: Boolean,
                default: false
            },
            //margin-top值,单位rpx
            marginTop: {
                type: [Number, String],
                default: 0
            },
            //margin-left值,单位rpx
            marginLeft: {
                type: [Number, String],
                default: 0
            },
            //margin-right值,单位rpx
            marginRight: {
                type: [Number, String],
                default: 0
            },
            //是否绝对定位
            absolute: {
                type: Boolean,
                default: false
            },
            top: {
                type: String,
                default: '-8rpx'
            },
            right: {
                type: String,
                default: '-18rpx'
            },
            //缩放比例
            scaleRatio: {
                type: Number,
                default: 1
            }
        },
        data() {
            let isNvue = false;
            // #ifdef APP-NVUE
            isNvue = true;
            // #endif
            return {
                isNvue: isNvue,
                width: 0,
                showValue: ''
            };
        },
        computed: {
            getBgColor() {
                let color = this.background
                // #ifdef APP-NVUE
                if (!color && this.type) {
                    //primary,success,warning,danger,purple,white
                    const app = uni && uni.$fui && uni.$fui.color
                    const colors = {
                        primary: (app && app.primary) || '#465CFF',
                        success: (app && app.success) || '#09BE4F',
                        warning: (app && app.warning) || '#FFB703',
                        danger: (app && app.danger) || '#FF2B2B',
                        purple: (app && app.purple) || '#6831FF',
                        white: '#FFFFFF'
                    }
                    color = colors[this.type] || colors.primary
                }
                // #endif
                return color
            }
        },
        watch: {
            value(val) {
                this.getWidth()
            }
        },
        mounted() {
            this.getWidth()
        },
        methods: {
            _getTextWidth(text) {
                let sum = 0;
                for (let i = 0, len = text.length; i < len; i++) {
                    if (text.charCodeAt(i) >= 0 && text.charCodeAt(i) <= 255)
                        sum = sum + 1;
                    else
                        sum = sum + 2;
                }
                const px = uni.upx2px(text.length > 1 ? 32 : 24)
                var strCode = text.charCodeAt();
                let multiplier = 12;
                if (strCode >= 65 && strCode <= 90) {
                    multiplier = 15;
                }
                return (sum / 2 * multiplier) + px + 'px';
            },
            getWidth() {
                let max = Number(this.max)
                let val = Number(this.value)
                let value = ''
                if (isNaN(val) || max === -1) {
                    value = this.value
                } else {
                    value = val > max ? `${max}+` : val
                }
                this.showValue = value;
                this.width = this.dot ? '8px' : this._getTextWidth(String(value))
            },
            handleClick() {
                this.$emit('click');
            }
        }
    }
</script>
 
<style scoped>
    .fui-badge__wrap {
        height: 36rpx;
        color: #FFFFFF;
        font-size: 24rpx;
        line-height: 36rpx;
        border-radius: 100px;
        /* #ifndef APP-NVUE */
        min-width: 36rpx !important;
        display: flex;
        box-sizing: border-box;
        white-space: nowrap;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: center;
        text-align: center;
        z-index: 10;
    }
 
    .fui-badge__dot {
        height: 8px !important;
        width: 8px !important;
        /* #ifdef APP-NVUE */
        border-radius: 100px;
        /* #endif */
 
        /* #ifndef APP-NVUE */
        display: inline-block;
        /* #endif */
 
        /* #ifndef APP-NVUE */
        border-radius: 50%;
        /* #endif */
        z-index: 10;
    }
 
    /* #ifndef APP-NVUE */
    .fui-badge__bg-primary {
        background-color: var(--fui-color-primary, #465CFF) !important;
    }
 
    .fui-badge__bg-success {
        background-color: var(--fui-color-success, #09BE4F) !important;
    }
 
    .fui-badge__bg-warning {
        background-color: var(--fui-color-warning, #FFB703) !important;
    }
 
    .fui-badge__bg-danger {
        background-color: var(--fui-color-danger, #FF2B2B) !important;
    }
 
    .fui-badge__bg-purple {
        background-color: var(--fui-color-purple, #6831FF) !important;
    }
 
    .fui-badge__bg-white {
        background-color: var(--fui-color-white, #FFFFFF) !important;
    }
 
    .fui-badge__text-color {
        color: var(--fui-color-danger, #FF2B2B) !important;
    }
 
    /* #endif */
 
    /* #ifdef APP-NVUE */
    .fui-badge__text-color {
        color: #FF2B2B !important;
    }
 
    .fui-badge__trans-origin {
        transform-origin: center center;
    }
 
    /* #endif */
    .fui-badge__absolute {
        position: absolute;
    }
</style>