index.js 3.1 KB

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