看到网上许多抽奖的代码脚本感觉都不适合,最后自己编写了自己脚本。我的这篇文章希望对你有帮助,有对sky8g网站感兴趣的请关注本网站,谢谢你的支持。
今天使用脚本写了个随机抽奖代码,可以自定义抽奖的类型和哪个类中奖的次数,自定义哪个国家的权重设置,比较完美的抽奖脚本。
废话不多说了直接贴出下面的代码,供参考希望对你有帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | var myObj = { "KR":['韩国单号1','韩国单号2','韩国单号3','韩国单号4','韩国单号5','韩国单号6'], "JP":['日本单号1','日本单号2','日本单号3','日本单号4','日本单号5','日本单号6'], "DE":['德国单号1','韩国单号2','德国单号3','德国单号4','德国单号5','德国单号6'], "US":['美国单号1','美国单号2','美国单号3','美国单号4','美国单号5','美国单号6'], }; var a = ['KR','JP','DE','US']; //国家 var b = [0.01,0.02,0.02,0.88]; //权重 //如果没设置国家默认只抽奖1次,则为{}空即可。 var countryCount = { "JP":[3,0], //第一个是国家的抽奖次数,并且抽奖次数必须小于这个国家的订单数量。第二个是已执行的次数, "AU":[1,0], 'DE':[2,0] }; var clickMaxTimes =5; //表示最大点击次数且 必须小于国家的数量 var clickCount =0; //点击的初始状态次数,不可更改 var countryTimes = 1; //可以抽奖的默认次数,不可更改 //submit function clickme(a,b){ if(clickCount >= clickMaxTimes ){ alert('抽奖已经完成!'); return; } clickCount++; var prize = ''; var count = Object.keys(myObj).length; if(count < 1){ alert('不能进行抽奖了!'); return; } if(count == 1){ for (q in myObj) { if(countryCount[q]){ countryTimes = countryCount[q][0]; } var myObjOrder = myObj[q]; var orderid = myObjOrder[randomNum(0,myObjOrder.length-1)]; if( countryCount[q] && countryCount[q][1] < countryTimes ){ if( countryCount[q][1] == countryTimes -1 ){ prize = q+","+orderid; delete myObj[q]; }else{ myObj[q].splice(myObj[q].indexOf(orderid),1); prize = q+","+orderid; } countryCount[q][1]++; }else{ prize = q+","+orderid; delete myObj[q]; } } return prize; } var countrycode = countryRandom(a,b); var countryOrderArr,countryOrderArrLenth,objCountryCode; var i; for (i in myObj) { if(i == countrycode){ if(countryCount[i]){ countryTimes = countryCount[i][0]; } countryOrderArr = myObj[i]; objCountryCode = i; countryOrderArrLenth = myObj[i].length; var orderid = countryOrderArr[randomNum(0,countryOrderArrLenth-1)]; var spliceCode = a.indexOf(objCountryCode); if( countryCount[i] && countryCount[i][1] < countryTimes){ if( countryCount[i][1] == countryTimes -1 ){ var b_point = b[spliceCode]; delete myObj[objCountryCode]; a.splice(spliceCode,1); b.splice(spliceCode,1); prize = objCountryCode+","+orderid; }else{ myObj[i].splice(myObj[i].indexOf(orderid),1); prize = objCountryCode+","+orderid; } countryCount[i][1]++; }else{ var b_point = b[spliceCode]; delete myObj[objCountryCode]; a.splice(spliceCode,1); b.splice(spliceCode,1); prize = objCountryCode+","+orderid; } return prize; } } } function suiJiSameGaiLv(oneRand,b){ var arr = []; if(arr.constructor === Array){ for(h in b){ if(b[h] == oneRand){ arr.push(parseInt(h));} } } return arr; } function countryRandom(arr1, arr2) { var sum = 0, factor = 0, random = Math.random(); for(var i = arr2.length - 1; i >= 0; i--) { sum += arr2[i]; }; random *= sum; for(var i = arr2.length - 1; i >= 0; i--) { factor += arr2[i]; if(random <= factor) return arr1[i]; }; return null; }; function randomNum(minNum,maxNum){ switch(arguments.length){ case 1: return parseInt(Math.random()*minNum+1,10); break; case 2: return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); break; default: return 0; break; } } //调用的方法 console.log(clickme(a,b)); |
以上是js源代码写出,有什么问题请留言交流。