app-add-subtract.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="app-add-subtract dir-left-nowrap">
  3. <view class="app-icon" @click.stop="_calcValue('minus')" :class="{'app-unreducible': inputValue == 1, 'app-can-be-reduced': inputValue > 1}"></view>
  4. <view class="app-value">
  5. <input v-model="inputValue" @blur.stop="_onBlur" type="number">
  6. </view>
  7. <view class="app-icon" @click.stop="_calcValue('plus')" :class="{'app-not-add': inputValue >= stock, 'app-can-add': inputValue < stock}"></view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'app-add-subtract',
  13. data() {
  14. return {
  15. inputValue: 0,
  16. step: 1,
  17. disabled: false,
  18. }
  19. },
  20. props: {
  21. value: {
  22. type: Number,
  23. default() {
  24. return 1;
  25. }
  26. },
  27. stock: {
  28. type: Number,
  29. default() {
  30. return 0;
  31. }
  32. },
  33. good_id: Number,
  34. },
  35. created() {
  36. this.inputValue = +this.value;
  37. },
  38. methods: {
  39. _calcValue(type) {
  40. if (this.disabled) {
  41. return;
  42. }
  43. const scale = this._getDecimalScale();
  44. let value = this.inputValue * scale;
  45. let step = this.step * scale;
  46. if (type === "minus") {
  47. value -= step;
  48. } else if (type === "plus") {
  49. value += step;
  50. }
  51. if (value < 1 || value > this.stock) {
  52. return;
  53. }
  54. this.inputValue = String(value / scale);
  55. },
  56. _getDecimalScale() {
  57. let scale = 1;
  58. // 浮点型
  59. if (~~this.step !== this.step) {
  60. scale = Math.pow(10, (this.step + "").split(".")[1].length);
  61. }
  62. return scale;
  63. },
  64. _onBlur(event) {
  65. let value = event.detail.value;
  66. if (!value) {
  67. // this.inputValue = 0;
  68. return;
  69. }
  70. value = +value;
  71. if (value > this.stock) {
  72. value = this.stock;
  73. } else if (value < 1) {
  74. value = 1;
  75. }
  76. this.inputValue = value;
  77. }
  78. },
  79. watch: {
  80. value: {
  81. handler(val) {
  82. this.inputValue = +val;
  83. }
  84. },
  85. inputValue(newVal, oldVal) {
  86. if (+newVal !== +oldVal) {
  87. this.$emit("change", newVal, this.good_id);
  88. }
  89. }
  90. }
  91. }
  92. </script>
  93. <style scoped lang="scss">
  94. .app-add-subtract {
  95. height: #{60rpx};
  96. .app-icon {
  97. height: #{60rpx};
  98. width: #{60rpx};
  99. background-size: 100% 100%;
  100. background-repeat: no-repeat;
  101. background-color: #f7f7f7;
  102. }
  103. .app-unreducible {
  104. background-image: url("../../../../static/image/cart/unreducible.png");
  105. }
  106. .app-not-add {
  107. background-image: url("../../../../static/image/cart/cant-add.png");
  108. }
  109. .app-can-add {
  110. background-image: url("../../../../static/image/cart/can-be-added.png");
  111. }
  112. .app-can-be-reduced {
  113. background-image: url("../../../../static/image/cart/can-be-reduced.png");
  114. }
  115. .app-value {
  116. height: #{60rpx};
  117. width: #{88rpx};
  118. input {
  119. height: #{60rpx};
  120. width: #{88rpx};
  121. font-size: #{21rpx};
  122. color: #353535;
  123. background-color: #f7f7f7;
  124. text-align: center;
  125. }
  126. }
  127. }
  128. </style>