uni-rate.vue 8.6 KB

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