num-input.vue 4.0 KB

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