uni-rate.vue 8.9 KB

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