uni-fav.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ? contentText.contentFav : contentText.contentDefault }}</text>
  14. </view>
  15. </template>
  16. <script>
  17. import uniIcons from "../uni-icons/uni-icons.vue";
  18. export default {
  19. name: "UniFav",
  20. components: {
  21. uniIcons
  22. },
  23. props: {
  24. star: {
  25. type: [Boolean, String],
  26. default: true
  27. },
  28. bgColor: {
  29. type: String,
  30. default: "#eeeeee"
  31. },
  32. fgColor: {
  33. type: String,
  34. default: "#666666"
  35. },
  36. bgColorChecked: {
  37. type: String,
  38. default: "#007aff"
  39. },
  40. fgColorChecked: {
  41. type: String,
  42. default: "#FFFFFF"
  43. },
  44. circle: {
  45. type: [Boolean, String],
  46. default: false
  47. },
  48. checked: {
  49. type: Boolean,
  50. default: false
  51. },
  52. contentText: {
  53. type: Object,
  54. default () {
  55. return {
  56. contentDefault: "收藏",
  57. contentFav: "已收藏"
  58. };
  59. }
  60. }
  61. },
  62. watch: {
  63. checked() {
  64. if (uni.report) {
  65. if (this.checked) {
  66. uni.report("收藏", "收藏");
  67. } else {
  68. uni.report("取消收藏", "取消收藏");
  69. }
  70. }
  71. }
  72. },
  73. methods: {
  74. onClick() {
  75. this.$emit("click");
  76. }
  77. }
  78. };
  79. </script>
  80. <style lang="scss" scoped>
  81. $fav-height: 25px;
  82. .uni-fav {
  83. /* #ifndef APP-NVUE */
  84. display: flex;
  85. /* #endif */
  86. flex-direction: row;
  87. align-items: center;
  88. justify-content: center;
  89. width: 60px;
  90. height: $fav-height;
  91. line-height: $fav-height;
  92. text-align: center;
  93. border-radius: 3px;
  94. }
  95. .uni-fav--circle {
  96. border-radius: 30px;
  97. }
  98. .uni-fav-star {
  99. /* #ifndef APP-NVUE */
  100. display: flex;
  101. /* #endif */
  102. height: $fav-height;
  103. line-height: 24px;
  104. margin-right: 3px;
  105. align-items: center;
  106. justify-content: center;
  107. }
  108. .uni-fav-text {
  109. /* #ifndef APP-NVUE */
  110. display: flex;
  111. /* #endif */
  112. height: $fav-height;
  113. line-height: $fav-height;
  114. align-items: center;
  115. justify-content: center;
  116. font-size: $uni-font-size-base;
  117. }
  118. </style>