| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- /*******************************************************************************
- * Copyright(c) 2022 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:数字组件(支持千分位,金额符)
- * 2.功能描述:dk-number-input组件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 周兴 2022-6-30 1.00 新建
- *******************************************************************************/
- const common = require('../../../utils/common.js')
- Component({
- options: {
- addGlobalClass: true,
- },
- properties: {
- titleValue: {
- type: String,
- value: ''
- },
- required: Boolean,//是否显示表单必填星号
- requiredLeft: {
- type: String,
- value: '50rpx'
- },
- titleWidth: {
- type: String,
- value: '164rpx'
- },
- height:{
- type:String,
- value:'46rpx'
- },
- titleColor: {
- type: String,
- value: '#1B365D'
- },
- titleFontWeight: {
- type: String,
- value: 'normal'
- },
- inputColor: {
- type: String,
- value: '#1B365D'
- },
- readonly: {
- type: Boolean,
- value: false
- },
- code: {
- type: String,
- value: 'wyj',
- },
- // 内容是否居右,默认是false
- contentRight: {
- type: Boolean,
- value: false
- },
- /**
- * 输入的数据
- */
- inputValue: {
- type: Float32Array,
- value: 0,
- },
- /**
- * 是否支持千分位
- */
- formatThousandth: {
- type: Boolean,
- value: true
- },
- /**
- * 是否可用
- */
- disabled: {
- type: Boolean,
- value: false
- },
- /**
- * 金额符
- */
- sign: {
- type: String,
- value: '¥'
- },
- percent: {
- type: String,
- value: '%'
- },
- percentSignFlag: {
- type: Boolean,
- value: false
- },
- /**
- * 标识的大小
- */
- signSize: {
- type: String,
- value: ''
- },
- /**
- * 是否居中
- */
- center: {
- type: String,
- value: 'center'
- },
- /**
- * 小数位数
- */
- digits: {
- type: Number,
- value: 2
- },
- /**
- * 最大值
- */
- max: {
- type: Number,
- value: 10000000
- },
- /**
- * 最小值
- */
- min: {
- type: Number,
- value: 0
- },
- /**
- * 字体大小
- */
- fontSize: {
- type: Number,
- value: 13
- },
- inputWidthFlag: {
- type: Boolean,
- value: false
- },
- /**
- * 字体大小
- */
- fontWeight: {
- type: String,
- value: 'nomal'
- },
- /**
- * 是否是负数处理(输入整数,自动变为负数)
- */
- negative: {
- type: Boolean,
- value: false
- },
- /**
- * 提示信息
- */
- placeholder: {
- type: String,
- value: ''
- },
- maxFlag: {
- type: Boolean,
- value: true
- },
- minFlag: {
- type: Boolean,
- value: true
- },
- focus:{
- type: Boolean,
- value: false
- },
- focusCustom:{
- type: Boolean,
- value: false
- },
- dataIndex: {
- type: Number,
- },
- dataIndex2: {
- type: Number,
- },
- left: {
- type: String,
- value: '1rpx'
- },
- /**
- * 错误提示信息
- */
- errorMessage: {
- type: String,
- },
- /**
- * 提示信息
- */
- tip: {
- type: String,
- }
- },
- data: {
- // focus: false,
- displayValue: '',
- clearFlag: false,
- },
- /**
- * 组件的方法列表
- */
- methods: {
- /**
- * @desc : 显示tip
- * @author : 周兴
- * @date : 2024/4/25 11:46
- */
- showTip() {
- let tip = this.data.tip
- if (tip) {
- wx.showToast({
- title: tip,
- icon: 'none',
- duration: 2000
- })
- }
- },
- setShowTipFlag(flag) {
- let code = this.data.code
- if(this.selectComponent('#'+code)){
- this.selectComponent('#'+code).setShowTipFlag(false)
- }
- },
- clickTip(){
- this.triggerEvent("clickTip", { })
- },
- /**
- * @desc : 光标进入选中
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- handleInput(e) {
- this.triggerEvent('triggerOpen', {
- })
- if (this.data.readonly) {
- return
- }
- setTimeout(() => {
- this.setData({
- inputValue: parseFloat(this.data.inputValue),
- displayValue: this.data.inputValue,
- focus: true
- })
- }, 30)
-
- },
- /**
- * @desc : 光标进入输入框
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- bindFocus(e) {
- if (isNaN(this.data.inputValue)) {
- this.setData({
- inputValue: 0
- })
- }
- },
- /**
- * @desc : 输入时数据的处理
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- bindInput(e) {
- let inputValue = this.data.inputValue
- let minFlag = this.data.minFlag
- let min = this.data.min
- // 如果输入后让数据变成非数字,那么就要还原成之前的数据
- if (isNaN(e.detail)) {
- // 如果最小值在0之上 ,就不允许输入负数
- if (minFlag && min >= 0 && e.detail == '-' || e.detail != '-') {
- this.setData({
- inputValue: inputValue
- })
- return;
- }
- }
- let flag = true;
- let values = e.detail.split('.')
- // 如果输入的是小数点,那么需要判断是否已经有小数点
- if (e.detail.endsWith('.')) {
- // 说明有多个小数点
- if (values && values.length > 2) {
- flag = false;
- if ((this.data.inputValue + '').indexOf('.') >= 0) {
- return this.data.inputValue;
- } else {
- // 可能连续输入2个小数点
- return this.data.inputValue + '.';
- }
- }
- // 如果inputValue是空,那么当成0处理
- if (e.detail == '.') {
- return '0.';
- }
- }
- //控制小数点后数字的输入
- let digits = this.data.digits
- if (values && values.length === 2 && values[1].length > digits) {
- flag = false;
- this.setData({
- inputValue: inputValue
- })
- return this.data.inputValue;
- }
- if (flag) {
- let value = 0;
- let detailValue = e.detail
- //影响输入小数点 有问题!!!
- // 直接变为负
- // if (this.data.negative) {
- // detailValue = -1 * Math.abs(detailValue)
- // }
- // 判断是否超过最大值
- if (this.data.maxFlag) {
- if (detailValue > this.data.max) {
- value = this.data.max
- } else {
- value = detailValue
- }
- }
- else if (this.data.minFlag) {
- if (detailValue <= this.data.min) {
- value = this.data.min
- } else {
- value = detailValue
- }
- } else {
- value = detailValue
- }
- if (e.detail == '-') {
- value = e.detail
- }
- this.setData({
- clearFlag: false,
- inputValue: value,
- })
- if (e.detail != '-') {
- this.handleData()
- }
- }
- },
- /**
- * @desc : 光标离开处理数据的显示
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- bindBlur() {
- if (this.data.clearFlag) {
- this.setData({
- inputValue: 0,
- displayValue: 0,
- clearFlag: false
- })
- }
- // 处理数据
- if (this.data.inputValue) {
- this.setData({
- inputValue: Number(this.data.inputValue)
- })
- }
- // 离开的时候进行数据处理
- // this.handleDisplayValue();
- this.triggerEvent('triggerBindBlur', {
- inputValue: this.data.inputValue,
- index: this.data.dataIndex,
- index2: this.data.dataIndex2,
- })
- this.setData({
- focus: false
- })
- },
-
- /**
- * @desc : 绑定值
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- handleData() {
- let value = this.data.inputValue;
- if (value == null || value == '') {
- this.setData({
- clearFlag: true
- })
- value = 0
- }
- if (isNaN(value)) {
- value = 0
- }
- // 如果负数标识开启
- if (this.data.negative) {
- if (value > 0) {
- value = -1 * value;
- }
- } else {
- // if (value < 0) {
- // value = -1 * value;
- // }
- }
- this.triggerEvent('triggerBindValue', {
- value: value
- })
- },
- /**
- * @desc : 处理数据的显示
- * @author : 周兴
- * @date : 2022/6/30 16:16
- */
- handleDisplayValue() {
- let inputValue = this.data.inputValue;
- let displayValue = ''
- // if(inputValue != undefined && inputValue != 0){
- //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
- if (inputValue != undefined && inputValue && inputValue != 0) {
- // 判断是否有千分位
- if (this.data.formatThousandth) {
- displayValue = common.toThousandCents(inputValue);
- }
- // 如果是负数显示
- if (this.data.negative && displayValue.indexOf("-") < 0) {
- // displayValue = this.data.sign + "-" + displayValue;
- displayValue = "-" + displayValue;
- } else {
- // 显示金额符号
- // displayValue = this.data.sign + displayValue;
- }
- } else {
- displayValue = inputValue;
- }
- this.setData({
- displayValue: displayValue
- })
- },
- },
- /**
- * 组件生命周期
- */
- lifetimes: {
- attached: function () {
- //处理数据的显示
- this.handleDisplayValue();
- },
- detached: function () {
- // 在组件实例被从页面节点树移除时执行
- },
- },
- })
|