SkuStepper.js 3.9 KB

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