uni-rate.vue 8.9 KB

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