index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 _constant = require("../utils/constant");
  7. var _string = require("../utils/format/string");
  8. var _checkbox = _interopRequireDefault(require("../checkbox"));
  9. var _createNamespace = (0, _utils.createNamespace)('coupon'),
  10. createComponent = _createNamespace[0],
  11. bem = _createNamespace[1],
  12. t = _createNamespace[2];
  13. function formatTimeStamp(timeStamp) {
  14. // compatible when the timestamp is seconds
  15. if (timeStamp < Math.pow(10, 12)) {
  16. return timeStamp * 1000;
  17. }
  18. return +timeStamp;
  19. }
  20. function getDate(timeStamp) {
  21. var date = new Date(formatTimeStamp(timeStamp));
  22. return date.getFullYear() + "." + (0, _string.padZero)(date.getMonth() + 1) + "." + (0, _string.padZero)(date.getDate());
  23. }
  24. function formatDiscount(discount) {
  25. return (discount / 10).toFixed(discount % 10 === 0 ? 0 : 1);
  26. }
  27. function formatAmount(amount) {
  28. return (amount / 100).toFixed(amount % 100 === 0 ? 0 : amount % 10 === 0 ? 1 : 2);
  29. }
  30. var _default = createComponent({
  31. props: {
  32. coupon: Object,
  33. chosen: Boolean,
  34. disabled: Boolean,
  35. currency: {
  36. type: String,
  37. default: '¥'
  38. }
  39. },
  40. computed: {
  41. validPeriod: function validPeriod() {
  42. var _this$coupon = this.coupon,
  43. startAt = _this$coupon.startAt,
  44. endAt = _this$coupon.endAt,
  45. customValidPeriod = _this$coupon.customValidPeriod;
  46. return customValidPeriod || getDate(startAt) + " - " + getDate(endAt);
  47. },
  48. faceAmount: function faceAmount() {
  49. var coupon = this.coupon;
  50. if (coupon.valueDesc) {
  51. return coupon.valueDesc + "<span>" + (coupon.unitDesc || '') + "</span>";
  52. }
  53. if (coupon.denominations) {
  54. var denominations = formatAmount(coupon.denominations);
  55. return "<span>" + this.currency + "</span> " + denominations;
  56. }
  57. if (coupon.discount) {
  58. return t('discount', formatDiscount(coupon.discount));
  59. }
  60. return '';
  61. },
  62. conditionMessage: function conditionMessage() {
  63. var condition = formatAmount(this.coupon.originCondition);
  64. return condition === '0' ? t('unlimited') : t('condition', condition);
  65. }
  66. },
  67. render: function render() {
  68. var h = arguments[0];
  69. var coupon = this.coupon,
  70. disabled = this.disabled;
  71. var description = disabled && coupon.reason || coupon.description;
  72. return h("div", {
  73. "class": bem({
  74. disabled: disabled
  75. })
  76. }, [h("div", {
  77. "class": bem('content')
  78. }, [h("div", {
  79. "class": bem('head')
  80. }, [h("h2", {
  81. "class": bem('amount'),
  82. "domProps": {
  83. "innerHTML": this.faceAmount
  84. }
  85. }), h("p", {
  86. "class": bem('condition')
  87. }, [this.coupon.condition || this.conditionMessage])]), h("div", {
  88. "class": bem('body')
  89. }, [h("p", {
  90. "class": bem('name')
  91. }, [coupon.name]), h("p", {
  92. "class": bem('valid')
  93. }, [this.validPeriod]), !this.disabled && h(_checkbox.default, {
  94. "attrs": {
  95. "size": 18,
  96. "value": this.chosen,
  97. "checkedColor": _constant.RED
  98. },
  99. "class": bem('corner')
  100. })])]), description && h("p", {
  101. "class": bem('description')
  102. }, [description])]);
  103. }
  104. });
  105. exports.default = _default;