uni-fav.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * @event {Function} click 点击 fav按钮触发事件
  30. * @example <uni-fav :checked="true"/>
  31. */
  32. import {
  33. initVueI18n
  34. } from '@dcloudio/uni-i18n'
  35. import messages from './i18n/index.js'
  36. const { t } = initVueI18n(messages)
  37. export default {
  38. name: "UniFav",
  39. // TODO 兼容 vue3,需要注册事件
  40. emits: ['click'],
  41. props: {
  42. star: {
  43. type: [Boolean, String],
  44. default: true
  45. },
  46. bgColor: {
  47. type: String,
  48. default: "#eeeeee"
  49. },
  50. fgColor: {
  51. type: String,
  52. default: "#666666"
  53. },
  54. bgColorChecked: {
  55. type: String,
  56. default: "#007aff"
  57. },
  58. fgColorChecked: {
  59. type: String,
  60. default: "#FFFFFF"
  61. },
  62. circle: {
  63. type: [Boolean, String],
  64. default: false
  65. },
  66. checked: {
  67. type: Boolean,
  68. default: false
  69. },
  70. contentText: {
  71. type: Object,
  72. default () {
  73. return {
  74. contentDefault: "",
  75. contentFav: ""
  76. };
  77. }
  78. }
  79. },
  80. computed: {
  81. contentDefault() {
  82. return this.contentText.contentDefault || t("uni-fav.collect")
  83. },
  84. contentFav() {
  85. return this.contentText.contentFav || t("uni-fav.collected")
  86. },
  87. },
  88. watch: {
  89. checked() {
  90. if (uni.report) {
  91. if (this.checked) {
  92. uni.report("收藏", "收藏");
  93. } else {
  94. uni.report("取消收藏", "取消收藏");
  95. }
  96. }
  97. }
  98. },
  99. methods: {
  100. onClick() {
  101. this.$emit("click");
  102. }
  103. }
  104. };
  105. </script>
  106. <style lang="scss" scoped>
  107. $fav-height: 25px;
  108. .uni-fav {
  109. /* #ifndef APP-NVUE */
  110. display: flex;
  111. /* #endif */
  112. flex-direction: row;
  113. align-items: center;
  114. justify-content: center;
  115. width: 60px;
  116. height: $fav-height;
  117. line-height: $fav-height;
  118. text-align: center;
  119. border-radius: 3px;
  120. /* #ifdef H5 */
  121. cursor: pointer;
  122. /* #endif */
  123. }
  124. .uni-fav--circle {
  125. border-radius: 30px;
  126. }
  127. .uni-fav-star {
  128. /* #ifndef APP-NVUE */
  129. display: flex;
  130. /* #endif */
  131. height: $fav-height;
  132. line-height: 24px;
  133. margin-right: 3px;
  134. align-items: center;
  135. justify-content: center;
  136. }
  137. .uni-fav-text {
  138. /* #ifndef APP-NVUE */
  139. display: flex;
  140. /* #endif */
  141. height: $fav-height;
  142. line-height: $fav-height;
  143. align-items: center;
  144. justify-content: center;
  145. font-size: $uni-font-size-base;
  146. }
  147. </style>