uni-fav.vue 3.8 KB

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