index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { use } from '../utils';
  2. import Checkbox from '../checkbox';
  3. var _use = use('coupon'),
  4. sfc = _use[0],
  5. bem = _use[1],
  6. t = _use[2];
  7. function padZero(num) {
  8. return (num < 10 ? '0' : '') + num;
  9. }
  10. function getDate(timeStamp) {
  11. var date = new Date(timeStamp * 1000);
  12. return date.getFullYear() + "." + padZero(date.getMonth() + 1) + "." + padZero(date.getDate());
  13. }
  14. function formatDiscount(discount) {
  15. return (discount / 10).toFixed(discount % 10 === 0 ? 0 : 1);
  16. }
  17. function formatAmount(amount) {
  18. return (amount / 100).toFixed(amount % 100 === 0 ? 0 : amount % 10 === 0 ? 1 : 2);
  19. }
  20. export default sfc({
  21. props: {
  22. coupon: Object,
  23. chosen: Boolean,
  24. disabled: Boolean,
  25. currency: {
  26. type: String,
  27. "default": '¥'
  28. }
  29. },
  30. computed: {
  31. validPeriod: function validPeriod() {
  32. return t('valid') + "\uFF1A" + getDate(this.coupon.startAt) + " - " + getDate(this.coupon.endAt);
  33. },
  34. faceAmount: function faceAmount() {
  35. var coupon = this.coupon;
  36. if (coupon.valueDesc) {
  37. return coupon.valueDesc + "<span>" + (coupon.unitDesc || '') + "</span>";
  38. }
  39. return coupon.denominations ? "<span>" + this.currency + "</span> " + formatAmount(this.coupon.denominations) : coupon.discount ? t('discount', formatDiscount(this.coupon.discount)) : '';
  40. },
  41. conditionMessage: function conditionMessage() {
  42. var condition = this.coupon.originCondition;
  43. condition = condition % 100 === 0 ? Math.round(condition / 100) : (condition / 100).toFixed(2);
  44. return condition === 0 ? t('unlimited') : t('condition', condition);
  45. }
  46. },
  47. render: function render(h) {
  48. var coupon = this.coupon,
  49. disabled = this.disabled;
  50. var description = disabled && coupon.reason || coupon.description;
  51. return h("div", {
  52. "class": bem({
  53. disabled: disabled
  54. })
  55. }, [h("div", {
  56. "class": bem('content')
  57. }, [h("div", {
  58. "class": bem('head')
  59. }, [h("h2", {
  60. "domProps": {
  61. "innerHTML": this.faceAmount
  62. }
  63. }), h("p", [this.coupon.condition || this.conditionMessage])]), h("div", {
  64. "class": bem('body')
  65. }, [h("h2", [coupon.name]), h("p", [this.validPeriod]), this.chosen && h(Checkbox, {
  66. "attrs": {
  67. "value": true
  68. },
  69. "class": bem('corner')
  70. })])]), description && h("p", {
  71. "class": bem('description')
  72. }, [description])]);
  73. }
  74. });