uni-fav.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view :class="[circle === true || circle === 'true' ? 'uni-fav--circle' : '']" :style="[{ backgroundColor: checked ? bgColorChecked : bgColor }]"
  3. @click="onClick" class="uni-fav">
  4. <!-- #ifdef MP-ALIPAY -->
  5. <view class="uni-fav-star" v-if="!checked && (star === true || star === 'true')">
  6. <uni-icons :color="fgColor" :style="{color: checked ? fgColorChecked : fgColor}" size="14" type="star-filled" />
  7. </view>
  8. <!-- #endif -->
  9. <!-- #ifndef MP-ALIPAY -->
  10. <uni-icons :color="fgColor" :style="{color: checked ? fgColorChecked : fgColor}" class="uni-fav-star" size="14" type="star-filled"
  11. v-if="!checked && (star === true || star === 'true')" />
  12. <!-- #endif -->
  13. <text :style="{color: checked ? fgColorChecked : fgColor}" class="uni-fav-text">{{ checked ? contentFav : contentDefault }}</text>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * Fav 收藏按钮
  19. * @description 用于收藏功能,可点击切换选中、不选中的状态
  20. * @tutorial https://ext.dcloud.net.cn/plugin?id=864
  21. * @property {Boolean} star = [true|false] 按钮是否带星星
  22. * @property {String} bgColor 未收藏时的背景色
  23. * @property {String} bgColorChecked 已收藏时的背景色
  24. * @property {String} fgColor 未收藏时的文字颜色
  25. * @property {String} fgColorChecked 已收藏时的文字颜色
  26. * @property {Boolean} circle = [true|false] 是否为圆角
  27. * @property {Boolean} checked = [true|false] 是否为已收藏
  28. * @property {Object} contentText = [true|false] 收藏按钮文字
  29. * @property {Boolean} stat 是否开启统计功能
  30. * @event {Function} click 点击 fav按钮触发事件
  31. * @example <uni-fav :checked="true"/>
  32. */
  33. import {
  34. initVueI18n
  35. } from '@dcloudio/uni-i18n'
  36. import messages from './i18n/index.js'
  37. const { t } = initVueI18n(messages)
  38. export default {
  39. name: "UniFav",
  40. // TODO 兼容 vue3,需要注册事件
  41. emits: ['click'],
  42. props: {
  43. star: {
  44. type: [Boolean, String],
  45. default: true
  46. },
  47. bgColor: {
  48. type: String,
  49. default: "#eeeeee"
  50. },
  51. fgColor: {
  52. type: String,
  53. default: "#666666"
  54. },
  55. bgColorChecked: {
  56. type: String,
  57. default: "#007aff"
  58. },
  59. fgColorChecked: {
  60. type: String,
  61. default: "#FFFFFF"
  62. },
  63. circle: {
  64. type: [Boolean, String],
  65. default: false
  66. },
  67. checked: {
  68. type: Boolean,
  69. default: false
  70. },
  71. contentText: {
  72. type: Object,
  73. default () {
  74. return {
  75. contentDefault: "",
  76. contentFav: ""
  77. };
  78. }
  79. },
  80. stat:{
  81. type: Boolean,
  82. default: false
  83. }
  84. },
  85. computed: {
  86. contentDefault() {
  87. return this.contentText.contentDefault || t("uni-fav.collect")
  88. },
  89. contentFav() {
  90. return this.contentText.contentFav || t("uni-fav.collected")
  91. },
  92. },
  93. watch: {
  94. checked() {
  95. if (uni.report && this.stat) {
  96. if (this.checked) {
  97. uni.report("收藏", "收藏");
  98. } else {
  99. uni.report("取消收藏", "取消收藏");
  100. }
  101. }
  102. }
  103. },
  104. methods: {
  105. onClick() {
  106. this.$emit("click");
  107. }
  108. }
  109. };
  110. </script>
  111. <style lang="scss" >
  112. $fav-height: 25px;
  113. .uni-fav {
  114. /* #ifndef APP-NVUE */
  115. display: flex;
  116. /* #endif */
  117. flex-direction: row;
  118. align-items: center;
  119. justify-content: center;
  120. width: 60px;
  121. height: $fav-height;
  122. line-height: $fav-height;
  123. text-align: center;
  124. border-radius: 3px;
  125. /* #ifdef H5 */
  126. cursor: pointer;
  127. /* #endif */
  128. }
  129. .uni-fav--circle {
  130. border-radius: 30px;
  131. }
  132. .uni-fav-star {
  133. /* #ifndef APP-NVUE */
  134. display: flex;
  135. /* #endif */
  136. height: $fav-height;
  137. line-height: 24px;
  138. margin-right: 3px;
  139. align-items: center;
  140. justify-content: center;
  141. }
  142. .uni-fav-text {
  143. /* #ifndef APP-NVUE */
  144. display: flex;
  145. /* #endif */
  146. height: $fav-height;
  147. line-height: $fav-height;
  148. align-items: center;
  149. justify-content: center;
  150. font-size: 12px;
  151. }
  152. </style>