SkuStepper.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { use } from '../../utils';
  2. import Stepper from '../../stepper';
  3. import { LIMIT_TYPE } from '../constants';
  4. const [sfc] = use('sku-stepper');
  5. const { QUOTA_LIMIT, STOCK_LIMIT } = LIMIT_TYPE;
  6. export default sfc({
  7. props: {
  8. quota: Number,
  9. quotaUsed: Number,
  10. hideStock: Boolean,
  11. skuEventBus: Object,
  12. skuStockNum: Number,
  13. selectedSku: Object,
  14. selectedNum: Number,
  15. stepperTitle: String,
  16. hideQuotaText: Boolean,
  17. selectedSkuComb: Object,
  18. disableStepperInput: Boolean,
  19. customStepperConfig: Object
  20. },
  21. data() {
  22. return {
  23. currentNum: this.selectedNum,
  24. // 购买限制类型: 限购/库存
  25. limitType: STOCK_LIMIT
  26. };
  27. },
  28. watch: {
  29. currentNum(num) {
  30. this.skuEventBus.$emit('sku:numChange', num);
  31. },
  32. stepperLimit(limit) {
  33. if (limit < this.currentNum) {
  34. this.currentNum = limit;
  35. }
  36. }
  37. },
  38. computed: {
  39. stock() {
  40. const { stockNum } = this.customStepperConfig;
  41. if (stockNum !== undefined) {
  42. return stockNum;
  43. }
  44. if (this.selectedSkuComb) {
  45. return this.selectedSkuComb.stock_num;
  46. }
  47. return this.skuStockNum;
  48. },
  49. stockText() {
  50. const { stockFormatter } = this.customStepperConfig;
  51. if (stockFormatter) return stockFormatter(this.stock);
  52. return `剩余${this.stock}件`;
  53. },
  54. quotaText() {
  55. const { quotaText, hideQuotaText } = this.customStepperConfig;
  56. if (hideQuotaText) return '';
  57. let text = '';
  58. if (quotaText) {
  59. text = quotaText;
  60. } else if (this.quota > 0) {
  61. text = `每人限购${this.quota}件`;
  62. }
  63. return text;
  64. },
  65. stepperLimit() {
  66. const quotaLimit = this.quota - this.quotaUsed;
  67. let limit;
  68. // 无限购时直接取库存,有限购时取限购数和库存数中小的那个
  69. if (this.quota > 0 && quotaLimit <= this.stock) {
  70. // 修正负的limit
  71. limit = quotaLimit < 0 ? 0 : quotaLimit;
  72. this.limitType = QUOTA_LIMIT;
  73. } else {
  74. limit = this.stock;
  75. this.limitType = STOCK_LIMIT;
  76. }
  77. return limit;
  78. }
  79. },
  80. methods: {
  81. setCurrentNum(num) {
  82. this.currentNum = num;
  83. },
  84. onOverLimit(action) {
  85. this.skuEventBus.$emit('sku:overLimit', {
  86. action,
  87. limitType: this.limitType,
  88. quota: this.quota,
  89. quotaUsed: this.quotaUsed
  90. });
  91. },
  92. onChange(currentValue) {
  93. const { handleStepperChange } = this.customStepperConfig;
  94. handleStepperChange && handleStepperChange(currentValue);
  95. this.$emit('change', currentValue);
  96. }
  97. },
  98. render(h) {
  99. return (
  100. <div class="van-sku-stepper-stock">
  101. <div class="van-sku-stepper-container">
  102. <div class="van-sku__stepper-title">{this.stepperTitle || '购买数量'}:</div>
  103. <Stepper
  104. vModel={this.currentNum}
  105. class="van-sku__stepper"
  106. max={this.stepperLimit}
  107. disableInput={this.disableStepperInput}
  108. onOverlimit={this.onOverLimit}
  109. onChange={this.onChange}
  110. />
  111. </div>
  112. {!this.hideStock && <div class="van-sku__stock">{this.stockText}</div>}
  113. {!this.hideQuotaText && this.quotaText && (
  114. <div class="van-sku__quota">{this.quotaText}</div>
  115. )}
  116. </div>
  117. );
  118. }
  119. });