只用一个背景图片实现九宫格抽奖(uniapp纯代码)

简介: 只用一个背景图片实现九宫格抽奖(uniapp纯代码)
<template>
    <view class="content">
        <view class="bar">
            <view class="line">
                <p :class="num == 0 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[0].img" mode=""></image>
                        <text>{{data[0].name}}</text>
                    </view>
                </p>
                <p :class="num == 1 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[1].img" mode=""></image>
                        <text>{{data[1].name}}</text>
                    </view>
                </p>
                <p :class="num == 2 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[2].img" mode=""></image>
                        <text>{{data[2].name}}</text>
                    </view>
                </p>
            </view>
            <view class="line">
                <p :class="num == 3 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[3].img" mode=""></image>
                        <text>{{data[3].name}}</text>
                    </view>
                </p>
                <p class="none" @click="click"></p>
                <p :class="num == 4 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[4].img" mode=""></image>
                        <text>{{data[4].name}}</text>
                    </view>
                </p>
            </view>
            <view class="line">
                <p :class="num == 5 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[5].img" mode=""></image>
                        <text>{{data[5].name}}</text>
                    </view>
                </p>
                <p :class="num == 6 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[6].img" mode=""></image>
                        <text>{{data[6].name}}</text>
                    </view>
                </p>
                <p :class="num == 7 ? 'block':'none'">
                    <view class="" v-if="type">
                        <image :src="url+data[7].img" mode=""></image>
                        <text>{{data[7].name}}</text>
                    </view>
                </p>
            </view>
        </view>
        <p @click="start" class="start"></p>
        <p @click="close" class="close"></p>
        <p class="zhong"> <text @click="show">查看攻略</text> <text>可抽奖次数:{{nums}}</text> <text @click="target('/success/myactive')">我的中奖记录</text></p>
        <image class="right" :src="url+'/uploads/20231115/bd89c46e0da9b7a025023fd31d51cfbe.png'" mode=""></image>
        <p class="reco"> <text style="width: 10%;">序号</text> <text style="width: 30%;">昵称</text> <text
                style="width: 30%;">奖品</text><text style="width: 30%;">时间</text></p>
        <view class="prize">
            <p class="list" v-for="(item,index) in winning_data" :key="index"> <text
                    style="width: 10%;">{{item.id}}</text> <text style="width: 30%;">{{item.nickname}}</text> <text
                    style="width: 30%;">{{item.name}}</text><text style="width: 30%;">{{item.create_time}}</text></p>
        </view>
    </view>
</template>
<script>
    import {
        active_list,
        winning,
        winning_add
    } from '@/api/api.js';
    import color from '../uni_modules/uview-ui/libs/config/color';
    export default {
        data() {
            return {
                data: [], //图片数据
                url: getApp().globalData.url, //全局数据
                list: [0, 1, 2, 4, 7, 6, 5, 3],
                num: 8,
                status: false,
                int: '',
                number: 0,
                type: true,
                datas: [], //概率数组
                close_status: false,
                randomNum: 0,
                nums: 1,
                winning_data: [],
                scenic_data:[]
            }
        },
        onLoad(options) {
            this.scenic_data = JSON.parse(options.data)
            this.active_list();
            this.winning()
        },
        methods: {
            target(url){
                uni.navigateTo({
                    url:url
                })
            },
            show() {
                uni.showModal({
                    title: this.data[0]['active'].name,
                    content: this.data[0]['active'].content,
                    success(res) {
                    }
                })
            },
            async winning() {
                const res = await winning()
                this.winning_data = res.data
            },
            // 打乱数组
            randomSortArrays(arr) {
                var stack = [];
                while (arr.length) {
                    //Math.random():返回 [0,1) 之间的一个随机数
                    var index = parseInt(Math.random() * arr.length); // 利用数组长度生成随机索引值
                    stack.push(arr[index]); // 将随机索引对应的数组元素添加到新的数组中
                    arr.splice(index, 1); // 删除原数组中随机生成的元素
                }
                if (this.datas[this.randomNum].name == stack[this.num].name) {
                    uni.showToast({
                        title: '恭喜你抽中' + stack[this.num].name + '!',
                        icon: 'none'
                    })
                    return this.data = stack
                }
                this.randomSortArrays(stack)
            },
            click() {
                    // this.status = false
                    // this.close_status = false
                    // this.type = true
                    // clearInterval(this.int)
                    // this.randomNum = Math.floor(Math.random() * 100) + 1;
                    // this.randomSortArrays(this.data)
                            uni.showToast({
                    title: '点击下方蓝色按钮开始抽奖,红色按钮停止抽奖',
                    icon: 'none'
                })
            },
            start() {
                if (this.nums == 0) {
                    uni.showToast({
                        title: '没有次数了,请点击查看攻略获取次数',
                        icon: 'none'
                    })
                    return
                }
                if (!this.status) {
                    this.nums = this.nums - 1
                    this.status = true
                    this.type = false
                    if (this.num == 8) this.num = 0
                    this.int = setInterval(() => {
                        if (this.status == true) {
                            this.number += 1
                            if (this.number == 8) this.number = 0
                            this.num = this.list[this.number]
                        }
                    }, 500)
                    setTimeout(() => {
                        this.close_status = true
                    }, 4000)
                } else {
                    uni.showToast({
                        title: '手速太快了',
                        icon: 'none'
                    })
                }
            },
            close() {
                if (this.status && this.close_status) {
                    this.status = false
                    this.close_status = false
                    this.type = true
                    clearInterval(this.int)
                    this.randomNum = Math.floor(Math.random() * 100) + 1;
                    this.randomSortArrays(this.data)
                    this.winning_add()
                } else {
                    uni.showToast({
                        title: '至少转一圈',
                        icon: 'none'
                    })
                }
            },
            async winning_add(){
                const res = await winning_add({
                    user_id:JSON.parse(uni.getStorageSync('user')).id,
                    scenic_id:this.scenic_data.id,
                    active_id:this.datas[0]['active']['id'],
                    name:this.data[this.num].name,
                    status:2
                })
                this.winning()
            },
            async active_list() {
                try {
                    const res = await active_list({
                        scenic_id: this.scenic_data.id
                    });
                    for (let i in res.data) {
                        for (let s = 0; s < res.data[i].num; s++) {
                            this.datas.push(res.data[i])
                        }
                    }
                    this.data = res.data
                    console.log(this.data,111);
                } catch (err) {
                    console.log(err);
                }
            },
        }
    }
</script>
<style>
    .prize {
        width: 90%;
        margin-left: 5%;
        height: 450rpx;
        overflow: hidden;
        overflow-y: scroll;
        font-size: 13px;
    }
    .reco {
        width: 80%;
        margin-left: 5%;
        display: flex;
        justify-content: space-evenly;
        padding: 10px 15px;
        margin-top: 80rpx;
        font-size: 12px;
    }
    .reco text {
        display: block;
        text-align: center;
    }
    .list text {
        display: block;
        text-align: center;
    }
    .list {
        width: 90%;
        display: flex;
        justify-content: space-evenly;
        padding: 10px 15px;
    }
    .right {
        width: 80rpx;
        height: 80rpx;
        position: absolute;
        top: 1235rpx;
        right: 0px;
        z-index: 999;
    }
    .content {
        width: 100%;
        height: 2000rpx;
        background-image: url('https://wwz.jingyi.icu/uploads/20231114/a5a1a2f6f9d999445c5ad4f760b23267.png');
        background-repeat: no-repeat;
        background-size: 100% 2000rpx;
        padding: 0;
        margin: 0;
    }
    .bar {
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        padding-top: 548rpx;
        /* display: none; */
    }
    .line {
        width: 76.3%;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        align-content: center;
        margin-top: 10rpx;
    }
    .line image {
        width: 80rpx;
        height: 80rpx;
    }
    .none {
        width: 32%;
        height: 190rpx;
        border-radius: 20rpx;
        background-color: rgba(255, 255, 127, 0);
    }
    .block {
        width: 32%;
        height: 190rpx;
        border-radius: 20rpx;
        background-color: rgba(255, 255, 127, 1);
    }
    .none view {
        width: 100%;
        height: 100%;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: center;
        transition: all 2s;
    }
    .none text {
        width: 100%;
        display: inline;
        text-align: center;
        margin-top: -50rpx;
        font-size: 13px;
        transition: all 2s;
    }
    .block view {
        width: 100%;
        height: 100%;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: center;
        transition: all 2s;
    }
    .block text {
        width: 100%;
        display: inline;
        text-align: center;
        margin-top: -50rpx;
        font-size: 13px;
        transition: all 2s;
    }
    .start {
        position: absolute;
        top: 78vh;
        width: 80rpx;
        height: 60rpx;
        background-color: aqua;
        left: 54%;
        background-color: rgba(255, 255, 127, 0);
    }
    .close {
        position: absolute;
        top: 78vh;
        width: 80rpx;
        height: 60rpx;
        background-color: aqua;
        left: 36%;
        background-color: rgba(255, 255, 127, 0);
    }
    .zhong {
        width: 85%;
        margin-left: 5%;
        display: flex;
        justify-content: space-between;
        margin-top: 115rpx;
        font-size: 15px;
    }
    .zhong text {
        display: flex;
    }
</style>
相关文章
|
JavaScript
VUE里的find与filter使用与区别
VUE里的find与filter使用与区别
1863 0
uniapp 全局数据(globalData)的设置,获取,更改
uniapp 全局数据(globalData)的设置,获取,更改
4061 0
|
9月前
|
人工智能 运维 供应链
同行致远丨AI 云下一程:从出海伙伴到全球智能基石
在全球化与智能化浪潮交汇的今天,企业选择云服务的逻辑,正转向“谁与我战略最同频”
|
3月前
|
人工智能 安全 开发者
组织级 Harness 的建设路径:从 Better Harness 的实践启程
Better Harness 是面向 AI 编程代理(Agent)的观测与治理框架,聚焦“单点做成→团队复制→规模运营”三阶段演进。它串联跨 Agent、项目与设备的任务证据链,将执行数据转化为可验证、可复用、可治理的交付能力,助力个人提效、团队沉淀实践、组织规模化交付。
306 0
|
5月前
|
存储 弹性计算 运维
阿里云国际站代理商:ECS宕机常见原因分析
阿里云 ECS 的宕机故障,大致可以归到三个方向:底层物理资源异常、实例内部软件与内核问题、以及外部网络攻击。每一类都有典型的日志特征和监控信号,但实际案例中,一个宕机事件往往是多因素叠加的结果,排查时切忌只看单一维度。
|
11月前
|
SQL 人工智能 Java
告别传统 Text-to-SQL:基于 Spring AI Alibaba 的数据分析智能体 DataAgent 深度解析
DataAgent是基于Spring AI Alibaba生态构建的企业级AI数据分析师,融合NL2SQL、多智能体协作与RAG技术,支持多数据源分析、自动纠错与可视化报告生成,让业务人员零代码获取深度数据洞察。
4342 42
告别传统 Text-to-SQL:基于 Spring AI Alibaba 的数据分析智能体 DataAgent 深度解析
|
9月前
|
人工智能 自然语言处理 前端开发
AI生成网站入门指南:从零基础到专业建站的路径
零基础建站难?AI生成网站成新选择!无需代码、低成本,三步搞定:①明确目标场景;②用自然语言生成页面与前后端代码(如LynxCode);③优化交互与适配。AI不替代开发者,而是降低门槛、提升效率,助创业者快速验证想法。
|
机器学习/深度学习 人工智能 自然语言处理
如何构建企业级数据智能体:Data Agent 开发实践
本篇将介绍DMS的一款数据分析智能体(Data Agent for Analytics )产品的技术思考和实践。Data Agent for Analytics 定位为一款企业级数据分析智能体, 基于Agentic AI 技术,帮助用户查数据、做分析、生成报告、深入洞察。
|
JavaScript
Vue2文字提示(Tooltip)
这篇文章介绍了如何在Vue 3框架中创建一个文字提示组件(Tooltip),允许自定义提示框的最大宽度、展示文本和提示文本,支持鼠标悬停显示和隐藏效果。
908 0
Vue2文字提示(Tooltip)