uniapp小程序九宫格抽奖

news2024/10/6 14:28:07

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

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

vue代码: 

<template>
	<view class="page" v-if="merchantInfo.cdn_static">
		<image class="bg" :src="merchantInfo.cdn_static +'statistics/luckDrawImg/sudoku/page_bg.png'" mode="aspectFill"></image>
		<view class="content">
			<view class="logo">
				<image :src="merchantInfo.logo" mode="heightFix"></image>
			</view>
			<view class="box">
				<view class="awards">
					<view class="awardsItem" v-for="(item,index) in awardsList" :key="index" :class="item.active ? 'awardsActive' : ''" @click="startPlay(index)">
						<view class="play" v-if="index == 4">
							<view class="go">{{item.title}}</view>
							<view v-if="index == 4" class="count">{{total}}次机会</view>
						</view>
						<image :src="merchantInfo.cdn_static + item.img" v-else></image>
					</view>
				</view>
				
				<view class="notification">
					<image :src="merchantInfo.cdn_static +'statistics/luckDrawImg/sudoku/notification.png'" mode="heightFix"></image>
					<text>好礼转不停</text>
				</view>
				<view class="btns">
					<view class="btn" @click="rule_show=true">
						<image :src="merchantInfo.cdn_static +'statistics/luckDrawImg/sudoku/rule.png'"></image>
					</view>
					<view class="btn" @click="getResult()">
						<image :src="merchantInfo.cdn_static +'statistics/luckDrawImg/sudoku/prize.png'"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="win" v-if="rule_show">
			<scroll-view scroll-y class="win_box .win_box_bg">
				<mp-html :content="luckDrawInfo.rule" />
			</scroll-view>
			<text class="iconfont iconcolseIcon theme-font-white" @click="rule_show=false"></text>
		</view>
		<view class="win" v-if="result_show">
			<view class="win_box1">
				<image class="win_bg" :src="merchantInfo.cdn_static +'statistics/luckDrawImg/result_bg.png'" mode=""></image>
				<view class="win_content">
					<view class="win_tips theme-font-white">{{currentPrize.desc}}</view>
					<view class="win_title">{{currentPrize.title}}</view>
					<view class="win_btn" @click="choiseAddress()">{{currentPrize.is_address==1?'选择地址':'确定'}}</view>
				</view>
			</view>
		</view>
		<view class="win" v-if="prize_show">
			<view class="win_tit theme-font-white">我的奖品</view>
			<view class="win_box2">
				<view class="items">
					<view class="left i_title">奖品</view>
					<view class="right i_title">中奖时间</view>
				</view>
				<scroll-view scroll-y class="list">
					<view class="item" v-for="(item,index) in list" :key="index">
						<view class="left">{{item.lottery_prize_title}}</view>
						<view class="right" v-if="item.is_address==1&&!item.address_id">
							<view class="r_btn"  @click="choiseAddress1(item)">去领奖</view>
						</view>
						<view class="right" v-else>{{item.created_time}}</view>
					</view>
				</scroll-view>
			</view>
			<text class="iconfont iconcolseIcon theme-font-white" @click="prize_show=false"></text>
		</view>
	</view>

</template>

<script>
	import { luckDrawInfo } from '@/api/luckDraw.js';
	import colors from '@/mixins/color';
	export default {
		mixins: [colors],
		data() {
			return {//https://cdn.dev.scrm.juplus.cn/InQLzDLoAl2S9LyNJUXQ45gpA.png
				mask: true,
				wtf:true,
				luckDrawInfo: {},
				rule_show:false,
				result_show:false,
				prize_show:false,
				total:0,
				currentPrize:{},
				list:[],
				id: "",
				awardsList: [],
				indexArray: [0, 1, 2, 5, 8, 7, 6, 3],
				num: 0,
				count: 0,
				timer: null
			}
		},
		props: {
			userId: {
				type: [Number,String]
			},
			type:{
				type: [Number,String]
			}
		},
		//渲染完了
		mounted() {
			this.id = this.userId;
			this.init();
		},
		methods: {
			/**
			 * 设置奖项
			 */
			setAwardsList(data){
				this.awardsList = [];
				let arrayBefore = data.length > 0 ? data.slice(0,4) : [];
				let arrayAfter = data.length > 0 ? data.slice(4) : [];
				this.awardsList = [...arrayBefore];
				this.awardsList.push({
					title: "抽"
				});
				this.awardsList = [...this.awardsList,...arrayAfter];
				this.awardsList.forEach((item,index) => {
					item.active = index == 4 ? true : false;
				});
			},
			init(){
				if(this.userInfo){
					this.getInfo()
				}else{
					setTimeout(()=>{
						this.init()
					},500)
				}
			},
			getInfo(){
				luckDrawInfo.getDetail({id:this.id}).then(res => {
					this.luckDrawInfo=res.data
					this.total=res.data.my_can_num
					this.setAwardsList(res.data.lottery_prize);
					this.action('lottery',this.id,0,2,this.luckDrawInfo.title,'','lottery')
				})
			},
			choiseAddress(){
				this.currentPrize.is_address==1?uni.navigateTo({
						url:'/pages/address/address'
					}):''
				this.result_show=false
				// 重置抽奖
				this.awardsList.forEach((item,index) => {
					item.active = index == 4 ? true : false;
				});
				this.num = 0;
				this.count = 0;
				this.timer = null;
			},
			choiseAddress1(data){
				this.currentPrize=data
				uni.navigateTo({
					url:'/pages/address/address'
				})
				this.prize_show=false
			},
			setAddress(id){
				luckDrawInfo.setAddress({address_id:id,history_id:this.currentPrize.history_id||this.currentPrize.id}).then(res => {
					uni.showToast({
						title:"地址设置成功",
						icon:'none'
					})
				})
			},
			getResult(){
				if(!this.wtf){
					return false
				}
				luckDrawInfo.getResult({lottery_id:this.id}).then(res => {
					this.list=res.data.data
					this.prize_show=true
				})
			},
			// 点击开始,请求接口抽奖
			startPlay(index) {
				if (index != 4) return false;
				
				if(this.luckDrawInfo.is_register==1&&!this.userInfo.type){
					uni.navigateTo({
						url:'/pages/login/login'
					})
					return false
				}
				if(this.luckDrawInfo.is_form==1&&this.luckDrawInfo.user_form_count==0){
					uni.navigateTo({
						url:'/pages/form/form?id='+this.luckDrawInfo.form_id+'&type_id=' + this.id + '&type=lottery'
					})
					return false
				}
				if(!this.wtf){
					return false
				}
				
				// 活动未开始或活动已结束
				let startTimeMs = new Date(this.luckDrawInfo.start_time).getTime();
				let endTimeMs = new Date(this.luckDrawInfo.end_time).getTime();
				let nowTimeMs = new Date().getTime();
				if (nowTimeMs < startTimeMs) {
					uni.showToast({
						icon: "none",
						title: "活动未开始"
					})
					return false;
				}
				if (nowTimeMs > endTimeMs) {
					uni.showToast({
						icon: "none",
						title: "活动已结束"
					})
					return false;
				}
				
				this.mask = false;
				this.wtf = false;
				
				// 抽奖开始
				clearInterval(this.timer)
				this.timer = setInterval(() => {
					this.awardsList.forEach((item,index) => {
						item.active = false;
					});
					this.awardsList[this.indexArray[this.num]].active = true;
					this.num++;
					if (this.num >= this.indexArray.length) {
						this.num = 0;
						this.count++;
					}
				},100);
				
				luckDrawInfo.run({id:this.id}).then(res => {
					this.currentPrize = res.data;
					this.total = res.data.row_lottery_new.my_can_num;
					// 抽奖停止
					let prizeIndex = this.awardsList.findIndex(item => item.id == this.currentPrize.id);
					let prizeNumIndex = prizeIndex != -1 ? this.indexArray.findIndex(item => item == prizeIndex) : null;
					let delayTimer = setInterval(()=>{
						// 奖品选中
						if (this.count >= 3 && this.num == prizeNumIndex) {
							clearInterval(this.timer);
							clearInterval(delayTimer);
							this.awardsList.forEach((item,index) => {
								item.active = index == prizeIndex ? true : false;
								this.$set(this.awardsList, index, {...this.awardsList[index], active: item.active});
							});
							setTimeout(() => {
								this.result_show = true;
								this.wtf = true;
							},600);
						}
					},100)
				}).catch(err => {
					this.wtf = true;
				});

			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'index.scss';
	/**/
</style>

scss代码:


.page{
	width: 750rpx;
	height: 100vh;
	position: relative;
}

.bg{
	width: 750rpx;
	height: 100vh;
}

.content{
	width: 750rpx;
	min-height: 100vh;
	height: 1448rpx;
	position: absolute;
	top: 0;
	left: 0;
}


.logo{
	height: 60rpx;
	display: flex;
	justify-content: center;
	margin-top: 90rpx;
	
	image{
		height: 60rpx;
	}
}

.box{
	width: 680rpx;
	margin: 0 auto;
	margin-top: 330rpx;
	
	.awards{
		width: 525rpx;
		height: 525rpx;
		margin: 0 auto;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-content:space-between;
		
		.awardsItem{
			width: 161rpx;
			height: 161rpx;
			background: linear-gradient(132deg, #FFCEA4 0%, #FFE6CB 100%);
			box-shadow: 0rpx 6rpx 0rpx 0rpx #E98F5D;
			border-radius: 15rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			cursor: pointer;
			
			image{
				width: 132rpx;
				height: 132rpx;
				border-radius: 12rpx;
			}
				
			.play{
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				.go{
					font-size: 68rpx;
					font-family: PingFangSC-Semibold, PingFang SC;
					font-weight: 600;
					color: #A10B14;
				}
				
				.count{
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #A10B14;
				}
			}
			
		}
		.awardsActive{
			background: linear-gradient(132deg, #FFECBB 0%, #FFB100 100%);
			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);
		}
	}
	
	.notification{
		height: 40rpx;
		margin-top: 226rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		
		image{
			height: 32rpx;
			margin-right: 16rpx;
		}
		text{
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FF8D3B;
		}
	}
	
	.btns{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 38rpx;
	}
	
	.btn{
		width: 332rpx;
		height: 110rpx;
		image{
			width: 100%;
			height: 100%;
		}
	}
	
}

.win{
	width: 750rpx;
	height: 100vh;
	background: rgba(0, 0, 0, 0.8);
	position: fixed;
	top: 0;
	left: 0;
	z-index: 2;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.win_box{
	width: 662rpx;
	height: 60%;
	padding: 40rpx;
	box-sizing: border-box;
	border-radius: 24rpx;
}

.win_box_bg{
	background: #FFCE2B;
}

.bg3{
	background: #C3E5FE;
}

.iconcolseIcon{
	font-size: 58rpx;
	margin-top: 98rpx;
}

.win_box1{
	width: 630rpx;
	height: 922rpx;
	position: relative;
}

.win_bg{
	width: 630rpx;
	height: 922rpx;
}

.win_content{
	width: 630rpx;
	height: 922rpx;
	position: absolute;
	left: 0;
	top: 0;
	display: flex;
	flex-direction: column;
	align-items: center;
}

.win_tips{
	font-size: 48rpx;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
	margin-top: 290rpx;
}

.win_title{
	font-size: 48rpx;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
	color: #FE6631;
	margin: 170rpx 0;
}

.win_btn{
	width: 280rpx;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	background: #FFE047;
	border-radius: 46rpx;
	font-size: 32rpx;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
	color: #13112C;
}

.win_tit{
	font-size: 48rpx;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
	margin-bottom: 32rpx;
}

.win_box2{
	width: 662rpx;
	height: 900rpx;
	background: #FFFFFF;
	border-radius: 24rpx;
	display: flex;
	flex-direction: column;
}

.items{
	width: 662rpx;
	height: 108rpx;
	background: #C3E5FE;
	border-radius: 24rpx 24rpx 0rpx 0rpx;
	display: flex;
	align-items: center;
	flex-shrink:0
}

.left,.right{
	width: 50%;
	text-align: center;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
}

.i_title{
	font-size: 36rpx;
}

.list{
	height: 792rpx;
	padding-bottom: 20rpx;
	overflow: hidden;
}

.item{
	width: 662rpx;
	height: 88rpx;
	display: flex;
	align-items: center;
	justify-content: space-around;
}

.item:nth-child(2n){
	background-color: #F4F4F4;
}

.r_btn{
	width: 160rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	background: #FFC659;
	border-radius: 46rpx;
	font-family: SourceHanSansSC-Medium, SourceHanSansSC;
	font-weight: bold;
	font-size: 32rpx;
	margin:0 auto;
}

效果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1168052.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

3 数据库系统核心知识点

一、事务 先回顾一下事务的概念&#xff1a; 事务指的是满足 ACID 特性的一组操作&#xff0c;可以通过 Commit 提交一个事务&#xff0c;也可以使用 Rollback 进行回滚ACID 1. 原子性(Atomicity) 1.事务被视为不可分割的最小单元&#xff0c;事务的所有操作要么全部提交成…

【算法】背包问题——01背包

题目 有 N 件物品和一个容量是 V 的背包。每件物品只能使用一次。 第 i 件物品的体积是 vi&#xff0c;价值是 wi。 求解将哪些物品装入背包&#xff0c;可使这些物品的总体积不超过背包容量&#xff0c;且总价值最大。 输出最大价值。 输入格式 第一行两个整数&#xff0…

算法通关村第六关|青铜|树通过序列构造二叉树

1.二叉树的性质 1.在二叉树的第 i 层上至多有 2^(i-1) 个结点。 2.深度为 k 的二叉树至多有 2^k - 1 个结点。 3.对于任意一棵二叉树&#xff0c;如果叶节点数为 N0 &#xff0c;度数为 2 的结点总数为 N2 &#xff0c;则 N0N21 。 4.具有 n 个结点的完全二叉树的深度为 lo…

uniapp原生插件之安卓华为统一扫码HMS Scan Kit

插件介绍 华为统一扫码服务&#xff08;Scan Kit&#xff09;提供便捷的条形码和二维码扫描、解析、生成能力 插件地址 安卓华为统一扫码HMS Scan Kit - DCloud 插件市场 超级福利 uniapp 插件购买超级福利 详细使用文档 详细使用文档 插件申请权限 android.permi…

Uniapp中使用Echarts

在小程序中使用echarts 我直接调用了HbuilderX的百度图表进行使用 LimeUi - 多端uniapp组件库 (qcoon.cn) 通用代码去官网找一下就好了赋值进去第一个柱状图表就可以显示了 地图的使用 <template><view style"height: 750rpx"><l-echart ref"…

软件测试用例方法---边界值法

原则&#xff1a; 输入最小值&#xff08;min&#xff09;、稍大于最小值&#xff08;min&#xff09;、域内任意值&#xff08;nom&#xff09;、稍小于最大值&#xff08;max-&#xff09;、最大值&#xff08;max&#xff09; 写法&#xff1a;“单故障”假设&#xff08;致…

java后端响应结果Result

目录 一、Result1-1、响应代码1-2、调用响应1-3、在前端vue页面使用方法 一、Result 1-1、响应代码 package com.aaa.common;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;Data AllArgsConstructor NoArgsConstructor public cla…

Breakpad在Windows,Linux双平台编译、集成以及dump文件的分析

Breakpad在Windows&#xff0c;Linux双平台编译、集成以及dump文件的分析 1、Windows平台 Windows平台上非常好的参考文档&#xff1a; https://r12f.com/posts/google-breakpad-1-introduction-with-windows/ https://r12f.com/posts/google-breakpad-2-implementations-o…

数字银行:数据安全的挑战与对策

随着科技的进步和互联网的普及&#xff0c;传统的银行业务逐渐向数字化转型。数字银行&#xff0c;以其高效、便捷、个性化的服务特点&#xff0c;正在改变着人们的生活方式。然而&#xff0c;与此同时&#xff0c;数据安全问题也日益凸显。如何在享受数字银行便利的同时&#…

项目实战:添加新库存记录

1、在index.html添加超链接&#xff0c;添加新库存add.html <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><link rel"stylesheet" href"style/index.css"…

项目实战:修改水果库存系统特定库存记录

1、在edit.html修改库存页面添加点击事件 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><link rel"stylesheet" href"style/index.css"><script s…

kubernetes集群编排——k8s存储

configmap 字面值创建 kubectl create configmap my-config --from-literalkey1config1 --from-literalkey2config2kubectl get cmkubectl describe cm my-config 通过文件创建 kubectl create configmap my-config-2 --from-file/etc/resolv.confkubectl describe cm my-confi…

18张图揭秘高性能Linux服务器内存池技术是如何实现的

大家生活中肯定都有这样的经验&#xff0c;那就是大众化的产品都比较便宜&#xff0c;但便宜的大众产品就是一个词&#xff0c;普通&#xff1b;而可以定制的产品一般都价位不凡&#xff0c;这种定制的产品注定不会在大众中普及&#xff0c;因此定制产品就是一个词&#xff0c;…

java基础练习(使用java实现跨库数据调度ETL)

简介 本文写一篇关于java库与库之间的数据传输&#xff0c;现实生产中可能是通过其他方式完成&#xff0c;在没有架构的基础上使用java实现跨库的数据传送&#xff0c;非常不便利。但是作为练习我觉得确实非常有用&#xff0c;涉及的java知识点相对较多。本文以一个实列讲解&am…

双十一越来越早,公域电商何去何从——深度解析

为什么双十一越来越早&#xff1f;传统的公域电商越来越饱和,某淘某东甚至和某79元网红打起了价格战 &#xff0c;市面上标品越来越多&#xff0c;商家越来越多&#xff0c;买东西的变少了&#xff0c;商家越卖越便宜。 公域电商&#xff1a; 就像一个大市场。里面摆满了各种商…

1015. 摘花生

题目&#xff1a; 1015. 摘花生 - AcWing题库 思路&#xff1a;dp 代码&#xff1a; #include<iostream> #include<cstdio> #include<cmath> using namespace std; const int N 110; typedef long long ll; int T, r, c; int num[N][N]; ll dp[N][N];//dp…

性能优化之懒加载 - 基于观察者模式和单例模式的实现

一、引入 在前端性能优化中&#xff0c;关于图片/视频等内容的懒加载一直都是优化利器。当用户看到对应的视图模块时&#xff0c;才去请求加载对应的图像。 原理也很简单&#xff0c;通过浏览器提供的 IntersectionObserver - Web API 接口参考 | MDN (mozilla.org)&#xff0c…

Express框架开发接口之书城商店原型图和数据库设计

1.原型图 这是利用Axure画的&#xff0c;简单画一下原型图&#xff0c;根据他们的业务逻辑我们完成书城商店API开发 首页 分类 商品详情 购物车 个人中心 2.数据库设计 首页 首页导航 导航栏产品 通过点击导航栏对应id和产品id关联获取产品内容 设置外建 sql语句 CR…

CRM系统数据库是如何影响客户体验的?

CRM客户关系管理由概念到软件实体&#xff0c;已经有几十年的时间&#xff0c;随着信息技术的进步&#xff0c;数字化让CRM软件乘上快车&#xff0c;迅速成为各类企业的数字化管理工具。CRM客户管理系统的一个重要功能便是改善并提升客户体验&#xff0c;且CRM数据库是与客户体…

朝夕光年游戏高光全套解决方案

作为全球用户与开发者的游戏研发与发行业务品牌——朝夕光年&#xff0c;一直致力于服务全球玩家&#xff0c;帮助玩家在令人惊叹的虚拟世界中一起玩耍与创造。 游戏研发的过程中遇到很多玩家痛点和提效、拉新的问题&#xff0c;如何帮助玩家更好地记忆、记录、分享和传播自己的…