1
0

app-add-subtract.vue 4.5 KB

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