uni-rate.vue 8.8 KB

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