index.js 2.8 KB

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