示例#1
0
 public function detail()
 {
     //echo generateExchangeCode();exit();
     $user_id = intval($this->user['user_id']);
     //$user_id = 496;
     if (!$user_id) {
         $this->errorOutput('登录才能参加抽奖哦!');
     }
     $id = intval($this->input['id']);
     $sort_id = intval($this->input['sort_id']);
     if ($this->input['shenwen']) {
         if (!$this->curl) {
             return array();
         }
         if ($_FILES) {
             $file = $_FILES;
         }
         $this->curl->setSubmitType('post');
         $this->curl->setReturnFormat('json');
         $this->curl->initPostData();
         $this->curl->addFile($file);
         $this->curl->addRequestData('shenwen', $this->input['shenwen']);
         $this->curl->addRequestData('a', 'show');
         $ret = $this->curl->request('shenwen.php');
         if ($ret['channel_name']) {
             $sql = "SELECT id FROM " . DB_PREFIX . "sort WHERE name LIME '" . $ret['channel_name'] . "%'";
             $res = $this->db->query_first($sql);
             if ($res['id']) {
                 $sort_id = $res['id'];
             }
         }
     }
     $data = array();
     if ($this->settings['lottery_filter']) {
         $sql = "SELECT * FROM " . DB_PREFIX . "lottery_filter WHERE 1 ";
         if ($sort_id) {
             $sql .= "AND sort_id = " . $sort_id . " ORDER BY order_id DESC LIMIT 0,1";
         } else {
             if ($id) {
                 $sql .= "AND id = " . $id;
             } else {
                 $sql .= "ORDER BY order_id DESC LIMIT 0,1";
             }
         }
         $res = $this->db->query_first($sql);
         if ($res['content']) {
             $data = unserialize($res['content']);
             $data['end_hours'] = $data['end_hour'];
             $data['start_hours'] = $data['start_hour'];
             $id = $data['id'];
             if ($res['win_info']) {
                 $data['win_info'] = unserialize($res['win_info']);
             }
         }
     }
     //hg_pre($data,0);
     if (empty($data)) {
         if ($sort_id) {
             $sql = "SELECT id FROM " . DB_PREFIX . "lottery WHERE status = 1 AND sort_id = " . $sort_id . " \n\t\t\t\t\t\tORDER BY order_id DESC LIMIT 0,1";
             $res = $this->db->query_first($sql);
             $id = $res['id'] ? $res['id'] : 0;
         }
     }
     $id = $id ? $id : intval($this->input['id']);
     if (!$id) {
         $this->errorOutput('不存在此活动');
     }
     if (empty($data)) {
         include_once CUR_CONF_PATH . 'lib/lottery_mode.php';
         $obj = new lottery_mode();
         $data = $obj->detail($id);
     }
     if (!$data['id'] || $data['status'] != 1) {
         $this->errorOutput('不存在此活动');
     }
     //hg_pre($data,0);
     //时间和周期判断
     if ($data['time_limit']) {
         $week_now = date('w', TIMENOW);
         $hour_now = date('His', TIMENOW);
         $day_now = date('d', TIMENOW);
         $notstartdesc = $data['notstartdesc'];
         if (!$notstartdesc) {
             $notstartdesc = $this->settings['notstartdesc'] ? $this->settings['notstartdesc'] : '活动尚未开始, 敬请期待';
         }
         if ($data['start_times'] > TIMENOW || $data['start_hours'] > $hour_now) {
             $this->errorOutput($notstartdesc);
         }
         if ($data['end_times'] < TIMENOW || $hour_now > $data['end_hours']) {
             $message = $data['finish_desc'];
             if (!$message) {
                 $message = $this->settings['finish_desc'] ? $this->settings['finish_desc'] : '活动已结束, 敬请期待下次活动1.';
             }
             $this->errorOutput($message);
         }
         if ($data['cycle_type'] && $data['cycle_value']) {
             $data['cycle_value'] = explode(',', $data['cycle_value']);
             if ($week_now == 0) {
                 $week_now = 1;
             }
             if ($data['cycle_type'] == 1 && !in_array($week_now, $data['cycle_value'])) {
                 $this->errorOutput($notstartdesc);
             } else {
                 if ($data['cycle_type'] == 2 && !in_array($day_now, $data['cycle_value'])) {
                     $this->errorOutput($notstartdesc);
                 }
             }
         }
     }
     //中奖限制
     if ($data['lottery_limit']) {
         $sql = "SELECT count(*) as total FROM " . DB_PREFIX . "win_info WHERE prize_id != 0 AND confirm = 1 AND member_id = " . $user_id . " AND lottery_id = " . $data['id'];
         $res = $this->db->query_first($sql);
         if ($res['total']) {
             $message = $this->settings['lottery_limit_tip'] ? $this->settings['lottery_limit_tip'] : '您已中奖,谢谢参与!';
             $this->errorOutput($message);
         }
     }
     //积分限制
     if ($data['score_limit'] && $data['need_score'] > 0) {
         include_once ROOT_PATH . 'lib/class/members.class.php';
         $mem_obj = new members();
         $credit = $mem_obj->get_member_credits($user_id);
         if ($credit[$user_id]['credits'] < $data['need_score']) {
             $this->errorOutput('对不起,您的积分不够了!');
         }
     }
     //限制ip
     if ($data['ip_limit']) {
         $ip_limit_hour = $data['ip_limit_time'] ? $data['ip_limit_time'] : 1;
         $ip_limit_num = $data['ip_limit_num'] ? $data['ip_limit_num'] : 1;
         $ip_limit_time = TIMENOW - $ip_limit_hour * 3600;
         $ip = hg_getip();
         //查询用户参加此活动次数
         $sql = "SELECT count(*) as total FROM " . DB_PREFIX . "win_info WHERE confirm = 1 AND ip = '" . $ip . "' AND lottery_id = " . $data['id'] . " AND create_time > " . $ip_limit_time;
         $res = $this->db->query_first($sql);
         //达到ip限制次数
         if ($res['total'] >= $data['ip_limit_num']) {
             $this->errorOutput('您已参加活动,下一时段活动再来吧!');
         }
     }
     //限制设备标识
     $device_token = $this->input['device_token'];
     if ($data['device_limit'] && $device_token) {
         $device_limit_hour = $data['device_limit_time'] ? $data['device_limit_time'] : 1;
         $device_num_limit = $data['device_num_limit'] ? $data['device_num_limit'] : 1;
         $device_limit_time = TIMENOW - $device_limit_hour * 3600;
         //查询用户参加此活动次数
         $sql = "SELECT count(*) as total FROM " . DB_PREFIX . "win_info WHERE confirm = 1 AND device_token = '" . $device_token . "' AND lottery_id = " . $data['id'] . " AND create_time > " . $device_limit_time;
         $res = $this->db->query_first($sql);
         //达到ip限制次数
         if ($res['total'] >= $data['device_num_limit']) {
             $this->errorOutput('您已参加活动,下一时段活动再来吧!');
         }
     }
     //限制用户参加活动次数
     if ($data['num_limit'] && $data['account_limit']) {
         //查询用户参加此活动次数
         $sql = "SELECT count(*) as total FROM " . DB_PREFIX . "win_info WHERE confirm = 1 AND member_id = " . $user_id . " AND lottery_id = " . $data['id'];
         $res = $this->db->query_first($sql);
         //查询额外奖励抽奖次数
         $sql = "SELECT reward_num FROM " . DB_PREFIX . "reward WHERE member_id = {$user_id} AND lottery_id = {$id}";
         $reward = $this->db->query_first($sql);
         $reward_num = $reward['reward_num'];
         if ($reward_num) {
             $data['account_limit'] += $reward_num;
         }
         //达到活动限制次数
         if ($res['total'] >= $data['account_limit']) {
             $this->errorOutput('您已参加活动,下一时段活动再来吧!');
         }
     }
     //区域限制
     if ($data['area_limit']) {
         $distance = '';
         if ($this->input['GPS_longitude'] || $this->input['GPS_latitude']) {
             //计算距离
             if ($data['GPS_latitude'] && $data['GPS_longitude']) {
                 $distance = GetDistance($data['GPS_latitude'], $data['GPS_longitude'], $this->input['GPS_latitude'], $this->input['GPS_longitude']);
             }
         } elseif ($this->input['baidu_longitude'] || $this->input['baidu_latitude']) {
             if ($data['baidu_latitude'] && $data['baidu_longitude']) {
                 $distance = GetDistance($data['baidu_latitude'], $data['baidu_longitude'], $this->input['baidu_latitude'], $this->input['baidu_longitude']);
             }
         }
         if ($distance > $data['distance']) {
             $this->errorOutput('您不在抽奖范围内,请到' . $data['address'] . '附近。');
         }
     }
     //版本限制
     if ($data['version_limit'] && $data['version_limit'] > $this->input['version']) {
         $this->errorOutput('您的应用版本过低,升级后再来吧!');
     }
     //未中奖反馈
     $feedback = $data['feedback'];
     $arr = $prize = $award = array();
     //查询活动奖品
     $prize_arr = array();
     $prize_arr = $data['prize'];
     //中奖次数限制
     $win_limit_flag = false;
     if ($data['win_limit']) {
         if (!$data['win_num_limit']) {
             $win_limit_flag = true;
         } else {
             $today_start = strtotime(date('Y-m-d', TIMENOW));
             $today_end = $today_start + 86400;
             $sql = "SELECT count(*) as total FROM " . DB_PREFIX . "win_info WHERE confirm = 1 AND prize_id != '' AND create_time >= '" . $today_start . "' AND create_time < '" . $today_end . "' AND lottery_id = " . $data['id'];
             $res = $this->db->query_first($sql);
             if ($res['total'] >= $data['win_num_limit']) {
                 $win_limit_flag = true;
             }
         }
     }
     if (empty($prize_arr) && !$win_limit_flag) {
         //查询奖项
         $sql = 'SELECT p.*,m.host,m.dir,m.filepath,m.filename FROM ' . DB_PREFIX . "prize p  \n\t\t\t\t\tLEFT JOIN " . DB_PREFIX . "materials m \n\t\t\t\t\t\tON p.indexpic_id = m.id \n\t\t\t\t\tWHERE p.lottery_id = {$data['id']} ORDER BY id ASC";
         $q = $this->db->query($sql);
         while ($row = $this->db->fetch_array($q)) {
             $prize_arr[$row['id']] = $row;
         }
     }
     $sum = array();
     $arr = array();
     if (is_array($prize_arr) && count($prize_arr) && !$win_limit_flag) {
         foreach ($prize_arr as $key => $val) {
             if ($val['prize_win'] >= $val['prize_num']) {
                 continue;
             }
             $chance = array();
             $chance = explode('/', $val['chance']);
             if (!$chance[1]) {
                 continue;
             }
             $arr[$val['id']] = $chance[0];
             $sum[$val['id']] = $chance[1];
         }
     }
     $prize_id = '';
     if ($sum && $arr) {
         $prize_id = get_rand($arr, $sum);
         //根据概率获取奖项id
     }
     $prize_id = $prize_id ? $prize_id : 0;
     $award['lottery_id'] = $data['id'];
     //抽奖活动id
     $award['score_limit'] = $data['score_limit'];
     //积分限制
     $award['need_score'] = $data['need_score'];
     //需要积分
     $award['id'] = $prize_id;
     //奖品id
     $award['win_info'] = $data['win_info'] ? $data['win_info'] : array();
     $award['indexpic'] = $data['indexpic'] ? $data['indexpic'] : array();
     if ($prize_id) {
         $prize = $prize_arr[$prize_id];
         $prize_indexpic = array('host' => $prize['host'], 'dir' => $prize['dir'], 'filepath' => $prize['filepath'], 'filename' => $prize['filename']);
         $award['prize'] = $prize['prize'];
         //奖品名称
         $award['name'] = $prize['name'];
         //奖项名称
         $award['tip'] = $prize['tip'];
         //奖品名称
         $award['prize_indexpic'] = $prize_indexpic;
         //奖品索引图
     } else {
         $feedback_count = count($feedback);
         $rand_num = mt_rand(0, $feedback_count - 1);
         $award['tip'] = $feedback[$rand_num];
         //奖品名称
         $award['name'] = '谢谢参与';
         //奖项名称
     }
     //记录获奖记录
     $lottery_data = array('lottery_id' => $data['id'], 'prize_id' => $prize_id, 'create_time' => TIMENOW);
     $send_no = md5(uniqid(rand(), true));
     $lottery_data['sendno'] = $award['sendno'] = $send_no;
     //中奖随机串
     if ($data['score_limit'] && $data['need_score'] != 0) {
         $award['scores'] = $data['need_score'] < 0 ? abs($data['need_score']) : '-' . $data['need_score'];
     }
     $sql = " INSERT INTO " . DB_PREFIX . "win_info SET ";
     foreach ($lottery_data as $k => $v) {
         $sql .= " {$k} = '{$v}',";
     }
     $sql = trim($sql, ',');
     $this->db->query($sql);
     $wininfo_id = $this->db->insert_id();
     //中奖信息记录成功
     if ($wininfo_id && $this->settings['lock_stock']) {
         //更新库存
         $sql = "UPDATE " . DB_PREFIX . "prize SET prize_win = prize_win + 1 WHERE id = " . $prize_id;
         $this->db->query($sql);
         //记录锁止库存表
         $lock_data = array('send_no' => $send_no, 'prize_id' => $prize_id, 'member_id' => $user_id, 'lottery_id' => $data['id'], 'create_time' => TIMENOW);
         $sql = " INSERT INTO " . DB_PREFIX . "stock_lock SET ";
         foreach ($lock_data as $k => $v) {
             $sql .= " {$k} = '{$v}',";
         }
         $sql = trim($sql, ',');
         $this->db->query($sql);
     }
     if ($this->input['tv_interact']) {
         $this->input['sendno'] = $lottery_data['sendno'];
         $this->update_win_info();
     }
     $this->addItem($award);
     $this->output();
 }