uni-rate.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view>
  3. <view ref="uni-rate" class="uni-rate">
  4. <view class="uni-rate__icon" :class="{'uni-cursor-not-allowed': disabled}"
  5. :style="{ 'margin-right': marginNumber + 'px' }" v-for="(star, index) in stars" :key="index"
  6. @touchstart.stop="touchstart" @touchmove.stop="touchmove" @mousedown.stop="mousedown"
  7. @mousemove.stop="mousemove" @mouseleave="mouseleave">
  8. <uni-icons :color="color" :size="size" :type="isFill ? 'star-filled' : 'star'" />
  9. <!-- #ifdef APP-NVUE -->
  10. <view :style="{ width: star.activeWitch.replace('%','')*size/100+'px'}" class="uni-rate__icon-on">
  11. <uni-icons style="text-align: left;" :color="disabled?'#ccc':activeColor" :size="size"
  12. type="star-filled" />
  13. </view>
  14. <!-- #endif -->
  15. <!-- #ifndef APP-NVUE -->
  16. <view :style="{ width: star.activeWitch}" class="uni-rate__icon-on">
  17. <uni-icons :color="disabled?disabledColor:activeColor" :size="size" type="star-filled" />
  18. </view>
  19. <!-- #endif -->
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. /**
  26. * Rate 评分
  27. * @description 评分组件
  28. * @tutorial https://ext.dcloud.net.cn/plugin?id=33
  29. * @property {Boolean} isFill = [true|false] 星星的类型,是否为实心类型, 默认为实心
  30. * @property {String} color 未选中状态的星星颜色,默认为 "#ececec"
  31. * @property {String} activeColor 选中状态的星星颜色,默认为 "#ffca3e"
  32. * @property {String} disabledColor 禁用状态的星星颜色,默认为 "#c0c0c0"
  33. * @property {Number} size 星星的大小
  34. * @property {Number} value/v-model 当前评分
  35. * @property {Number} max 最大评分评分数量,目前一分一颗星
  36. * @property {Number} margin 星星的间距,单位 px
  37. * @property {Boolean} disabled = [true|false] 是否为禁用状态,默认为 false
  38. * @property {Boolean} readonly = [true|false] 是否为只读状态,默认为 false
  39. * @property {Boolean} allowHalf = [true|false] 是否实现半星,默认为 false
  40. * @property {Boolean} touchable = [true|false] 是否支持滑动手势,默认为 true
  41. * @event {Function} change uniRate 的 value 改变时触发事件,e={value:Number}
  42. */
  43. export default {
  44. name: "UniRate",
  45. props: {
  46. isFill: {
  47. // 星星的类型,是否镂空
  48. type: [Boolean, String],
  49. default: true
  50. },
  51. color: {
  52. // 星星未选中的颜色
  53. type: String,
  54. default: "#ececec"
  55. },
  56. activeColor: {
  57. // 星星选中状态颜色
  58. type: String,
  59. default: "#FEC823"
  60. },
  61. disabledColor: {
  62. // 星星禁用状态颜色
  63. type: String,
  64. default: "#c0c0c0"
  65. },
  66. size: {
  67. // 星星的大小
  68. type: [Number, String],
  69. default: 20
  70. },
  71. value: {
  72. // 当前评分
  73. type: [Number, String],
  74. default: 0
  75. },
  76. modelValue: {
  77. // 当前评分
  78. type: [Number, String],
  79. default: 0
  80. },
  81. max: {
  82. // 最大评分
  83. type: [Number, String],
  84. default: 5
  85. },
  86. margin: {
  87. // 星星的间距
  88. type: [Number, String],
  89. default: 0
  90. },
  91. disabled: {
  92. // 是否可点击
  93. type: [Boolean, String],
  94. default: false
  95. },
  96. readonly: {
  97. // 是否只读
  98. type: [Boolean, String],
  99. default: false
  100. },
  101. allowHalf: {
  102. // 是否显示半星
  103. type: [Boolean, String],
  104. default: false
  105. },
  106. touchable: {
  107. // 是否支持滑动手势
  108. type: [Boolean, String],
  109. default: true
  110. }
  111. },
  112. data() {
  113. return {
  114. valueSync: "",
  115. userMouseFristMove: true,
  116. userRated: false,
  117. userLastRate: 1
  118. };
  119. },
  120. watch: {
  121. value(newVal) {
  122. this.valueSync = Number(newVal);
  123. },
  124. modelValue(newVal) {
  125. this.valueSync = Number(newVal);
  126. },
  127. },
  128. computed: {
  129. stars() {
  130. const value = this.valueSync ? this.valueSync : 0;
  131. const starList = [];
  132. const floorValue = Math.floor(value);
  133. const ceilValue = Math.ceil(value);
  134. for (let i = 0; i < this.max; i++) {
  135. if (floorValue > i) {
  136. starList.push({
  137. activeWitch: "100%"
  138. });
  139. } else if (ceilValue - 1 === i) {
  140. starList.push({
  141. activeWitch: (value - floorValue) * 100 + "%"
  142. });
  143. } else {
  144. starList.push({
  145. activeWitch: "0"
  146. });
  147. }
  148. }
  149. return starList;
  150. },
  151. marginNumber() {
  152. return Number(this.margin)
  153. }
  154. },
  155. created() {
  156. this.valueSync = Number(this.value || this.modelValue);
  157. this._rateBoxLeft = 0
  158. this._oldValue = null
  159. },
  160. mounted() {
  161. setTimeout(() => {
  162. this._getSize()
  163. }, 100)
  164. // #ifdef H5
  165. this.PC = this.IsPC()
  166. // #endif
  167. },
  168. methods: {
  169. touchstart(e) {
  170. // #ifdef H5
  171. if (this.IsPC()) return
  172. // #endif
  173. if (this.readonly || this.disabled) return
  174. const {
  175. clientX,
  176. screenX
  177. } = e.changedTouches[0]
  178. // TODO 做一下兼容,只有 Nvue 下才有 screenX,其他平台式 clientX
  179. this._getRateCount(clientX || screenX)
  180. },
  181. touchmove(e) {
  182. // #ifdef H5
  183. if (this.IsPC()) return
  184. // #endif
  185. if (this.readonly || this.disabled || !this.touchable) return
  186. const {
  187. clientX,
  188. screenX
  189. } = e.changedTouches[0]
  190. this._getRateCount(clientX || screenX)
  191. },
  192. /**
  193. * 兼容 PC @tian
  194. */
  195. mousedown(e) {
  196. // #ifdef H5
  197. if (!this.IsPC()) return
  198. if (this.readonly || this.disabled) return
  199. const {
  200. clientX,
  201. } = e
  202. this.userLastRate = this.valueSync
  203. this._getRateCount(clientX)
  204. this.userRated = true
  205. // #endif
  206. },
  207. mousemove(e) {
  208. // #ifdef H5
  209. if (!this.IsPC()) return
  210. if (this.userRated) return
  211. if (this.userMouseFristMove) {
  212. console.log('---mousemove----', this.valueSync);
  213. this.userLastRate = this.valueSync
  214. this.userMouseFristMove = false
  215. }
  216. if (this.readonly || this.disabled || !this.touchable) return
  217. const {
  218. clientX,
  219. } = e
  220. this._getRateCount(clientX)
  221. // #endif
  222. },
  223. mouseleave(e) {
  224. // #ifdef H5
  225. if (!this.IsPC()) return
  226. if (this.readonly || this.disabled || !this.touchable) return
  227. if (this.userRated) {
  228. this.userRated = false
  229. return
  230. }
  231. this.valueSync = this.userLastRate
  232. // #endif
  233. },
  234. // #ifdef H5
  235. IsPC() {
  236. var userAgentInfo = navigator.userAgent;
  237. var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  238. var flag = true;
  239. for (let v = 0; v < Agents.length - 1; v++) {
  240. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  241. flag = false;
  242. break;
  243. }
  244. }
  245. return flag;
  246. },
  247. // #endif
  248. /**
  249. * 获取星星个数
  250. */
  251. _getRateCount(clientX) {
  252. this._getSize()
  253. const size = Number(this.size)
  254. if (size === NaN) {
  255. return new Error('size 属性只能设置为数字')
  256. }
  257. const rateMoveRange = clientX - this._rateBoxLeft
  258. let index = parseInt(rateMoveRange / (size + this.marginNumber))
  259. index = index < 0 ? 0 : index;
  260. index = index > this.max ? this.max : index;
  261. const range = parseInt(rateMoveRange - (size + this.marginNumber) * index);
  262. let value = 0;
  263. if (this._oldValue === index && !this.PC) return;
  264. this._oldValue = index;
  265. if (this.allowHalf) {
  266. if (range > (size / 2)) {
  267. value = index + 1
  268. } else {
  269. value = index + 0.5
  270. }
  271. } else {
  272. value = index + 1
  273. }
  274. value = Math.max(0.5, Math.min(value, this.max))
  275. this.valueSync = value
  276. this._onChange()
  277. },
  278. /**
  279. * 触发动态修改
  280. */
  281. _onChange() {
  282. this.$emit("input", this.valueSync);
  283. this.$emit("update:modelValue", this.valueSync);
  284. this.$emit("change", {
  285. value: this.valueSync
  286. });
  287. },
  288. /**
  289. * 获取星星距离屏幕左侧距离
  290. */
  291. _getSize() {
  292. // #ifndef APP-NVUE
  293. uni.createSelectorQuery()
  294. .in(this)
  295. .select('.uni-rate')
  296. .boundingClientRect()
  297. .exec(ret => {
  298. if (ret) {
  299. this._rateBoxLeft = ret[0].left
  300. }
  301. })
  302. // #endif
  303. // #ifdef APP-NVUE
  304. dom.getComponentRect(this.$refs['uni-rate'], (ret) => {
  305. const size = ret.size
  306. if (size) {
  307. this._rateBoxLeft = size.left
  308. }
  309. })
  310. // #endif
  311. }
  312. }
  313. };
  314. </script>
  315. <style lang="scss">
  316. .uni-rate {
  317. /* #ifndef APP-NVUE */
  318. display: flex;
  319. /* #endif */
  320. line-height: 1;
  321. font-size: 0;
  322. flex-direction: row;
  323. /* #ifdef H5 */
  324. cursor: pointer;
  325. /* #endif */
  326. }
  327. .uni-rate__icon {
  328. position: relative;
  329. line-height: 1;
  330. font-size: 0;
  331. }
  332. .uni-rate__icon-on {
  333. overflow: hidden;
  334. position: absolute;
  335. top: 0;
  336. left: 0;
  337. line-height: 1;
  338. text-align: left;
  339. }
  340. .uni-cursor-not-allowed {
  341. /* #ifdef H5 */
  342. cursor: not-allowed !important;
  343. /* #endif */
  344. }
  345. </style>