uni-rate.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="rate-media">
  3. <view class="rate-media-body">
  4. <view class="rate-media-cell"
  5. v-for="(item,index) in max"
  6. :key="index"
  7. @click="clickStars(index)">
  8. <img :style="{'width': size + 'rpx','height':size+'rpx','margin':margin+'rpx'}" :src="valueSync>index?star_fill:star_empty"/>
  9. </view>
  10. </view>
  11. <view class="rate-media-info" v-if="is_score||is_attitude">
  12. <view v-if="is_score">{{is_infos_text()}}</view>
  13. <view v-if="is_attitude">{{rateScoreText}}</view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default{
  19. name:'rate',
  20. props:{
  21. star_fill: {
  22. //亮星星
  23. type: [String],
  24. default: 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/tmp/star.png'
  25. },
  26. star_empty: {
  27. //暗星星
  28. type: [String],
  29. default: 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/tmp/star1.png'
  30. },
  31. score:{
  32. type:Array,
  33. default:function(){
  34. return ['极差','差','一般','较好','非常好']
  35. }
  36. },
  37. is_score:{
  38. type: [Boolean, String],
  39. default: false
  40. },
  41. attitude:{
  42. type: Array,
  43. default:function(){
  44. return ['非常不满意,各方面都很差', '不满意,比较差', '一般,还需改善', '比较满意,仍可改善', '非常满意,无可挑剔']
  45. }
  46. },
  47. is_attitude:{
  48. type: [Boolean, String],
  49. default: false
  50. },
  51. size: {
  52. // 星星的大小
  53. type: [Number, String],
  54. default: 48
  55. },
  56. value: {
  57. // 当前评分
  58. type: [Number, String],
  59. default: 0
  60. },
  61. max: {
  62. // 最大评分
  63. type: [Number, String],
  64. default: 5
  65. },
  66. disabled: {
  67. // 是否可点击
  68. type: [Boolean, String],
  69. default: false
  70. },
  71. margin: {
  72. // 星星的间距
  73. type: [Number, String],
  74. default: '0 5'
  75. },
  76. },
  77. data() {
  78. return {
  79. valueSync: 0,
  80. rateScoreText:"",
  81. };
  82. },
  83. created() {
  84. this.valueSync = Number(this.value);
  85. },
  86. mounted() {
  87. this.valueSync = Number(this.value);
  88. },
  89. watch: {
  90.   value: {
  91.     handler(newValue, oldValue) {
  92.       //do ...
  93. this.valueSync = Number(this.value);
  94.     },
  95.     deep: true
  96.   }
  97. },
  98. methods: {
  99. clickStars(i){
  100. if (this.disabled) {
  101. return;
  102. }
  103. this.rateScoreText = this.attitude[i]||''
  104. this.valueSync = i+1
  105. this.$emit("change", {
  106. value: this.valueSync,
  107. attitude: this.attitude[i]||'',
  108. score: this.score[i]||''
  109. });
  110. },
  111. is_infos_text(){
  112. return this.score[this.valueSync-1||0]||''
  113. },
  114. // is_score_text(index){
  115. // return this.score[index]
  116. // }
  117. }
  118. }
  119. </script>
  120. <style lang="scss">
  121. .rate-media{
  122. display: flex;
  123. line-height: 1;
  124. justify-content: space-between;
  125. .rate-media-body{
  126. display: flex;
  127. }
  128. .rate-media-info{
  129. display: flex;
  130. align-items: center;
  131. color: #999;
  132. font-size: 30rpx;
  133. view:nth-child(1){
  134. margin:0 20rpx;
  135. }
  136. }
  137. }
  138. </style>