• php 积分抽奖活动(大转盘)


    以下是项目代码(公众号,使用积分进行抽奖活动),只可做参考:

    public function Sncode()
    {
    $tid = I('request.tid', 0, 'intval'); // 大转盘id,
    $wid = I('request.wid', 0, 'intval'); // 应用id,可去掉
    $token = $this->getOpenId();       // 获取微信用户的token

    // 定义数据库
    $big_wheel = D('Big_wheel'); // 大转盘设置表
    $big_wheel_log = M('big_wheel_log'); // 抽奖记录表
    $big_code = M('big_wheel_code'); // 已抽中奖品记录表

    // 查询抽奖活动数据
    $where = " `id` = " . $tid;
    $list = $big_wheel->relation(true)->where($where)->find();

    // 查询积分,若积分不足则无法参与抽奖
    $score = $this->duser['score'];
    if ($score < $list['score_num']) {
    echo json_encode(array('error' => 'score'));
    exit;
    }

    //已抽奖总次数
    $where_log = "`big_wheel_id`=" . $tid . " and `w_id`=" . $list['w_id'] . " and `code`='" . $token . "'";
    $count = $big_wheel_log->where($where_log)->count();

    //减去额外获得的抽奖机会 - 分享(此操作暂时隐藏)
    $map_share['wid'] = array('eq', $wid);
    $map_share['token'] = array('eq', $token);
    $map_share['big_wheel_id'] = array('eq', $tid);
    $count_share = M('big_wheel_share')->where($map_share)->count();
    $count = $count - $count_share;


    //当日限制次数
    if ($list['most_num_times'] > 0) {
    $where_log .= " and (addtime BETWEEN " . strtotime(date('Y-m-d 00:00:00')) . " and " . strtotime(date('Y-m-d 23:59:59')) . ")";
    $count_today = $big_wheel_log->where($where_log)->count();
    $count_today = $count_today - $count_share;// 当日抽奖次数减去额外获得的抽奖次数
        }

    //判断抽奖次数
    if ($count_today >= $list['most_num_times'] && $list['most_num_times'] > 0) {
    echo json_encode(array('error' => 'invalid_today', 'nums' => $list['most_num_times']));
    exit;
    }

    // 扣除积分
    M('app_dishop_duser')->where(array('id' => $this->duser['id']))->setDec('score', $list['score_num']);

    //插入抽奖记录表
    $data['big_wheel_id'] = $tid;
    $data['w_id'] = $list['w_id'];
    $data['code'] = $token;
    $data['date'] = date('Y-m-d', time());
    $data['addtime'] = time();
    $big_wheel_log->add($data);

    /*
    * 奖项数组
    * 是一个二维数组,记录了所有本次抽奖的奖项信息,
    * 其中id表示中奖等级,prize表示奖品,v表示中奖概率。
    * 注意其中的v必须为整数,你可以将对应的 奖项的v设置成0,即意味着该奖项抽中的几率是0,
    * 数组中v的总和(基数),基数越大越能体现概率的准确性。
    * 本例中v的总和为100,那么中奖概率就是1%,
    * 如果v的总和是10000,那中奖概率就是万分之一了。
    *
    */
    $prize_arr = array(
    '0' => array('id' => 1, 'prize' => '1', 'v' => $list['c_probability_one']),
    '1' => array('id' => 2, 'prize' => '谢谢参与', 'v' => $list['no_probability']),
    '2' => array('id' => 3, 'prize' => '2', 'v' => $list['c_probability_two']),
    '3' => array('id' => 4, 'prize' => '3', 'v' => $list['c_probability_three']),
    );

    foreach ($prize_arr as $key => $val) {
    $arr[$val['id']] = $val['v'];
    }

    $rid = $this->get_rand($arr); //根据概率获取奖项id

    $res['yes'] = $prize_arr[$rid - 1]['prize']; //中奖项
    unset($prize_arr[$rid - 1]); //将中奖项从数组中剔除,剩下未中奖项
    shuffle($prize_arr); //打乱数组顺序
    for ($i = 0; $i < count($prize_arr); $i++) {
    $pr[] = $prize_arr[$i]['prize'];
    }

    // 若抽中奖品,则存入库中,并返回提示
    if ($res['yes'] == '1' || $res['yes'] == '2' || $res['yes'] == '3') {
    //这个是保存到数据库表示你这个人抽中奖品
    if ($res['yes'] == '1') {
    // 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
    $code_count = $big_code->where(array('prizetype' => 1, 'big_wheel_id' => $tid))->count();
    if ($code_count >= $list['c_num_one']) {
    $data = array('prizetype' => null, 'success' => true);
    echo json_encode($data);
    exit;
    }
    $code['category'] = $list['c_name_one'];
    } elseif ($res['yes'] == '2') {
    // 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
    $code_count = $big_code->where(array('prizetype' => 2, 'big_wheel_id' => $tid))->count();
    if ($code_count >= $list['c_num_two']) {
    $data = array('prizetype' => null, 'success' => true);
    echo json_encode($data);
    exit;
    }
    $code['category'] = $list['c_name_two'];
    } elseif ($res['yes'] == '3') {
    // 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
    $code_count = $big_code->where(array('prizetype' => 3, 'big_wheel_id' => $tid))->count();
    if ($code_count >= $list['c_num_three']) {
    $data = array('prizetype' => null, 'success' => true);
    echo json_encode($data);
    exit;
    }
    $code['category'] = $list['c_name_three'];
    }
    $code['sn_id'] = 'sn' . time() . mt_rand(10000, 99999);
    $code['w_id'] = $list['w_id'];
    $code['big_wheel_id'] = $tid;
    $code['prizetype'] = $res['yes'];
    $code['code'] = $token;
    $code['winners_time'] = time();
    $code['state'] = 1;
    $big_code->add($code);
    $data = array('sn' => $code['sn_id'], 'prizetype' => $res['yes'], 'success' => true);
    echo json_encode($data);
    exit;
    } else {
    $data = array('prizetype' => null, 'success' => true);
    echo json_encode($data);
    exit;
    }
    }

    // 抽奖概率计算
    public function get_rand($proArr)
    {
    $result = '';

    //概率数组的总概率精度
    $proSum = array_sum($proArr);

    //概率数组循环
    foreach ($proArr as $key => $proCur) {
    $randNum = mt_rand(1, $proSum);
    if ($randNum <= $proCur) {
    $result = $key;
    break;
    } else {
    $proSum -= $proCur;
    }
    }
    unset ($proArr);
    return $result;
    }
  • 相关阅读:
    pikachu-xss(1)
    eNSP上配置RIPv2的认证
    eNSP模拟器OSPF单区域配置
    OSPF与ACL综合实验
    利用单臂路由实现vlan间路由
    理解Hybrid接口的应用
    eNSP下配置Trunk接口实现跨交换机传递数据
    eNSP上VLAN的基础的配置及access接口
    eNSP下利用三层交换机实现VLAN间路由
    NFS网络文件系统
  • 原文地址:https://www.cnblogs.com/jiaoda/p/10621796.html
Copyright © 2020-2023  润新知