index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <view class="jnpf-input-number">
  3. <!-- 数字输入 -->
  4. <view v-if="!detailed">
  5. <u-number-box v-if="controls" v-model="innerValue" :step="step" :min="min" :max="max" :key="key"
  6. :disabled="disabled" :positive-integer="false" :input-height="60" @blur="onNumberBlur"
  7. @change="onChange" />
  8. <view v-else class="input-content" :class="{'input-border':addonBefore||addonAfter}">
  9. <view class="input-left u-line-1" v-if="addonBefore">{{addonBefore}}</view>
  10. <view class="input-center">
  11. <u-input v-model="innerValue" :placeholder="placeholder" :class="{'input-disabled':disabled}"
  12. :input-align='addonBefore || addonAfter? "center":"right"' :disabled="disabled"
  13. :clearable="false" @focus="onFocus" @blur="onBlur" @input="onInputChange">
  14. </u-input>
  15. </view>
  16. <view class="input-right u-line-1" v-if="addonAfter">{{addonAfter}}</view>
  17. </view>
  18. </view>
  19. <!-- 详情 -->
  20. <view v-else>
  21. <view class="detail-content u-flex">
  22. <view class="detail-left u-line-1" v-if="addonBefore&&!controls">{{addonBefore}}</view>
  23. <view class="detail-center">{{thousands?jnpf.thousandsFormat(innerValue) :innerValue}}</view>
  24. <view class="detail-right u-line-1" v-if="addonAfter&&!controls">{{addonAfter}}</view>
  25. </view>
  26. </view>
  27. <!-- 大写金额 -->
  28. <view class="amount-chinese-name" v-if="isAmountChinese&&getChineseName">{{getChineseName}}</view>
  29. </view>
  30. </template>
  31. <script>
  32. /**
  33. * inputNumber 数字输入框
  34. * @modelValue v-model
  35. * @min {Number} 最小值
  36. * @max {Number} 最大值
  37. * @step {Number} 每次点击改变的间隔大小
  38. * @placeholder {String} 占位符
  39. * @addonBefore {String} 前缀
  40. * @addonAfter {String} 后缀
  41. * @thousands {Boolean} 金额千位符
  42. * @isAmountChinese {Boolean} 金额大写
  43. * @detailed {Boolean} 详情
  44. */
  45. export default {
  46. name: 'jnpf-input-number',
  47. props: {
  48. modelValue: {
  49. type: [Number, String],
  50. default: ''
  51. },
  52. min: {
  53. type: Number,
  54. default: -999999999999999
  55. },
  56. max: {
  57. type: Number,
  58. default: 999999999999999
  59. },
  60. step: {
  61. type: Number,
  62. default: 1
  63. },
  64. disabled: {
  65. type: Boolean,
  66. default: false
  67. },
  68. addonBefore: {
  69. default: ''
  70. },
  71. addonAfter: {
  72. default: ''
  73. },
  74. precision: {
  75. type: Number
  76. },
  77. controls: {
  78. type: Boolean,
  79. default: false
  80. },
  81. thousands: {
  82. type: Boolean,
  83. default: false
  84. },
  85. isAmountChinese: {
  86. type: Boolean,
  87. default: false
  88. },
  89. detailed: {
  90. type: Boolean,
  91. default: false
  92. },
  93. type: {
  94. default: ''
  95. },
  96. placeholder: {
  97. default: '请输入'
  98. },
  99. },
  100. data() {
  101. return {
  102. innerValue: null,
  103. key: +new Date()
  104. }
  105. },
  106. watch: {
  107. modelValue: {
  108. handler(val) {
  109. if (val == null || val == undefined) return;
  110. this.setValue(val)
  111. },
  112. immediate: true
  113. },
  114. getChineseName(val) {
  115. uni.$emit('initCollapse')
  116. }
  117. },
  118. computed: {
  119. getChineseName() {
  120. if (!this.isAmountChinese || (!this.getNumberValue && this.getNumberValue !== 0)) return ""
  121. return this.jnpf.getAmountChinese(this.getNumberValue)
  122. },
  123. getNumberValue() {
  124. return this.handleConvertNum(this.innerValue)
  125. },
  126. },
  127. methods: {
  128. setValue(val) {
  129. this.innerValue = (!val && val !== 0) || isNaN(val) ? null : Number(val);
  130. if (!this.innerValue && this.innerValue !== 0) return
  131. if (this.innerValue < this.min) this.innerValue = this.min
  132. if (this.innerValue > this.max) this.innerValue = this.max
  133. if (!isNaN(this.precision)) {
  134. const value = Number(this.getNumberValue).toFixed(this.precision)
  135. this.innerValue = this.controls ? Number(value) : value
  136. }
  137. if (this.thousands) this.innerValue = this.jnpf.thousandsFormat(this.innerValue)
  138. },
  139. onChange() {
  140. this.setValue(this.innerValue)
  141. this.$nextTick(() => {
  142. this.emitValueChanges(this.innerValue)
  143. })
  144. },
  145. emitValueChanges(value) {
  146. // 统一事件触发逻辑
  147. this.$emit('update:modelValue', value)
  148. this.$emit('change', value)
  149. },
  150. onInputChange() {
  151. this.$nextTick(() => {
  152. if (this.innerValue == null || this.innerValue == undefined) return;
  153. if (this.innerValue < this.min || this.innerValue > this.max) return;
  154. this.emitValueChanges(this.innerValue)
  155. })
  156. },
  157. onNumberBlur() {
  158. this.key = +new Date()
  159. this.setValue(this.innerValue)
  160. this.$emit('blur', this.innerValue)
  161. },
  162. onBlur(val) {
  163. this.setValue(this.getNumberValue)
  164. this.$nextTick(() => {
  165. if (this.getNumberValue > this.min && this.getNumberValue < this.max) return;
  166. this.$emit('blur', this.getNumberValue)
  167. this.emitValueChanges(this.getNumberValue)
  168. })
  169. },
  170. onFocus() {
  171. if (!this.innerValue) return
  172. if (this.innerValue.toString().indexOf('e+') > -1) return
  173. this.innerValue = !isNaN(this.precision) ? Number(this.getNumberValue).toFixed(this.precision) : this
  174. .getNumberValue
  175. },
  176. handleConvertNum(val) {
  177. if (!val && val !== 0) return null
  178. let num = this.$u.deepClone(val.toString().split("."))
  179. const arr2 = num.length > 1 ? num[1].split("").filter(o => (!isNaN(o))).join('') : []
  180. let arr = num[0].split("").filter(o => (!isNaN(o))).join('');
  181. let res = num[1] ? arr + '.' + arr2 : Number(arr)
  182. return val.toString().indexOf('-') != -1 ? Number('-' + res) : res
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .jnpf-input-number {
  189. width: 100%;
  190. display: flex;
  191. flex-direction: column;
  192. align-items: flex-end;
  193. :deep(.u-number-input) {
  194. width: 150rpx !important;
  195. }
  196. .input-content {
  197. display: flex;
  198. border-radius: 10rpx;
  199. height: 74rpx;
  200. &.input-border {
  201. border: 1rpx solid rgb(220, 223, 230)
  202. }
  203. .input-disabled {
  204. :deep(.uni-input-placeholder) {
  205. color: #9B9B9B !important;
  206. }
  207. :deep(.uni-input-input) {
  208. color: #9B9B9B !important;
  209. }
  210. }
  211. .input-left,
  212. .input-right {
  213. flex-shrink: 0;
  214. width: 128rpx;
  215. background-color: #f5f7fa;
  216. color: #909399;
  217. padding: 0 10rpx;
  218. text-align: center;
  219. }
  220. .input-left {
  221. border-right: 1rpx solid #dcdfe6;
  222. border-radius: 10rpx 0 0 10rpx;
  223. }
  224. .input-right {
  225. border-left: 1rpx solid #dcdfe6;
  226. border-radius: 0px 10px 10px 0px;
  227. }
  228. }
  229. .detail-content {
  230. .detail-left {
  231. max-width: 128rpx;
  232. padding-right: 16rpx;
  233. color: #909399;
  234. }
  235. .detail-right {
  236. max-width: 128rpx;
  237. padding-left: 16rpx;
  238. color: #909399;
  239. }
  240. }
  241. .amount-chinese-name {
  242. color: #999999;
  243. line-height: 40rpx;
  244. padding: 10rpx 10rpx 0 0;
  245. }
  246. }
  247. </style>