num-input.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="num-input">
  3. <view class="i l" @tap.stop="red">-</view>
  4. <view class="i c" @tap.stop="inputNum = value ? value : ''; showInput = true">{{ value }}</view>
  5. <view class="i r basecolor" @tap.stop="add">+</view>
  6. <view v-if="showInput" class="inputModelWrapper">
  7. <view class="inputModel">
  8. <view class="t">请输入数量</view>
  9. <view class="m">
  10. <input ref="input" focus type="number" maxlength="4" v-model="inputNum" />
  11. </view>
  12. <view class="b">
  13. <text class="l" @tap.stop="showInput = false">取消</text>
  14. <text class="r basecolor" @tap.stop="comfirmNum">确定</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. value: {
  24. type: Number,
  25. default: 0
  26. },
  27. num: { //限制下单数量
  28. type: Number,
  29. default: 0
  30. },
  31. args: {
  32. type: Array,
  33. default: () => []
  34. },
  35. max: {
  36. type: Number,
  37. default: 9999
  38. },
  39. },
  40. data() {
  41. return {
  42. showInput: false,
  43. inputNum: 0
  44. }
  45. },
  46. methods: {
  47. red() {
  48. if (this.value > 0) {
  49. // this.$emit('change', this.value - 1, ...this.args)
  50. // this.$emit('input', this.value - 1, ...this.args)
  51. // 限制下单数量
  52. this.$emit('change', this.value - this.num, ...this.args)
  53. this.$emit('input', this.value - this.num, ...this.args)
  54. }
  55. },
  56. add() {
  57. if (this.value + 1 > this.max) {
  58. uni.toast('数量不能再多了')
  59. } else {
  60. // this.$emit('change', this.value + 1, ...this.args)
  61. // this.$emit('input', this.value + 1, ...this.args)
  62. //限制下单数量
  63. this.$emit('change', this.value + this.num, ...this.args)
  64. this.$emit('input', this.value + this.num, ...this.args)
  65. }
  66. },
  67. comfirmNum() {
  68. //限制下单数量
  69. if (this.inputNum % this.num != 0) {
  70. uni.showModal({
  71. content: `请输入${this.num}的倍数`,
  72. showCancel: false
  73. })
  74. return
  75. }
  76. if (this.inputNum) {
  77. if (+this.inputNum > this.max) {
  78. this.$emit('change', this.max, ...this.args)
  79. this.$emit('input', this.max, ...this.args)
  80. } else {
  81. this.$emit('change', +this.inputNum, ...this.args)
  82. this.$emit('input', +this.inputNum, ...this.args)
  83. }
  84. } else {
  85. this.$emit('change', 0, ...this.args)
  86. this.$emit('input', 0, ...this.args)
  87. }
  88. this.showInput = false
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. .num-input {
  95. @include flex();
  96. width: 200rpx;
  97. height: 64rpx;
  98. overflow: hidden;
  99. font-size: 32rpx;
  100. background: #F2F2F2;
  101. border-radius: 64rpx;
  102. .i {
  103. flex: 1;
  104. height: 100%;
  105. @include flex();
  106. &.c {
  107. border-left: 4rpx solid #FFFFFF;
  108. border-right: 4rpx solid #FFFFFF;
  109. }
  110. }
  111. .inputModelWrapper {
  112. @include flex();
  113. top: 0;
  114. left: 0;
  115. z-index: 2;
  116. width: 100vw;
  117. height: 100vh;
  118. position: fixed;
  119. background: rgba(0, 0, 0, 0.3);
  120. .inputModel {
  121. width: 600rpx;
  122. height: 400rpx;
  123. background: #FFFFFF;
  124. border-radius: 8rpx;
  125. @include flex(column);
  126. .t {
  127. @include flex();
  128. width: 100%;
  129. height: 100rpx;
  130. border-bottom: 1rpx solid #CCCCCC;
  131. }
  132. .m {
  133. width: 100%;
  134. flex: 1;
  135. @include flex();
  136. input {
  137. width: 240rpx;
  138. height: 120rpx;
  139. text-align: center;
  140. font-size: 88rpx;
  141. border-radius: 8rpx;
  142. border: 1rpx solid #CCCCCC;
  143. }
  144. }
  145. .b {
  146. @include flex();
  147. width: 100%;
  148. height: 100rpx;
  149. border-top: 1rpx solid #CCCCCC;
  150. text {
  151. height: 100%;
  152. flex: 1;
  153. @include flex();
  154. &.basecolor {
  155. border-left: 1rpx solid #CCCCCC;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. </style>