记录:随机数抽奖
要求:每次生成3个 1 - 10 之间可重复(或不可重复)的随机数,10次为一轮,每轮要求数字5出现6次、数字4出现3次、…。
提炼需求:
1,可设置最小数、最大数、每次抽奖生成随机数的个数、是否允许重复
2,可设置每轮指定数字的出现次数
3,可设置每轮的抽奖次数
注意:设置项发生变化,则重置游戏
效果图示例:
Choujianglog 模型
<?phpnamespace app\admin\model\choujiang;use think\Model;class Choujianglog extends Model
{// 表名protected $name = 'choujiang_log';// 自动写入时间戳字段protected $autoWriteTimestamp = 'integer';// 定义时间戳字段名protected $createTime = 'createtime';protected $updateTime = false;protected $deleteTime = false;// 追加属性protected $append = [];// 获取抽奖当前轮次public function getLunci($choujiang_id, $yilunchoujiagcishu) {$lunci = $this->where(['choujiang_id'=>$choujiang_id])->max('lunci');if(!$lunci){$lunci = 1;}return $lunci;}// 获取抽奖接下来的轮次public function getnextLunci($choujiang_id, $yilunchoujiagcishu) {$lunci = $this->where(['choujiang_id'=>$choujiang_id])->max('lunci');if(!$lunci){$lunci = 1;}$choujiangcishu = $this->where(['choujiang_id'=>$choujiang_id, 'lunci'=>$lunci])->group('sign')->count();if($choujiangcishu >= $yilunchoujiagcishu){$lunci += 1;}return $lunci;}// 生成signpublic function createSign() {$str = '123567890123567890123567890';//34个字符$str = str_shuffle($str); //随机地打乱字符串中的所有字符$sign = date('YmdHis').substr($str,0,4); //截取字符串if($this->where(['sign'=>$sign])->value('sign')){$this->createSign();}else{return $sign;}}}
Choujiang模型
<?phpnamespace app\admin\model\choujiang;use think\Model;class Choujiang extends Model
{// 表名protected $name = 'choujiang';// 自动写入时间戳字段protected $autoWriteTimestamp = 'integer';// 定义时间戳字段名protected $createTime = 'createtime';protected $updateTime = 'updatetime';protected $deleteTime = false;// 追加属性protected $append = ['state_text'];public function getStateList(){return ['1' => __('State 1'), '2' => __('State 2')];}public function getStateTextAttr($value, $data){$value = $value ? $value : (isset($data['state']) ? $data['state'] : '');$list = $this->getStateList();return isset($list[$value]) ? $list[$value] : '';}}
Choujiangset模型
<?phpnamespace app\admin\model\choujiang;use think\Model;class Choujiangset extends Model
{// 表名protected $name = 'choujiang_set';// 自动写入时间戳字段protected $autoWriteTimestamp = false;// 定义时间戳字段名protected $createTime = false;protected $updateTime = false;protected $deleteTime = false;// 追加属性protected $append = ['state_text'];public function getStateList(){return ['1' => __('State 1'), '2' => __('State 2')];}public function getStateTextAttr($value, $data){$value = $value ? $value : (isset($data['state']) ? $data['state'] : '');$list = $this->getStateList();return isset($list[$value]) ? $list[$value] : '';}}
代码:
<?phpnamespace app\api\controller;use app\common\controller\Api;
use think\Exception