• uniapp小程序九宫格抽奖


    定义好奖品下标,计时器开始抽奖,请求接口,出现中奖奖品之后,获取中奖商品对应的奖品下标,再次计时器判断当前移动的小标是否为中奖商品的下标,并且是否转到3圈(防止转1圈就停止),如果时就清除两次计时器。

    当前代码封装为九宫格的组件;

    vue代码: 

    1. <script>
    2. import { luckDrawInfo } from '@/api/luckDraw.js';
    3. import colors from '@/mixins/color';
    4. export default {
    5. mixins: [colors],
    6. data() {
    7. return {//https://cdn.dev.scrm.juplus.cn/InQLzDLoAl2S9LyNJUXQ45gpA.png
    8. mask: true,
    9. wtf:true,
    10. luckDrawInfo: {},
    11. rule_show:false,
    12. result_show:false,
    13. prize_show:false,
    14. total:0,
    15. currentPrize:{},
    16. list:[],
    17. id: "",
    18. awardsList: [],
    19. indexArray: [0, 1, 2, 5, 8, 7, 6, 3],
    20. num: 0,
    21. count: 0,
    22. timer: null
    23. }
    24. },
    25. props: {
    26. userId: {
    27. type: [Number,String]
    28. },
    29. type:{
    30. type: [Number,String]
    31. }
    32. },
    33. //渲染完了
    34. mounted() {
    35. this.id = this.userId;
    36. this.init();
    37. },
    38. methods: {
    39. /**
    40. * 设置奖项
    41. */
    42. setAwardsList(data){
    43. this.awardsList = [];
    44. let arrayBefore = data.length > 0 ? data.slice(0,4) : [];
    45. let arrayAfter = data.length > 0 ? data.slice(4) : [];
    46. this.awardsList = [...arrayBefore];
    47. this.awardsList.push({
    48. title: "抽"
    49. });
    50. this.awardsList = [...this.awardsList,...arrayAfter];
    51. this.awardsList.forEach((item,index) => {
    52. item.active = index == 4 ? true : false;
    53. });
    54. },
    55. init(){
    56. if(this.userInfo){
    57. this.getInfo()
    58. }else{
    59. setTimeout(()=>{
    60. this.init()
    61. },500)
    62. }
    63. },
    64. getInfo(){
    65. luckDrawInfo.getDetail({id:this.id}).then(res => {
    66. this.luckDrawInfo=res.data
    67. this.total=res.data.my_can_num
    68. this.setAwardsList(res.data.lottery_prize);
    69. this.action('lottery',this.id,0,2,this.luckDrawInfo.title,'','lottery')
    70. })
    71. },
    72. choiseAddress(){
    73. this.currentPrize.is_address==1?uni.navigateTo({
    74. url:'/pages/address/address'
    75. }):''
    76. this.result_show=false
    77. // 重置抽奖
    78. this.awardsList.forEach((item,index) => {
    79. item.active = index == 4 ? true : false;
    80. });
    81. this.num = 0;
    82. this.count = 0;
    83. this.timer = null;
    84. },
    85. choiseAddress1(data){
    86. this.currentPrize=data
    87. uni.navigateTo({
    88. url:'/pages/address/address'
    89. })
    90. this.prize_show=false
    91. },
    92. setAddress(id){
    93. luckDrawInfo.setAddress({address_id:id,history_id:this.currentPrize.history_id||this.currentPrize.id}).then(res => {
    94. uni.showToast({
    95. title:"地址设置成功",
    96. icon:'none'
    97. })
    98. })
    99. },
    100. getResult(){
    101. if(!this.wtf){
    102. return false
    103. }
    104. luckDrawInfo.getResult({lottery_id:this.id}).then(res => {
    105. this.list=res.data.data
    106. this.prize_show=true
    107. })
    108. },
    109. // 点击开始,请求接口抽奖
    110. startPlay(index) {
    111. if (index != 4) return false;
    112. if(this.luckDrawInfo.is_register==1&&!this.userInfo.type){
    113. uni.navigateTo({
    114. url:'/pages/login/login'
    115. })
    116. return false
    117. }
    118. if(this.luckDrawInfo.is_form==1&&this.luckDrawInfo.user_form_count==0){
    119. uni.navigateTo({
    120. url:'/pages/form/form?id='+this.luckDrawInfo.form_id+'&type_id=' + this.id + '&type=lottery'
    121. })
    122. return false
    123. }
    124. if(!this.wtf){
    125. return false
    126. }
    127. // 活动未开始或活动已结束
    128. let startTimeMs = new Date(this.luckDrawInfo.start_time).getTime();
    129. let endTimeMs = new Date(this.luckDrawInfo.end_time).getTime();
    130. let nowTimeMs = new Date().getTime();
    131. if (nowTimeMs < startTimeMs) {
    132. uni.showToast({
    133. icon: "none",
    134. title: "活动未开始"
    135. })
    136. return false;
    137. }
    138. if (nowTimeMs > endTimeMs) {
    139. uni.showToast({
    140. icon: "none",
    141. title: "活动已结束"
    142. })
    143. return false;
    144. }
    145. this.mask = false;
    146. this.wtf = false;
    147. // 抽奖开始
    148. clearInterval(this.timer)
    149. this.timer = setInterval(() => {
    150. this.awardsList.forEach((item,index) => {
    151. item.active = false;
    152. });
    153. this.awardsList[this.indexArray[this.num]].active = true;
    154. this.$set(this.awardsList, [this.indexArray[this.num]], { ...this.awardsList[this.indexArray[this.num]], active: true });
    155. this.num++;
    156. if (this.num >= this.indexArray.length) {
    157. this.num = 0;
    158. this.count++;
    159. }
    160. },100);
    161. luckDrawInfo.run({id:this.id}).then(res => {
    162. this.currentPrize = res.data;
    163. this.total = res.data.row_lottery_new.my_can_num;
    164. // 抽奖停止
    165. let prizeIndex = this.awardsList.findIndex(item => item.id == this.currentPrize.id);
    166. let prizeNumIndex = prizeIndex != -1 ? this.indexArray.findIndex(item => item == prizeIndex) : null;
    167. let delayTimer = setInterval(()=>{
    168. // 奖品选中
    169. if (this.count >= 3 && this.num == prizeNumIndex) {
    170. clearInterval(this.timer);
    171. clearInterval(delayTimer);
    172. this.awardsList.forEach((item,index) => {
    173. item.active = index == prizeIndex ? true : false;
    174. this.$set(this.awardsList, index, {...this.awardsList[index], active: item.active});
    175. });
    176. setTimeout(() => {
    177. this.result_show = true;
    178. this.wtf = true;
    179. },600);
    180. }
    181. },100)
    182. }).catch(err => {
    183. this.wtf = true;
    184. });
    185. }
    186. }
    187. }
    188. script>
    189. <style scoped lang="scss">
    190. @import 'index.scss';
    191. /**/
    192. style>

    scss代码:

    1. .page{
    2. width: 750rpx;
    3. min-height: 100vh;
    4. height: 1448rpx;
    5. position: relative;
    6. }
    7. .bg{
    8. width: 750rpx;
    9. min-height: 100vh;
    10. height: 1448rpx;
    11. }
    12. .content{
    13. width: 750rpx;
    14. min-height: 100vh;
    15. height: 1448rpx;
    16. position: absolute;
    17. top: 0;
    18. left: 0;
    19. }
    20. .logo{
    21. height: 60rpx;
    22. display: flex;
    23. justify-content: center;
    24. margin-top: 90rpx;
    25. image{
    26. height: 60rpx;
    27. }
    28. }
    29. .box{
    30. width: 680rpx;
    31. margin: 0 auto;
    32. margin-top: 330rpx;
    33. .awards{
    34. width: 525rpx;
    35. height: 525rpx;
    36. margin: 0 auto;
    37. display: flex;
    38. flex-wrap: wrap;
    39. justify-content: space-between;
    40. align-content:space-between;
    41. .awardsItem{
    42. width: 161rpx;
    43. height: 161rpx;
    44. background: linear-gradient(132deg, #FFCEA4 0%, #FFE6CB 100%);
    45. box-shadow: 0rpx 6rpx 0rpx 0rpx #E98F5D;
    46. border-radius: 15rpx;
    47. display: flex;
    48. flex-direction: column;
    49. align-items: center;
    50. justify-content: center;
    51. cursor: pointer;
    52. image{
    53. width: 132rpx;
    54. height: 132rpx;
    55. border-radius: 12rpx;
    56. }
    57. .play{
    58. display: flex;
    59. flex-direction: column;
    60. align-items: center;
    61. justify-content: center;
    62. .go{
    63. font-size: 68rpx;
    64. font-family: PingFangSC-Semibold, PingFang SC;
    65. font-weight: 600;
    66. color: #A10B14;
    67. }
    68. .count{
    69. font-size: 24rpx;
    70. font-family: PingFangSC-Regular, PingFang SC;
    71. font-weight: 400;
    72. color: #A10B14;
    73. }
    74. }
    75. }
    76. .awardsActive{
    77. background: linear-gradient(132deg, #FFECBB 0%, #FFB100 100%);
    78. box-shadow: 0rpx 6rpx 0rpx 0rpx #E38800, inset 0rpx 2rpx 6rpx 0rpx rgba(255,255,255,0.65), inset 0rpx -2rpx 6rpx 0rpx rgba(255,255,255,0.3);
    79. }
    80. }
    81. .notification{
    82. height: 40rpx;
    83. margin-top: 226rpx;
    84. display: flex;
    85. justify-content: center;
    86. align-items: center;
    87. image{
    88. height: 32rpx;
    89. margin-right: 16rpx;
    90. }
    91. text{
    92. font-size: 28rpx;
    93. font-family: PingFangSC-Regular, PingFang SC;
    94. font-weight: 400;
    95. color: #FF8D3B;
    96. }
    97. }
    98. .btns{
    99. display: flex;
    100. align-items: center;
    101. justify-content: space-between;
    102. margin-top: 38rpx;
    103. }
    104. .btn{
    105. width: 332rpx;
    106. height: 110rpx;
    107. image{
    108. width: 100%;
    109. height: 100%;
    110. }
    111. }
    112. }
    113. .win{
    114. width: 750rpx;
    115. height: 100vh;
    116. background: rgba(0, 0, 0, 0.8);
    117. position: fixed;
    118. top: 0;
    119. left: 0;
    120. z-index: 2;
    121. display: flex;
    122. flex-direction: column;
    123. align-items: center;
    124. justify-content: center;
    125. }
    126. .win_box{
    127. width: 662rpx;
    128. height: 60%;
    129. padding: 40rpx;
    130. box-sizing: border-box;
    131. border-radius: 24rpx;
    132. }
    133. .win_box_bg{
    134. background: #FFCE2B;
    135. }
    136. .bg3{
    137. background: #C3E5FE;
    138. }
    139. .iconcolseIcon{
    140. font-size: 58rpx;
    141. margin-top: 98rpx;
    142. }
    143. .win_box1{
    144. width: 630rpx;
    145. height: 922rpx;
    146. position: relative;
    147. }
    148. .win_bg{
    149. width: 630rpx;
    150. height: 922rpx;
    151. }
    152. .win_content{
    153. width: 630rpx;
    154. height: 922rpx;
    155. position: absolute;
    156. left: 0;
    157. top: 0;
    158. display: flex;
    159. flex-direction: column;
    160. align-items: center;
    161. }
    162. .win_tips{
    163. font-size: 48rpx;
    164. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    165. font-weight: bold;
    166. margin-top: 290rpx;
    167. }
    168. .win_title{
    169. font-size: 48rpx;
    170. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    171. font-weight: bold;
    172. color: #FE6631;
    173. margin: 170rpx 0;
    174. }
    175. .win_btn{
    176. width: 280rpx;
    177. height: 80rpx;
    178. line-height: 80rpx;
    179. text-align: center;
    180. background: #FFE047;
    181. border-radius: 46rpx;
    182. font-size: 32rpx;
    183. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    184. font-weight: bold;
    185. color: #13112C;
    186. }
    187. .win_tit{
    188. font-size: 48rpx;
    189. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    190. font-weight: bold;
    191. margin-bottom: 32rpx;
    192. }
    193. .win_box2{
    194. width: 662rpx;
    195. height: 900rpx;
    196. background: #FFFFFF;
    197. border-radius: 24rpx;
    198. display: flex;
    199. flex-direction: column;
    200. }
    201. .items{
    202. width: 662rpx;
    203. height: 108rpx;
    204. background: #C3E5FE;
    205. border-radius: 24rpx 24rpx 0rpx 0rpx;
    206. display: flex;
    207. align-items: center;
    208. flex-shrink:0
    209. }
    210. .left,.right{
    211. width: 50%;
    212. text-align: center;
    213. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    214. font-weight: bold;
    215. }
    216. .i_title{
    217. font-size: 36rpx;
    218. }
    219. .list{
    220. height: 792rpx;
    221. padding-bottom: 20rpx;
    222. overflow: hidden;
    223. }
    224. .item{
    225. width: 662rpx;
    226. height: 88rpx;
    227. display: flex;
    228. align-items: center;
    229. justify-content: space-around;
    230. }
    231. .item:nth-child(2n){
    232. background-color: #F4F4F4;
    233. }
    234. .r_btn{
    235. width: 160rpx;
    236. height: 60rpx;
    237. line-height: 60rpx;
    238. text-align: center;
    239. background: #FFC659;
    240. border-radius: 46rpx;
    241. font-family: SourceHanSansSC-Medium, SourceHanSansSC;
    242. font-weight: bold;
    243. font-size: 32rpx;
    244. margin:0 auto;
    245. }

    效果:

  • 相关阅读:
    mysql++库connected与ping方法的区别
    FreeRTOS入门教程(任务优先级,Tick)
    融云全球社交泛娱乐洞察,互联网社交换挡期的「社区产品」机遇
    第二篇:Sentinel之SPI机制使用与源码分析
    测试移动电源IC IP5306、IP5407
    java计算机毕业设计ssm基金分析系统的设计与实现
    netty学习 java-io
    Servlet请求转发与重定向
    maven多仓库私库模板配置
    并查集(Union-Find)
  • 原文地址:https://blog.csdn.net/qq_43235503/article/details/134211561