treasureChest.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. winAudio: null
  38. }
  39. },
  40. watch: {
  41. onHide(val) {
  42. if (!this.showBox) return
  43. if (val) { // 暂停
  44. clearInterval(this.timer)
  45. clearTimeout(this.timeout)
  46. this.timer = null
  47. this.timeout = null
  48. } else { // 继续
  49. if (!this.opening) {
  50. const flag = this.boxList.some(item => this.flagTime >= item.time && this.flagTime <= item.time + 20);
  51. if(flag) {
  52. this.countDown()
  53. this.setAutoHide() // 重新计算剩余时长
  54. } else {
  55. this.count = 20
  56. clearInterval(this.timer)
  57. clearTimeout(this.timeout)
  58. this.timer = null
  59. this.timeout = null
  60. }
  61. }
  62. }
  63. },
  64. },
  65. mounted() {
  66. this.initAudio();
  67. },
  68. methods: {
  69. initAudio() {
  70. this.winAudio = uni.createInnerAudioContext();
  71. this.winAudio.src = 'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/audio/treasureChest.mp3';
  72. this.winAudio.volume = 0.6;
  73. },
  74. close() {
  75. this.swing = false
  76. this.opening = true
  77. this.showBox = false
  78. },
  79. showTreasure(flagTime) {
  80. this.flagTime = flagTime
  81. const idx = this.boxList.findIndex(v => v.time == flagTime && v.status == 0)
  82. if (idx == -1) return
  83. this.currentIdx = idx
  84. this.showBox = true
  85. this.opening = false
  86. this.swing = false
  87. this.fadeOut = false
  88. this.dropIn = true
  89. this.count = 20
  90. if(this.isfull) {
  91. uni.vibrateLong()
  92. }
  93. clearInterval(this.timer)
  94. clearTimeout(this.timeout)
  95. this.countDown()
  96. this.setAutoHide()
  97. },
  98. /* 倒计时 */
  99. countDown() {
  100. clearInterval(this.timer) // 防止重复启动
  101. this.timer = setInterval(() => {
  102. if (this.count > 0) {
  103. this.count--
  104. } else {
  105. clearInterval(this.timer)
  106. this.timer = null
  107. }
  108. }, 1000)
  109. },
  110. /* 用户点击宝箱 */
  111. open() {
  112. if (this.opening || this.swing) return
  113. clearTimeout(this.timeout)
  114. clearInterval(this.timer)
  115. this.swing = true
  116. this.claim(1)
  117. },
  118. claimSuccess(status) {
  119. setTimeout(()=>{
  120. this.swing = false
  121. this.opening = true
  122. if(status == 1) {
  123. this.winAudio.play();
  124. }
  125. },400)
  126. this.boxList[this.currentIdx].status = status
  127. setTimeout(() => {
  128. this.winAudio?.stop();
  129. this.showBox = false
  130. }, 3000)
  131. },
  132. /* 设置/重置 autoHide 定时器 */
  133. setAutoHide() {
  134. clearTimeout(this.timeout)
  135. const remainMs = this.count * 1000
  136. if (remainMs <= 0) { // 已经过期
  137. this.autoHide()
  138. return
  139. }
  140. this.timeout = setTimeout(() => this.autoHide(), remainMs)
  141. },
  142. /* 20s 未点击,自动消失 */
  143. autoHide() {
  144. clearInterval(this.timer)
  145. this.fadeOut = true
  146. if(this.isfull) {
  147. this.claim(2)
  148. } else {
  149. this.claim(0)
  150. }
  151. setTimeout(() => {
  152. this.showBox = false
  153. }, 600)
  154. },
  155. claim(status) {
  156. this.$emit('claimFun', {
  157. rewardId: this.boxList[this.currentIdx].rewardVideoRelation.rewardId,
  158. status: status,
  159. time: this.boxList[this.currentIdx].time
  160. })
  161. }
  162. },
  163. beforeDestroy() {
  164. if (this.winAudio) {
  165. this.winAudio.stop();
  166. this.winAudio.destroy();
  167. this.winAudio = null;
  168. }
  169. clearInterval(this.timer)
  170. clearTimeout(this.timeout)
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. @keyframes dropIn {
  176. 0% {
  177. opacity: 0;
  178. transform: translateY(-160px);
  179. }
  180. 60% {
  181. opacity: 1;
  182. transform: translateY(12px);
  183. }
  184. 100% {
  185. opacity: 1;
  186. transform: translateY(0);
  187. }
  188. }
  189. .treasure-box.dropIn {
  190. animation: dropIn 0.7s cubic-bezier(.25, .85, .45, 1.25) forwards,
  191. pulse 1s ease-in-out infinite 0.7s;
  192. }
  193. @keyframes swing {
  194. 20% {
  195. transform: rotate(-8deg);
  196. }
  197. 40% {
  198. transform: rotate(8deg);
  199. }
  200. 60% {
  201. transform: rotate(-6deg);
  202. }
  203. 80% {
  204. transform: rotate(6deg);
  205. }
  206. 100% {
  207. transform: rotate(0deg);
  208. }
  209. }
  210. .treasure-box.swing {
  211. animation: swing 0.4s ease-in-out infinite forwards;
  212. }
  213. /* 宝箱 */
  214. .treasure-box {
  215. position: fixed;
  216. left: 100px;
  217. bottom: 80px;
  218. width: 192px;
  219. /* 128 × 1.5 */
  220. height: 138px;
  221. /* 92 × 1.5 */
  222. animation: pulse 0.4s ease-in-out infinite;
  223. // z-index: 999;
  224. display: flex;
  225. align-items: flex-end;
  226. }
  227. .openingGif {
  228. width: 192px;
  229. /* 128 × 1.5 */
  230. height: 138px;
  231. /* 92 × 1.5 */
  232. }
  233. .boxPng {
  234. width: 135px;
  235. height: 96px;
  236. margin-left: 20px;
  237. }
  238. .countdown {
  239. position: absolute;
  240. top: 50%;
  241. left: 50%;
  242. transform: translate(-50%, -50%);
  243. color: #fff;
  244. font-size: 20px;
  245. font-weight: bold;
  246. text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
  247. }
  248. /* 动画 */
  249. @keyframes pulse {
  250. 0%,
  251. 100% {
  252. transform: scale(1);
  253. }
  254. 50% {
  255. transform: scale(1.15);
  256. }
  257. }
  258. /* 打开后停止 pulse */
  259. .treasure-box.opening {
  260. animation: none;
  261. }
  262. /* 自动消失 */
  263. .treasure-box.fadeOut {
  264. animation: fadeOutUp .6s ease-out forwards;
  265. }
  266. @keyframes fadeOutUp {
  267. to {
  268. opacity: 0;
  269. transform: translate(-50%, -40px);
  270. }
  271. }
  272. </style>