模拟中奖小demo

class Winning {
  constructor(el, option) {
    this.el = el
    this.option = Object.assign(
      {},
      {
        type: 'qq', // 可选择qq或者phone
        min: 10, //最少数据
        max: 20 // 最多数据,
      },
      option
    )
    this._init()
  }
  _init() {
    this.getData()
  }
  // TODO: 生成数据(可以采用回调加强灵活性)
  getData() {
    let html = '',
      count = random(this.option.min, this.option.max),
      type = this.option.type === 'qq'
    for (let i = 0; i < count; i++) {
      html += `
  • 恭喜${ type ? 'QQ' : '手机' }用户: ${ type ? getQQ() : getPhone() } 于 ${new Date().Format( 'yyyy-MM-dd' )} 奖励: ${randomGift()}
  • ` } document.getElementById(this.el).innerHTML = html } // 滚动 scroll() {} } //***************************** */ const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min) // 获取随机6-10位QQ号 function getQQ() { let qq = random(100000, 9999999999).toString() return qq.replace(qq.substr(-4), '****') } let phoneTest = [ '133', '149', '153', '173', '177', '180', '181', '189', '199', '130', '131', '132', '145', '155', '156', '166', '171', '175', '176', '185', '186', '166', '134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '172', '178', '182', '183', '184', '187', '188', '198' ] const getPhone = () => { return ( phoneTest[random(0, phoneTest.length - 1)] + '****' + random(1000, 9999) ) } const gifts = [ '永久豪华绿钻', '永久豪华绿钻', '永久视频会员', '永久豪华黄钻', '永久超级会员', '' ] // 随机中奖的奖品 function randomGift() { let num = random(0, gifts.length - 1), zan = random(100, 10000) return num === gifts.length - 1 ? gifts[num] + `${zan}名片赞` : gifts[num] } // 格式化时间 Date.prototype.Format = function(fmt) { let o = { 'M+': this.getMonth() + 1, //月份 'd+': this.getDate(), //日 'h+': this.getHours(), //小时 'm+': this.getMinutes(), //分 's+': this.getSeconds(), //秒 'q+': Math.floor((this.getMonth() + 3) / 3), //季度 S: this.getMilliseconds() //毫秒 } if (/(y+)/.test(fmt)) fmt = fmt.replace( RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length) ) for (var k in o) if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace( RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length) ) return fmt }

    你可能感兴趣的:(模拟中奖小demo)