BAOLH
2026-11-10 4d730af0447c686133130d0116a0fa3dd82c2dca
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
<template>
  <div>
    <!-- 金额标准输入框 -->
    <el-input
      ref="value"
      v-model="value"
      type="number"
      :style="{width: width + 'px'}"
      :class="{'web-display-none': showDisplay, 'web-display-inline': !showDisplay && !showCh, 'web-display-inline-table': !showDisplay && showCh}"
      :disabled="disabled"
      @blur="moneyBlur($event.target.value)"
    >
      <template v-if="showCh" slot="append">{{ customChinese }}</template>
    </el-input>
    <!-- 金额格式输入框 -->
    <el-input
      v-model="valueStr"
      :style="{width: width + 'px'}"
      :class="{'web-display-none': !showDisplay, 'web-display-inline': showDisplay && !showCh, 'web-display-inline-table': showDisplay && showCh}"
      :disabled="disabled"
      @focus="moneyFocus($event.target.value)"
    >
      <template v-if="showCh" slot="append">{{ customChinese }}</template>
    </el-input>
  </div>
</template>
 
<script>
import Nzh from 'nzh'
 
export default {
  name: 'AssemblyMoneyInput',
  model: {
    // 指向props的参数名
    prop: 'modelVal',
    // 事件名称
    event: 'change'
  },
  props: {
    modelVal: {
      type: Number,
      default() {
        return null
      }
    },
    width: {
      type: Number,
      default() {
        return 200
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    },
    showCh: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      // 金额输入框显示状态
      showDisplay: false,
      // 金额标准输入框数据
      value: null,
      // 金额格式输入框数据
      valueStr: null
    }
  },
  computed: {
    customNzh() {
      return new Nzh({
        ch: '零一二三四五六七八九', // 数字字符
        ch_u: '个十百千万亿兆京', // 数位单位
        ch_f: '负', // 负号
        ch_d: '点', // 小数点
        m_u: '元角分厘', // 金额单位
        m_t: '人民币', // 金额前缀
        m_z: '整' // 无小数后缀
      })
    },
    customChinese() {
      // 使用自定义配置进行转换
      if (this.value === null || this.value === undefined) {
        return ''
      }
      return this.customNzh.toMoney(this.value, { outSymbol: false })
    }
  },
  watch: {
    // 监听金额标准输入框数据变化:赋值给modelVal
    value(val) {
      this.$emit('change', val !== null && val !== undefined ? Number(val) : null)
      // 回调钩子
      this.$emit('afterChange')
    }
  },
  mounted() {
    // 金额标准输入框初始化
    this.value = this.modelVal
    // 金额输入框显示初始化
    this.showDisplay = false
    // 判断是否有初始金额
    if (this.value != null) {
      // 金额标准输入框失去焦点触发
      this.moneyBlur(this.value)
    }
  },
  methods: {
    // 金额标准输入框失去焦点触发
    moneyBlur(el) {
      // 金额输入框显示状态切换
      this.showDisplay = true
      // 金额格式化
      this.getInputValue(el)
      // 函数回调
      this.$emit('blur')
    },
    // 金额格式输入框获取焦点触发
    moneyFocus(el) {
      // 金额输入框显示状态切换
      this.showDisplay = false
      // 自动获取焦点
      this.$nextTick(() => {
        this.$refs.value.focus()
      })
    },
    // 金额格式化
    getInputValue(el) {
      // 金额
      const inputVal = el !== null && el !== undefined ? el.toString() : ''
      if (inputVal.trim() !== '') {
        // 金额添加千分位(金额去除千分位(金额))
        this.comdify(this.delcommafy(inputVal))
      } else {
        // 清空金额
        this.valueStr = null
        this.value = null
      }
    },
    // 金额去除千分位
    delcommafy(num) {
      if (!num) return num
      num = num.toString()
      num = num.replace(/,/gi, '')
      return num
    },
    // 金额添加千分位
    comdify(n) {
      if (!n) return n
      const str = n.split('.')
      const re = /\d{1,3}(?=(\d{3})+$)/g
      const n1 = str[0].replace(re, '$&,')
      // 金额格式输入框数据
      this.valueStr = str.length > 1 && str[1].length > 2 ? `${n1}.${str[1].substring(0, 2)}` : str.length > 1 && str[1].length > 1 ? `${n1}.${str[1]}` : str.length > 1 && str[1].length ? `${n1}.${str[1]}0` : `${n1}.00`
      // 金额标准输入框数据
      this.value = str.length > 1 && str[1].length > 2 ? Number(`${str[0]}.${str[1].substring(0, 2)}`) : str.length > 1 && str[1].length > 1 ? Number(`${str[0]}.${str[1]}`) : str.length > 1 && str[1].length ? Number(`${str[0]}.${str[1]}0`) : Number(`${str[0]}.00`)
    }
  }
}
</script>
 
<style scoped>
.web-display-none {
  display: none !important;
}
.web-display-inline {
  display: inline-block !important;
}
.web-display-inline-table {
  display: inline-table !important;
}
</style>