turntableTwo.vue 8.2 KB

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