index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="ui-unit-discount" :style="box_style" v-if="visible">
  3. <span class="bold">
  4. <!-- 类型1 -->
  5. <template v-if="type == 1">
  6. {{ value_parse }}%<br />
  7. <view>OFF</view>
  8. </template>
  9. <!-- 类型2 -->
  10. <template v-else>
  11. -{{ value_parse }}%
  12. </template>
  13. </span>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'unit-discount',
  19. props: {
  20. value: {
  21. default: 0
  22. },
  23. config: {
  24. type: Object,
  25. required: true
  26. }
  27. },
  28. computed: {
  29. /**
  30. * 控制是否展示
  31. * 1. 装修页强制展示
  32. * 2. 小于0隐藏
  33. */
  34. visible() {
  35. let visible = true;
  36. visible = (this.config.discount_show === undefined || this.config.discount_show === null) ? true : Number(
  37. this.config.discount_show) >= 1;
  38. if (Number(this.value) <= 0) {
  39. visible = false;
  40. }
  41. return visible;
  42. },
  43. /**
  44. * 折扣标的类型
  45. * @default 2
  46. * @example
  47. * 1 = **%OFF
  48. * 2 = -***%
  49. */
  50. type() {
  51. return this.config.discount_type || 1;
  52. },
  53. /**
  54. * 折扣标的右边距
  55. */
  56. right() {
  57. return 0;
  58. },
  59. /**
  60. * 折扣标的上边距
  61. */
  62. top() {
  63. return 0;
  64. },
  65. /**
  66. * 整体宽度
  67. */
  68. width() {
  69. return 40;
  70. },
  71. /**
  72. * 整体高度
  73. */
  74. height() {
  75. return 40;
  76. },
  77. // 折扣标的自定义样式
  78. style_body() {
  79. const style = {
  80. width: this.width + 'px',
  81. height: this.height + 'px',
  82. right: this.right + 'px',
  83. top: this.top + 'px',
  84. color: this.config.discount_font_color || '#fff'
  85. };
  86. if (this.config.discount_bg_image) {
  87. style['background-image'] = `url("${this.config.discount_bg_image}")`;
  88. style['border-radius'] = 0;
  89. } else {
  90. style['background-color'] = this.config.discount_bg_color || '#333333';
  91. }
  92. return style;
  93. },
  94. box_style() {
  95. if (this.config.discount_bg_image) {
  96. return `
  97. width: ${this.width} + 'px';
  98. height: ${this.height} + 'px';
  99. right: ${this.right} + 'px';
  100. top: ${this.top} + 'px';
  101. color:${this.config.discount_font_color};
  102. background-image:url("${this.config.discount_bg_image}");
  103. border-radius = 0;
  104. `;
  105. } else {
  106. return `
  107. width: ${this.width} + 'px';
  108. height: ${this.height} + 'px';
  109. right: ${this.right} + 'px';
  110. top: ${this.top} + 'px';
  111. color:${this.config.discount_font_color};
  112. background-color:${this.config.discount_bg_color};
  113. `;
  114. }
  115. },
  116. // 计算值,四舍五入
  117. value_parse() {
  118. const nval = Math.round(this.value);
  119. if (nval <= 0) {
  120. return 0;
  121. }
  122. if (nval >= 100) {
  123. return 100;
  124. }
  125. return nval;
  126. }
  127. }
  128. };
  129. </script>
  130. <style lang="less" scoped>
  131. // 浮动折扣标
  132. .ui-unit-discount {
  133. position: absolute;
  134. right: 0px;
  135. top: 0px;
  136. width: 40px;
  137. height: 40px;
  138. border-radius: 100%;
  139. overflow: hidden;
  140. z-index: 1;
  141. background-size: 100% 100%;
  142. display: flex;
  143. justify-content: center;
  144. align-items: center;
  145. text-align: center;
  146. >span {
  147. font-size: 12px;
  148. // line-height: 22 / 75rem;
  149. >view {
  150. font-size: 12px;
  151. font-style: normal;
  152. font-weight: 400;
  153. font-family: OpenSans-Regular, arial, serif;
  154. }
  155. }
  156. }
  157. </style>