uni-rate.vue 8.5 KB

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