SkuStepper.js 4.1 KB

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