vote.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const UnihaloVoteUid = "unihalo_vote_uid"
  2. export const voteCacheUtil = {
  3. getAll() {
  4. const data = uni.getStorageSync(UnihaloVoteUid)
  5. if (!data) {
  6. return null
  7. }
  8. return JSON.parse(data)
  9. },
  10. get(name) {
  11. const data = this.getAll()
  12. if (!data) {
  13. return null
  14. }
  15. return data[name]
  16. },
  17. has(name) {
  18. const data = this.getAll()
  19. if (!data) return false
  20. return data[name] != undefined
  21. },
  22. set(name, value) {
  23. let data = this.getAll()
  24. if (!data) {
  25. data = {
  26. [name]: value
  27. }
  28. } else {
  29. data[name] = value
  30. }
  31. uni.setStorageSync(UnihaloVoteUid, JSON.stringify(data))
  32. }
  33. }
  34. export const VOTE_TYPES = {
  35. "pk": "双选PK",
  36. "multiple": "多选",
  37. "single": "单选"
  38. }
  39. export const VOTE_STATES = {
  40. "未开始": {
  41. state: "未开始",
  42. color: "orange"
  43. },
  44. "进行中": {
  45. state: "进行中",
  46. color: "green"
  47. },
  48. "已结束": {
  49. state: "已结束",
  50. color: "red"
  51. },
  52. }
  53. export const calcVoteState = (vote) => {
  54. if (vote.spec.timeLimit !== 'custom') {
  55. return vote.spec.hasEnded ? VOTE_STATES["已结束"] : VOTE_STATES["进行中"]
  56. }
  57. const nowTime = new Date().getTime()
  58. const startTime = new Date(vote.spec.startDate).getTime()
  59. const endTime = new Date(vote.spec.endDate).getTime()
  60. if (nowTime < startTime) {
  61. return VOTE_STATES["未开始"]
  62. }
  63. if (nowTime < endTime) {
  64. return VOTE_STATES["进行中"]
  65. }
  66. return vote.spec.hasEnded ? VOTE_STATES["已结束"] : VOTE_STATES["进行中"]
  67. }
  68. export const calcVotePercent = (vote, voteOption) => {
  69. if (!vote || !voteOption) return 0
  70. const votedDataList = vote?.stats?.voteDataList || []
  71. if (votedDataList.length == 0) return 0
  72. const voteCount = vote?.stats?.voteCount || 0
  73. if (voteCount == 0) return 0
  74. const _voteOption = votedDataList.find(x => x.id == voteOption.id)
  75. if (!_voteOption) return 0;
  76. const percent = (_voteOption.voteCount / voteCount) * 100
  77. return Math.round(percent)
  78. }