Files
2026-02-07 23:14:57 +08:00

119 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Component({
properties: {
value: {
type: String,
optionalTypes: [Number],
value: '',
observer(val) {
this.getWidth()
}
},
max:{
type: String,
optionalTypes: [Number],
value: -1
},
//类型primarysuccesswarningdangerpurplewhite
type: {
type: String,
value: 'primary'
},
//背景色如果设置背景则type失效
background: {
type: String,
value: ''
},
//字体颜色
color: {
type: String,
value: '#FFFFFF'
},
//是否显示为圆点
dot: {
type: Boolean,
value: false
},
//margin-top值单位rpx
marginTop: {
type: String,
optionalTypes: [Number],
value: 0
},
//margin-left值单位rpx
marginLeft: {
type: String,
optionalTypes: [Number],
value: 0
},
//margin-right值单位rpx
marginRight: {
type: String,
optionalTypes: [Number],
value: 0
},
//是否绝对定位
absolute: {
type: Boolean,
value: false
},
top: {
type: String,
value: '-8rpx'
},
right: {
type: String,
value: '-18rpx'
},
//缩放比例
scaleRatio: {
type: Number,
value: 1
}
},
data: {
width: 0,
showValue: ''
},
lifetimes: {
ready() {
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 sys = wx.getSystemInfoSync()
const px = sys.windowWidth / 750 * (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.data.max)
let val = Number(this.data.value)
let value = ''
if (val === NaN || max === -1) {
value = this.data.value
} else {
value = val > max ? `${max}+` : val
}
let width = this.data.dot ? '8px' : this._getTextWidth(String(value))
this.setData({
showValue:value,
width: width
})
},
handleClick() {
this.triggerEvent('click');
}
}
})