index.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // 赶进度待优化
  2. Component({
  3. properties: {
  4. value: {
  5. type: null,
  6. value: 0,
  7. observer() {
  8. this.updated()
  9. }
  10. },
  11. //最小数值
  12. min: {
  13. type: Number,
  14. value: -999999
  15. },
  16. //最大数值
  17. max: {
  18. type: Number,
  19. value: 999999,
  20. observer() {
  21. this.updated()
  22. }
  23. },
  24. //递增或递减步长
  25. step: {
  26. type: Number,
  27. value: 1
  28. },
  29. //是否禁用
  30. disabled: {
  31. type: Boolean,
  32. value: false
  33. },
  34. //是否禁用输入
  35. disabledInput: {
  36. type: Boolean,
  37. value: false
  38. },
  39. //是否整数
  40. integer: {
  41. type: Boolean,
  42. value: false
  43. }
  44. },
  45. data: {
  46. disabledMinus: false,
  47. disabledAdd: false
  48. },
  49. methods: {
  50. //数据更新
  51. updated() {
  52. const val = this.data.value
  53. if (val == '' || isNaN(val)) return
  54. if (val <= this.data.min) {
  55. this.setData({
  56. disabledMinus: true,
  57. value: this.data.min
  58. })
  59. } else {
  60. this.setData({
  61. disabledMinus: false
  62. })
  63. }
  64. if (val >= this.data.max) {
  65. this.setData({
  66. disabledAdd: true,
  67. value: this.data.max
  68. })
  69. } else {
  70. this.setData({
  71. disabledAdd: false
  72. })
  73. }
  74. },
  75. //实时监听input输入
  76. inputChange(e: WechatMiniprogram.Input) {
  77. this.setData({
  78. value: e.detail.value
  79. })
  80. },
  81. //计算精度
  82. fixedFloat(a: number, b: number, sign: string) {
  83. const handle = (x: number): [string, number] => {
  84. let y = String(x)
  85. let p = y.lastIndexOf('.')
  86. if (p === -1) {
  87. return [y, 0]
  88. } else {
  89. return [y.replace('.', ''), y.length - p - 1]
  90. }
  91. }
  92. // v 操作数1, w 操作数2, s 操作符, t 精度
  93. const operate = (v: number, w: number, s: string, t: number) => {
  94. switch (s) {
  95. case '+':
  96. return (v + w) / t
  97. case '-':
  98. return (v - w) / t
  99. case '*':
  100. return (v * w) / (t * t)
  101. case '/':
  102. return (v / w)
  103. default:
  104. return 0
  105. }
  106. }
  107. const padding0 = (p: number) => {
  108. var z = ''
  109. while (p--) {
  110. z += '0'
  111. }
  112. return z
  113. }
  114. const c: [string, number] = handle(a)
  115. const d: [string, number] = handle(b)
  116. let k = 0
  117. if (c[1] === 0 && d[1] === 0) {
  118. return operate(+c[0], +d[0], sign, 1)
  119. } else {
  120. k = Math.pow(10, Math.max(c[1], d[1]))
  121. if (c[1] !== d[1]) {
  122. if (c[1] > d[1]) {
  123. d[0] += padding0(c[1] - d[1])
  124. } else {
  125. c[0] += padding0(d[1] - c[1])
  126. }
  127. }
  128. return operate(+c[0], +d[0], sign, k)
  129. }
  130. },
  131. minus() {
  132. this.setData({
  133. value: this.fixedFloat(this.data.value, this.data.step, '-')
  134. }, () => {
  135. this.triggerEvent('change', this.data.value)
  136. })
  137. },
  138. add() {
  139. this.setData({
  140. value: this.fixedFloat(this.data.value, this.data.step, '+')
  141. }, () => {
  142. this.triggerEvent('change', this.data.value)
  143. })
  144. },
  145. //失去焦点
  146. inputBlur() {
  147. if (this.data.value === '' || isNaN(this.data.value)) {
  148. if (this.data.min > 0) {
  149. this.setData({
  150. value: this.data.min
  151. })
  152. } else {
  153. this.setData({
  154. value: this.data.max < 0 ? this.data.max : 0
  155. })
  156. }
  157. } else {
  158. if (this.data.integer) {
  159. this.setData({
  160. value: parseInt(this.data.value)
  161. })
  162. }
  163. }
  164. this.triggerEvent('change', this.data.value)
  165. }
  166. }
  167. })