vue 代码写 老虎机抽奖活动

公司让用vue写几个活动,老虎机,大转盘,摇一摇,砸金蛋,翻牌,刮刮乐等活动。记录下来。
这个是我老虎机抽奖写好后的图片效果。代码在下面,全代码,不包括,活动规则和中奖记录的模板代码,没啥大用。


老虎机抽奖

代码我也懒的一个一个讲了,太忙了,让我一个月整好10个活动。。。。。还都是vue-cli脚手架搭建来写,我怕是要猝死啊,呜呜
直接上代码。我要是忽然不更新了,我怕是已经凉了,记着捞我。


<template>
<div class="counts">
  <div class="banner">
      <img src="~img/banner_newyear.png">
    </div>
    <div class="total">

      <div class="game_user">— {{title}} —</div>
      <div class="num" ref="numHeight">
        <div class='slot' v-for='slot in slots' ref='slots'>
          <div class='slot__window'>
            <div class='slot__wrap'>
              <div class='slot__item' v-for='opt in slot.items'>
                <img :src="opt.goodsIconUrl">
                <span class="isTitle">{{opt.prizeName}}</span>
              </div>
              <div class='slot__item slot__item--copy'>
                <img :src="slot.items[0].goodsIconUrl">
                <span class="isTitle">{{slot.items[0].prizeName}}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- <img src="https://ojlf2aayk.qnssl.com/onebtn.png" v-if="isLucky" class="luckybtn" @click="start"> -->
      <div class="touchbtn_start  point_touch" @click="start"></div>

      <div class="btn_group">

                        <div class="add_btn">
                            <div class="act_rule" @click="showdiv(1)">活动规则</div>
                            <div class="luck_recond" @click="showdiv(2)">中奖记录</div>
                        </div>
                    </div>

    </div>

      <ruleComponent :itemsListName="itemsListName" v-if="isShowRule"></ruleComponent>
      <codeComponent :itemsListCodeName="itemsListCodeName" v-if="isShowCode"></codeComponent>
    <mt-popup v-model="popupVisible" :closeOnClickModal=false :modal = false>
      <div class="pop_box" @touchmove.prevent>
        <div class="tell_tit">
          <h1 class="pop-idea">用户登录</h1>
        </div>
        <div class="info-set">
          <div class="writer"><span>手机号:</span> <input v-model.trim="ruleForm.phone" type="tel" placeholder="请输入您的入手机号" maxlength="11" class="tel_writer"></div>
          <div class="writer"><span>随机码:</span>
            <input v-model.trim="ruleForm.code" type="text" placeholder="请输入随机码" maxlength="5" class="tel_writer">
            <img :src="createCode" @click="imgClick" class="print-code">
          </div>
          <div class="writer"><span>验证码:</span> <input v-model.trim="ruleForm.createCode" type="tel" placeholder="请输入验证码" maxlength="4" class="tel_writer"> <div :style="btnImg" @click="reginInto" id="btn">{{reginValue}}</div></div>
        </div>
        <div class="sure_btn" @click="buttomRegin">立即登录</div>
        <div class="close-btn" @click="popupVisible=false"><img src="~img/close.png"></div>
      </div>
    </mt-popup>
    <transition name="fade">
    <div class="isVodel" v-show="popupVisible" @touchmove.prevent>
    </div>
      </transition>

        <div class="isVodel" v-if="isShowRule" @touchmove.prevent></div>

      <div class="isVodel" v-if="isShowCode" @touchmove.prevent></div>


      <div class="popup" v-if="toast_control">
          <div class="popbg"></div>
          <div class="popbox">
            <div class="img" :class="{'img1':isImgTrue}"></div>
            <div class="words1">{{hasPrize.words1}}</div>
            <div class="words2">{{hasPrize.words2}}</div>
            <div class="words3" v-show="hasPrize.words3!=''">{{hasPrize.words3}}</div>
          </div>
          <div class="close" @click="toast_control=false"></div>
        </div>

</div>
</template>
<script>
const next = window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  window.msRequestAnimationFrame ||
  window.oRequestAnimationFrame || function(cb) {
    window.setTimeout(cb, 1000 / 60)
  }
  import ruleComponent from "./rule.vue"
    import codeComponent from "./code.vue"
  import {
    initApi,
    loginApi,
    lotteryApi,
    awardApi,
    getCaptcha,
    sendCaptcha,
    registerApi,
    winning
  } from "../api/api";
export default {
  data() {
    return {
      isButton: false,
      popupVisible: false,
      isShowRule:false,
      isShowCode:false,
      createCode: "",
      isImgTrue:false,
      ruleForm: {
        phone: '',
        code: '',
        createCode: "",
      },
      hasPrize:{},
      reginValue: "获取验证码",
      isCode: true, //验证码开关
      toast_control:false,
      itemsList:"",
      itemsListName:"",
      btnImg: {},
      title: '幸运老虎机',
      isLucky: true,
      interval: false,
      numone: 0,
      numtwo: 0,
      numthe: 0,
      textone: 24,
      texttwo: 27,
      textthe: 30,
      slots: [],
      objectItem:[],
      opts: null,
      startedAt: null,
      indexChoice:0,
      winnum:0,
      itemsListCodeName:[],
    }
  },
  components: {
    ruleComponent,
    codeComponent
  },
  methods: {
    winningMenu(){
      winning().then((res)=>{
        if(res.httpCode=200){
          this.itemsListCodeName = res.data;
          this.isShowCode = true;
        }

      })
    },
    showdiv(index){
      if(index==1){
        this.itemsListName = this.itemsList;
        this.isShowRule = true;

      }else{
        if (this.isButton) {
          this.popupVisible = true;
          return
        }
        this.winningMenu();

      }
    },
    loginMenu() {
      let paramsName = "";
      if (window.location.host.split(".")[0] != "192") {
        paramsName = window.location.host.split(".")[0];
      } else {
        paramsName = "m02bsfsk"
      }

      loginApi(paramsName).then((res) => {
        if (res.httpCode == 200) {
          window.localStorage.setItem("activity_lefeng", res.data.sign);
          this.initApiMenu();

        }
      })
    },
    initApiMenu() {
        initApi().then((res)=>{
          this.ObjectAll = res.data;
          this.popupVisible = res.data.proRegister;
          this.isButton = res.data.proRegister;
          this.imgClick();

          res.data.marketingCampaign.terms.forEach((item, index) => {
            if (item.termType == 10) {
              this.itemsList = item.termVal;
            }
          })


          res.data.marketingCampaign.pictures.forEach((item) => {
            if (item.picType == "1000") {

              //document.body.style.backgroundImage = "url(" + item.picUrl + ")"
            } else if (item.picType == "1202") {
              //this.beganToDraw = "url(" + item.picUrl + ")"
            } else if (item.picType == "1200") {
              //this.beganToDrawImg = "url(" + item.picUrl + ")"
            }


          })

          res.data.marketingCampaign.prizes.forEach((item, index) => {

            this.objectItem.push({
              "prizeType": index + 1,
              "prizeName": item.prizeName,
              "goodsIconUrl": item.goodsInfo.goodsIconUrl,
              "goodsId": item.goodsId,
            })



          })



          for(var i=0;i<4;i++){
            this.slots.push({
                title: i,
                items: this.objectItem,
              });

          }


          console.log(this.slots);
        })
    },
    buttomRegin() {

      if (this.ruleForm.phone != "" || this.ruleForm.phone) {
        if (/^1[34578]\d{9}$/.test(this.ruleForm.phone)) {
          console.log("手机号通过验证");
        } else {
          this.$toast("请输入正确的手机号")

          return false;
        }
      } else {
        this.$toast("请输入手机号")
        return false;
      }

      if (this.ruleForm.code == "") {
        this.$toast("请输入随机码")
        return false;
      }
      if (this.ruleForm.createCode == "") {
        this.$toast("请输入验证码")
        return false;
      }
      this.$indicator.open({
         text: '加载中...',
         spinnerType: 'fading-circle'
       });
      let params = {
        "phone": this.ruleForm.phone,
        "phoneCode": this.ruleForm.createCode,
      }

      registerApi(params).then((res) => {
        this.$indicator.close()
        if (res.httpCode == 200) {
          this.isButton = false;
          this.popupVisible = false;

        } else {
          this.$toast(res.retMsg)
        }
      }).catch((err)=>{
        this.$indicator.close();
        this.$toast("接口异常");
      })
    },
    //验证码
    reginInto() {


      if (this.ruleForm.phone != "" || this.ruleForm.phone) {
        if (/^1[34578]\d{9}$/.test(this.ruleForm.phone)) {
          console.log("手机号通过验证");
        } else {
          this.$toast("请输入正确的手机号")

          return false;
        }
      } else {
        this.$toast("请输入手机号")
        return false;
      }

      if (this.ruleForm.code == "") {
        this.$toast("请输入随机码")
        return false;
      }

      if (!this.isCode) {
        return false;
      }
      this.sendCaptchaMenu(this.ruleForm.code);




    },
    //发送验证码
    sendCaptchaMenu(code) {
      let params = {
        "mobile": this.ruleForm.phone,
        "type": "sms_login",
        "captcha": code,
      }
      sendCaptcha(params).then((res) => {
        if (res.httpCode == 200) {
          this.btnImg = {
            "background": "#d3d4d6"
          }
          this.reginValue = "60秒"
          let isNum = 60
          this.isCode = false;
          var inteval = setInterval(() => {
            isNum--
            this.reginValue = isNum + "秒"
            if (isNum == 0) {
              clearInterval(inteval);
              this.isCode = true;
              this.reginValue = "获取验证码"
              this.btnImg = {
                "background": "#fc817e"
              }
            }

          }, 1000);
        } else {
          this.$toast(res.retMsg);
          this.isCode = true;
          this.reginValue = "获取验证码"
          this.btnImg = {
            "background": "#fc817e"
          }
          return false;
        }
      })
    },
    //随机码
    imgClick() {
      getCaptcha().then(res => {
        console.log(res);
        this.createCode = "data:image/png;base64," + res.data;
      });
      //$("#codeImg").attr("src","createCode?r="+Math.random())
    },
    startChou(index){
      console.log(this.$refs.numHeight.offsetHeight);
      let heightN = this.$refs.numHeight.offsetHeight;

      if (this.opts) {
        return
      }
      this.opts = this.slots.map((data, i) => {
        const slot = this.$refs.slots[i]; // map(function(){})利用map便利slots的每一个选项组,最终得到return的返回值
        // const choice = Math.floor(Math.random() * data.items.length); // 随机生成一个[0,data.items.length]的整数(floor向下取整)
         const choice = index;
        console.log("choice", i, data.items[choice])
        const opts = {
          el: slot.querySelector('.slot__wrap'), //指向奖项元素的父级;
          finalPos: choice * heightN, // 180为每一个奖品滚动标签的高度;
          //        startOffset: 2000 + Math.random() * 500 + i * 500,// 影响转的圈数
          startOffset: 2000 + Math.random() * 500 + i * 500, // 影响转的圈数
          height: data.items.length * heightN,
          duration: 3000 + i * 700, // milliseconds
          isFinished: false,
        }
        return opts
      })
      //    console.log(this.opts);//这个时候this.opts已经和opts一模一样了
      next(this.animate); // 开启动画
    },
    start: function() {
      if (this.isButton) {
        this.popupVisible = true;
        return
      }


      lotteryApi().then((res) => {
        if (res.httpCode == 200) {
          this.objectItem.forEach((item, index) => {
            if (item.goodsId == res.data.goodsId) {

                this.winnum = index;
                this.startChou(this.winnum);
            }
          })
        }else{
          if(res.retCode=="10030001"){
            this.loginMenu();
          }else{
            this.$toast(res.retMsg)
          }
        }
      });


    },
    game_over(index) {
      let _this = this;
      _this.toast_control = true;
        var obj = {}
        if(index == 1){
            this.isImgTrue = false;
          if (_this.objectItem[_this.winnum].prizeName == "谢谢参与") {
            obj = {
              type: 0,
              words1: '谢谢参与',
              words2: "不要气馁!",
              words3: '还有更多大奖等着你~'
            }
          }  else {
            obj = {
              type: 1,
              words1: '恭喜您!',
              words2: "获得" + _this.objectItem[_this.winnum].prizeName,
              words3: ''
            }
          }
        }else{
          this.isImgTrue = true;
            obj = {
              type: 3,
              words1: '抱歉!',
              words2: '出现未知错误,请稍后再试',
              words3: ''
            }
        }

        _this.hasPrize = obj
    },
    //兑奖apo
    awardApiMenu() {
      let _this = this
      awardApi().then((res) => {
        if (res.httpCode == 200) {
          _this.game_over(1);
        }else{
            _this.game_over(2);
        }
      })
    },
    animate: function(timestamp) { // timestamp当前的方法持续的毫秒数
      if (this.startedAt == null) {
        this.startedAt = timestamp // 动画初始时间
      }
      const timeDiff = timestamp - this.startedAt //动画持续的时间
      this.opts.forEach(opt => {
        if (opt.isFinished) {
          return
        }
        const timeRemaining = Math.max(opt.duration - timeDiff, 0); // 总的持续时间 - 动画持续时间 = 剩下的时间,0表示结束
        const power = 3
        const offset = (Math.pow(timeRemaining, power) / Math.pow(opt.duration, power)) * opt.startOffset
        // Math.pow(timeRemaining, power)表示: timeRemaining 的3 次幂;
        // negative, such that slots move from top to bottom
        const pos = -1 * Math.floor((offset + opt.finalPos) % opt.height)
        opt.el.style.transform = "translateY(" + pos + "px)"

        if (timeDiff > opt.duration) {
          //        console.log('finished', opt, pos, opt.finalPost)
          opt.isFinished = true
        }

      })

      if (this.opts.every(o => o.isFinished)) {
        this.opts = null
        this.startedAt = null
        this.awardApiMenu();
        //      console.log('finished')
      } else {
        next(this.animate)
      }
    }
  },
  mounted() {
    this.loginMenu();
  }
}
</script>
<style media="screen">
.fade-enter-active, .fade-leave-active {
  transition: opacity .2s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.mint-indicator{
  position: relative;
  z-index: 2000;
}
.mint-popup {
  z-index: 200!important;
}

</style>
<style scoped>

.pop_box {
  border-radius: .08rem;
  position: relative;
  left: 0;
  width: 6.2rem;
  margin-left: auto;
  margin-right: auto;
  background-color: #fff;
  z-index: 400;
  padding-bottom: .35rem;
}
.isVodel{
  position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0.5;
background: #000;
z-index: 100;
}
.tell_tit {
  width: 100%;
  height: 1.44rem;

  background-size: 100% 100%;
}

.pop-idea {
  line-height: 1.44rem;
  font-size: .38rem;
  color: #fff;
  text-align: center;
  font-weight: 700;
  background-color: #00aca6;
  background-image: url(~img/clold.png);

}

.info-set {
  text-align: center;
}

.writer {
  position: relative;
  width: 100%;
  border-bottom: 1px solid #989594;
  display: -ms-flexbox;
  display: flex;
  padding-top: .08rem;
  padding-bottom: .08rem;
}

.writer span {
  text-align: center;
  width: 1.5rem;
  color: #4d3932;
  font-size: .28rem;
  line-height: .8rem;
}

input[type=text] {
  -webkit-appearance: none;
  background: none;
  outline: none;
}

.writer input {
  -ms-flex: 1;
  flex: 1;
  color: #5a5340;
  font-size: .26rem;
  line-height: .3rem;
  border: none;
}

.print-code {
  position: absolute;
  width: 1.39rem;
  height: .5rem;
  right: .2rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 15px;
  margin: 0;
  padding: 0;
  outline: none;
  font-family: Hiragino Sans GB, "sans-serif";
}

.writer span {
  text-align: center;
  width: 1.5rem;
  color: #4d3932;
  font-size: .28rem;
  line-height: .8rem;
}

#btn {
  position: absolute;
  width: 1.49rem;
  height: .6rem;
  line-height: .6rem;
  right: .2rem;
  top: 50%;
  transform: translateY(-50%);
  border-radius: .45rem;
  text-align: center;
  color: #fff;
  background: #fc817e;
  font-size: .22rem;
}

.sure_btn {
  margin-top: .3rem;
  line-height: .65rem;
  width: 4.2rem;
  background: #fc817e;
  color: #fff;
  font-size: .26rem;
  margin-left: auto;
  margin-right: auto;
  border-radius: .45rem;
  border-bottom: 4px solid #dc625d;
  text-align: center;
}



.close-btn {
  font-size: 15px;
  margin: 0;
  padding: 0;
  outline: none;
  font-family: Hiragino Sans GB, "sans-serif";
  position: absolute;
  top: -.19rem;
  right: -.19rem;
}

.close-btn img {
  width: .42rem;
  height: .42rem;
}
</style>
<style lang='less' scoped>

</style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 207,248评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,681评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,443评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,475评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,458评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,185评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,451评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,112评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,609评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,083评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,163评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,803评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,357评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,357评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,590评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,636评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,925评论 2 344

推荐阅读更多精彩内容