treasureChest.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <cover-view key="treasureBox" class="treasure-box" :style="{display: showBox&&isfull?'flex':'none'}"
  3. :class="[{opening}, {fadeOut}, {dropIn},{swing}]" @click="open">
  4. <cover-image :class="opening ? 'box-img openingGif' : 'box-img boxPng'"
  5. :src="currentIdx >=0 ? opening ? boxList[currentIdx].openChestUrl : boxList[currentIdx].closeChestUrl :''"></cover-image>
  6. <cover-view class="countdown" v-if="!opening">{{count}}s</cover-view>
  7. </cover-view>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. boxList: {
  13. type: Array,
  14. default: []
  15. },
  16. isfull: {
  17. type: Boolean,
  18. default: false
  19. },
  20. onHide: {
  21. type: Boolean,
  22. default: false
  23. },
  24. },
  25. data() {
  26. return {
  27. showBox: false, // 控制宝箱显隐
  28. opening: false, // 是否正在打开
  29. fadeOut: false, // 控制自动消失的动画类
  30. dropIn: false,
  31. swing: false,
  32. count: 20, // 倒计时
  33. timer: null, // 倒计时句柄
  34. timeout: null, // 20s 未点击自动消失句柄
  35. currentIdx: -1,
  36. flagTime: 0
  37. }
  38. },
  39. watch: {
  40. onHide(val) {
  41. if (!this.showBox) return
  42. if (val) { // 暂停
  43. clearInterval(this.timer)
  44. clearTimeout(this.timeout)
  45. this.timer = null
  46. this.timeout = null
  47. } else { // 继续
  48. if (!this.opening) {
  49. const flag = this.boxList.some(item => this.flagTime >= item.time && this.flagTime <= item.time + 20);
  50. if(flag) {
  51. this.countDown()
  52. this.setAutoHide() // 重新计算剩余时长
  53. } else {
  54. this.count = 20
  55. clearInterval(this.timer)
  56. clearTimeout(this.timeout)
  57. this.timer = null
  58. this.timeout = null
  59. }
  60. }
  61. }
  62. },
  63. },
  64. methods: {
  65. close() {
  66. this.swing = false
  67. this.opening = true
  68. this.showBox = false
  69. },
  70. showTreasure(flagTime) {
  71. this.flagTime = flagTime
  72. const idx = this.boxList.findIndex(v => v.time == flagTime && v.status == 0)
  73. if (idx == -1) return
  74. this.currentIdx = idx
  75. this.showBox = true
  76. this.opening = false
  77. this.swing = false
  78. this.fadeOut = false
  79. this.dropIn = true
  80. this.count = 20
  81. clearInterval(this.timer)
  82. clearTimeout(this.timeout)
  83. this.countDown()
  84. this.setAutoHide()
  85. },
  86. /* 倒计时 */
  87. countDown() {
  88. clearInterval(this.timer) // 防止重复启动
  89. this.timer = setInterval(() => {
  90. if (this.count > 0) {
  91. this.count--
  92. } else {
  93. clearInterval(this.timer)
  94. this.timer = null
  95. }
  96. }, 1000)
  97. },
  98. /* 用户点击宝箱 */
  99. open() {
  100. if (this.opening || this.swing) return
  101. clearTimeout(this.timeout)
  102. clearInterval(this.timer)
  103. this.swing = true
  104. this.claim(1)
  105. },
  106. claimSuccess(status) {
  107. setTimeout(()=>{
  108. this.swing = false
  109. this.opening = true
  110. },400)
  111. this.boxList[this.currentIdx].status = status
  112. setTimeout(() => {
  113. this.showBox = false
  114. }, 3000)
  115. },
  116. /* 设置/重置 autoHide 定时器 */
  117. setAutoHide() {
  118. clearTimeout(this.timeout)
  119. const remainMs = this.count * 1000
  120. if (remainMs <= 0) { // 已经过期
  121. this.autoHide()
  122. return
  123. }
  124. this.timeout = setTimeout(() => this.autoHide(), remainMs)
  125. },
  126. /* 20s 未点击,自动消失 */
  127. autoHide() {
  128. clearInterval(this.timer)
  129. this.fadeOut = true
  130. // 标记已打开
  131. this.claim(2)
  132. setTimeout(() => {
  133. this.showBox = false
  134. }, 600)
  135. },
  136. claim(status) {
  137. this.$emit('claimFun', {
  138. rewardId: this.boxList[this.currentIdx].rewardVideoRelation.rewardId,
  139. status: status,
  140. time: this.boxList[this.currentIdx].time
  141. })
  142. }
  143. },
  144. beforeDestroy() {
  145. clearInterval(this.timer)
  146. clearTimeout(this.timeout)
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. @keyframes dropIn {
  152. 0% {
  153. opacity: 0;
  154. transform: translateY(-160px);
  155. }
  156. 60% {
  157. opacity: 1;
  158. transform: translateY(12px);
  159. }
  160. 100% {
  161. opacity: 1;
  162. transform: translateY(0);
  163. }
  164. }
  165. .treasure-box.dropIn {
  166. animation: dropIn 0.7s cubic-bezier(.25, .85, .45, 1.25) forwards,
  167. pulse 1s ease-in-out infinite 0.7s;
  168. }
  169. @keyframes swing {
  170. 20% {
  171. transform: rotate(-8deg);
  172. }
  173. 40% {
  174. transform: rotate(8deg);
  175. }
  176. 60% {
  177. transform: rotate(-6deg);
  178. }
  179. 80% {
  180. transform: rotate(6deg);
  181. }
  182. 100% {
  183. transform: rotate(0deg);
  184. }
  185. }
  186. .treasure-box.swing {
  187. animation: swing 0.4s ease-in-out infinite forwards;
  188. }
  189. /* 宝箱 */
  190. .treasure-box {
  191. position: fixed;
  192. left: 100px;
  193. bottom: 80px;
  194. width: 192px;
  195. /* 128 × 1.5 */
  196. height: 138px;
  197. /* 92 × 1.5 */
  198. animation: pulse 0.4s ease-in-out infinite;
  199. // z-index: 999;
  200. display: flex;
  201. align-items: flex-end;
  202. }
  203. .openingGif {
  204. width: 192px;
  205. /* 128 × 1.5 */
  206. height: 138px;
  207. /* 92 × 1.5 */
  208. }
  209. .boxPng {
  210. width: 135px;
  211. height: 96px;
  212. margin-left: 20px;
  213. }
  214. .countdown {
  215. position: absolute;
  216. top: 50%;
  217. left: 50%;
  218. transform: translate(-50%, -50%);
  219. color: #fff;
  220. font-size: 20px;
  221. font-weight: bold;
  222. text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
  223. }
  224. /* 动画 */
  225. @keyframes pulse {
  226. 0%,
  227. 100% {
  228. transform: scale(1);
  229. }
  230. 50% {
  231. transform: scale(1.15);
  232. }
  233. }
  234. /* 打开后停止 pulse */
  235. .treasure-box.opening {
  236. animation: none;
  237. }
  238. /* 自动消失 */
  239. .treasure-box.fadeOut {
  240. animation: fadeOutUp .6s ease-out forwards;
  241. }
  242. @keyframes fadeOutUp {
  243. to {
  244. opacity: 0;
  245. transform: translate(-50%, -40px);
  246. }
  247. }
  248. </style>