turntableTwo.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <uni-popup ref="turntablePopup" type="center" :is-mask-click="false">
  3. <view class="turntable">
  4. <image class="turntable-bg" src="https://cos.his.cdwjyyh.com/fs/20250910/de386927c5f44e02ab3ecc480ddb9797.png" mode="aspectFit"></image>
  5. <view class="turntable-box">
  6. <view class="turntable-wrap">
  7. <view class="turntable-item" :class="{ active: idx == light,disabled: item.isReceive }"
  8. v-for="(item,idx) in cells" :key="idx">
  9. <image :src="item.iconUrl" mode="aspectFit"></image>
  10. <view class="ellipsis">{{item.name}}</view>
  11. </view>
  12. </view>
  13. </view>
  14. <image class="close" src="https://cos.his.cdwjyyh.com/fs/20250910/93608bad8ad1479a854ec26e8eaaacea.png" @click="close"></image>
  15. <view class="btn-box">
  16. <image src="https://cos.his.cdwjyyh.com/fs/20250910/fe230bfdf74f4f0dac88af53f709d6c5.png" mode="widthFix" :class="isReceive==1 ?'gray':''" @click="start"></image>
  17. </view>
  18. </view>
  19. </uni-popup>
  20. </template>
  21. <script>
  22. import {getVideoRewardRules} from "@/api/course.js"
  23. export default {
  24. data() {
  25. return {
  26. baseUrl: uni.getStorageSync('requestImagesPath'),
  27. light: -1,
  28. running: false,
  29. targetIdx: -1,
  30. total: 0,
  31. speed: 100,
  32. cells:[],
  33. timer: null,
  34. isReceive: 0,
  35. stepAudio: null, // 跳动音
  36. winAudio: null // 中奖音
  37. }
  38. },
  39. computed: {
  40. // 剩余可用索引
  41. availIdx() {
  42. return this.cells.map((_, i) => i).filter(i => !this.cells[i].isReceive)
  43. },
  44. // 是否全部抽完
  45. allDone() {
  46. return this.availIdx.length === 0
  47. }
  48. },
  49. methods: {
  50. initAudio() {
  51. this.stepAudio = uni.createInnerAudioContext();
  52. this.stepAudio.src = 'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/audio/spin.mp3';
  53. this.stepAudio.volume = 0.6;
  54. this.winAudio = uni.createInnerAudioContext();
  55. this.winAudio.src = 'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/audio/win.mp3';
  56. this.winAudio.volume = 1;
  57. },
  58. getVideoRewardRules(urlOption) {
  59. const param = {
  60. type: 4,
  61. ...urlOption
  62. }
  63. getVideoRewardRules(param).then(res=>{
  64. if(res.code == 200) {
  65. this.cells = res.data || []
  66. this.total = this.cells.length
  67. this.isReceive = this.cells.every(item => item.isReceive == true)? 1 :0
  68. }else {
  69. uni.showToast({
  70. title: res.msg,
  71. icon: 'none'
  72. })
  73. }
  74. })
  75. },
  76. open(urlOption) {
  77. this.initAudio();
  78. this.$refs.turntablePopup.open()
  79. this.getVideoRewardRules(urlOption)
  80. },
  81. close(type) {
  82. if(type=='close') {
  83. this.running = false
  84. clearInterval(this.timer)
  85. }
  86. if (this.running) {
  87. uni.showToast({
  88. title: '抽取中,请勿关闭',
  89. icon: 'none'
  90. })
  91. return
  92. }
  93. this.$refs.turntablePopup.close()
  94. },
  95. start() {
  96. if (this.running) {
  97. uni.showToast({
  98. title: '抽取中',
  99. icon: 'none'
  100. })
  101. return
  102. }
  103. if(this.isReceive == 1) return
  104. this.running = true
  105. clearInterval(this.timer)
  106. let last = -1
  107. if(this.availIdx.length >1) {
  108. this.timer = setInterval(() => {
  109. let next;
  110. do {
  111. next = this.availIdx[Math.floor(Math.random() * this.availIdx.length)];
  112. } while (next === last);
  113. last = next;
  114. this.light = next;
  115. this.stepAudio.stop(); // 停掉上一声(防止重叠)
  116. this.stepAudio.play(); // 播放跳动音
  117. }, this.speed)
  118. }
  119. this.$emit('sendRewardFun',4)
  120. },
  121. endSuccess(code) {
  122. this.targetIdx = this.cells.findIndex(it=>it.code == code)
  123. clearInterval(this.timer)
  124. this.timer = null
  125. if(this.targetIdx==-1) {
  126. uni.showToast({
  127. title: '抽奖失败',
  128. icon: 'none'
  129. })
  130. return
  131. }
  132. const STEP = 100; // 每格固定 100 ms
  133. const MIN_STEP = 10; // 最少 10 步 → 1 s
  134. this.speed = STEP;
  135. let totalSteps = Math.max(MIN_STEP, this.availIdx.length);
  136. // 约 2.5s / 100ms
  137. let step = 0
  138. let last = -1
  139. const that = this
  140. if(this.availIdx.length <= 1) {
  141. this.light = this.targetIdx
  142. this.stepAudio.stop();
  143. this.winAudio.play(); // 播放中奖音
  144. this.running = false
  145. this.isReceive = 1
  146. uni.showModal({
  147. title: '恭喜,中奖',
  148. content: this.cells[this.targetIdx].name,
  149. showCancel: false,
  150. success: function (res) {
  151. if (res.confirm) {
  152. if (that.stepAudio) {
  153. that.stepAudio.stop();
  154. that.stepAudio.destroy();
  155. that.stepAudio = null;
  156. }
  157. if (that.winAudio) {
  158. that.winAudio.stop();
  159. that.winAudio.destroy();
  160. that.winAudio = null;
  161. }
  162. that.$refs.turntablePopup.close()
  163. that.$emit("openAppPop")
  164. }
  165. }
  166. });
  167. return
  168. }
  169. this.timer = setInterval(() => {
  170. let next
  171. do {
  172. next = this.availIdx[Math.floor(Math.random() * this.availIdx.length)]
  173. } while (next === last)
  174. last = next
  175. this.light = next
  176. this.stepAudio.stop(); // 停掉上一声(防止重叠)
  177. this.stepAudio.play(); // 播放跳动音
  178. step++
  179. if (step >= totalSteps) {
  180. clearInterval(this.timer)
  181. this.light = this.targetIdx
  182. this.stepAudio.stop();
  183. this.winAudio.play(); // 播放中奖音
  184. // 置灰
  185. this.running = false
  186. this.isReceive = 1
  187. setTimeout(()=>{
  188. uni.showModal({
  189. title: '恭喜,中奖',
  190. content: this.cells[this.targetIdx].name,
  191. showCancel: false,
  192. success: function (res) {
  193. if (res.confirm) {
  194. if (that.stepAudio) {
  195. that.stepAudio.stop();
  196. that.stepAudio.destroy();
  197. that.stepAudio = null;
  198. }
  199. if (that.winAudio) {
  200. that.winAudio.stop();
  201. that.winAudio.destroy();
  202. that.winAudio = null;
  203. }
  204. that.$refs.turntablePopup.close()
  205. that.$emit("openAppPop")
  206. }
  207. }
  208. });
  209. },500)
  210. // this.cells[this.targetIdx].isReceive = true
  211. }
  212. }, this.speed)
  213. }
  214. }
  215. }
  216. </script>
  217. <style scoped lang="scss">
  218. .gray {
  219. filter: grayscale(1); /* 1 = 100% 灰度 */
  220. }
  221. .close {
  222. width: 64rpx;
  223. height: 64rpx;
  224. position: absolute;
  225. top: 88rpx;
  226. right: 20rpx;
  227. z-index: 2;
  228. }
  229. .active {
  230. transform: scale(1.05);
  231. background: linear-gradient(0deg, #FFD44F 0%, #FFFABD 100%) !important;
  232. box-shadow: 0rpx 2rpx 0rpx 0rpx #FFFCF5 !important;
  233. }
  234. .disabled {
  235. filter: grayscale(1) brightness(0.7);
  236. opacity: 0.5;
  237. }
  238. .btn-box {
  239. width: 540rpx;
  240. height: 104rpx;
  241. position: absolute;
  242. bottom: 46rpx;
  243. left: 50%;
  244. transform: translateX(-50%);
  245. z-index: 2;
  246. image {
  247. width: 100%;
  248. height: auto;
  249. }
  250. }
  251. .turntable {
  252. width: 750rpx;
  253. height: 1056rpx;
  254. position: relative;
  255. &-bg {
  256. width: 100%;
  257. height: 100%;
  258. position: absolute;
  259. top: 0;
  260. left: 0;
  261. z-index: 1;
  262. }
  263. &-box {
  264. position: absolute;
  265. bottom: 174rpx;
  266. left: 50%;
  267. transform: translateX(-50%);
  268. z-index: 2;
  269. width: 623rpx;
  270. height: 628rpx;
  271. padding: 16rpx 0 8rpx 16rpx;
  272. box-sizing: border-box;
  273. }
  274. &-wrap {
  275. display: flex;
  276. flex-wrap: wrap;
  277. }
  278. &-item {
  279. width: 112rpx;
  280. height: 112rpx;
  281. background: linear-gradient(0deg, #FCEFCA 0%, #FFFFFF 100%);
  282. border-radius: 16rpx;
  283. font-family: PingFang SC;
  284. font-weight: 500;
  285. font-size: 20rpx;
  286. color: #333333;
  287. display: flex;
  288. align-items: center;
  289. justify-content: center;
  290. flex-direction: column;
  291. margin: 0 8rpx 8rpx 0;
  292. transition: transform 0.2s, box-shadow 0.2s;
  293. image {
  294. width: 45rpx;
  295. height: 63rpx;
  296. }
  297. .ellipsis {
  298. overflow: hidden;
  299. text-overflow: ellipsis;
  300. white-space: nowrap;
  301. }
  302. }
  303. }
  304. </style>