function ajax_data()
 {
     $public_info = get_token_appinfo();
     $this->assign('public_info', $public_info);
     $id = I('id');
     $data = D('Scratch')->getScratchInfo($id);
     $this->assign('data', $data);
     // dump($data);
     // 奖项
     $addon = 'Scratch';
     $prizes = D('Prize')->getPrizes($id, $addon);
     $this->assign('prizes', $prizes);
     // 抽奖记录
     // $all_prizes = M ( 'sn_code' )->where ( $map )->order ( 'id desc' )->select ();
     $all_prizes = D('SnCode')->getSnCodes($id, $addon);
     // dump ( $all_prizes );
     foreach ($all_prizes as $all) {
         if ($all['prize_id'] > 0) {
             $has[$all['prize_id']] += 1;
             // 每个奖项已经中过的次数
             $new_prizes[] = $all;
             // 最新中奖记录
             $all['uid'] == $this->mid && ($my_prizes[] = $all);
             // 我的中奖记录
         } else {
             $no_count += 1;
             // 没有中奖的次数
         }
         // 记录我已抽奖的次数
         $all['uid'] == $this->mid && ($my_count += 1);
     }
     $this->assign('new_prizes', $new_prizes);
     $this->assign('my_prizes', $my_prizes);
     // dump ( $new_prizes );
     // dump ( $my_prizes );
     // 权限判断
     $follow = get_followinfo($this->mid);
     $is_admin = is_login();
     $error = '';
     if ($data['start_time'] > time()) {
         $error = '活动还没开始';
     }
     if ($data['end_time'] <= time()) {
         $error = '活动已结束';
     } else {
         if ($data['max_num'] > 0 && $data['max_num'] <= $my_count) {
             $error = '您的刮卡机会已用完啦';
         } else {
             if ($data['follower_condtion'] > intval($follow['status']) && !$is_admin) {
                 switch ($data['follower_condtion']) {
                     case 1:
                         $error = '关注后才能参与';
                         break;
                     case 2:
                         $error = '用户绑定后才能参与';
                         break;
                     case 3:
                         $error = '领取会员卡后才能参与';
                         break;
                 }
             } else {
                 if ($data['credit_conditon'] > intval($follow['score']) && !$is_admin) {
                     $error = '您的金币值不足';
                 } else {
                     if ($data['credit_bug'] > intval($follow['score']) && !$is_admin) {
                         $error = '您的金币值不够扣除';
                     } else {
                         if (!empty($data['addon_condition'])) {
                             addon_condition_check($data['addon_condition']) || ($error = '您没权限参与');
                         }
                     }
                 }
             }
         }
     }
     $this->assign('error', $error);
     // 抽奖算法
     if (empty($error)) {
         $prize = D('Scratch')->_lottery($data, $prizes, $new_prizes, $my_count, $has, $no_count);
         $prizes = D('Prize')->getPrizes($id, $addon);
         $prize['img'] = get_cover_url($prize['img']);
         $this->assign('prize', $prize);
     }
     $content = $this->fetch(ONETHINK_ADDON_PATH . 'Scratch/View/default/Scratch/data.html');
     $returnData = I('callback') . '({"html":"' . rawurlencode($content) . '","prizeJson":"' . rawurlencode(json_encode($prize)) . '"})';
     echo $returnData;
     exit;
 }
 function show()
 {
     $id = $map['target_id'] = I('id');
     $data = M('scratch')->find($id);
     $this->assign('data', $data);
     // dump($data);
     // 奖项
     $map['addon'] = 'Scratch';
     $prizes = M('prize')->where($map)->select();
     $this->assign('prizes', $prizes);
     // 抽奖记录
     $all_prizes = M('sn_code')->where($map)->order('id desc')->select();
     // dump ( $all_prizes );
     foreach ($all_prizes as $all) {
         if ($all['prize_id'] > 0) {
             $has[$all['prize_id']] += 1;
             // 每个奖项已经中过的次数
             $new_prizes[] = $all;
             // 最新中奖记录
             $all['uid'] == $this->mid && ($my_prizes[] = $all);
             // 我的中奖记录
         } else {
             $no_count += 1;
             // 没有中奖的次数
         }
         // 记录我已抽奖的次数
         $all['uid'] == $this->mid && ($my_count += 1);
     }
     $this->assign('new_prizes', $new_prizes);
     $this->assign('my_prizes', $my_prizes);
     // dump ( $new_prizes );
     // dump ( $my_prizes );
     // 权限判断
     unset($map);
     $map['token'] = get_token();
     $map['openid'] = get_openid();
     $follow = M('follow')->where($map)->find();
     $is_admin = is_login();
     $error = '';
     if ($data['end_time'] <= time()) {
         $error = '活动已结束';
     } else {
         if ($data['max_num'] > 0 && $data['max_num'] <= $my_count) {
             $error = '您的刮卡机会已用完啦';
         } else {
             if ($data['follower_condtion'] > intval($follow['status']) && !$is_admin) {
                 switch ($data['follower_condtion']) {
                     case 1:
                         $error = '关注后才能参与';
                         break;
                     case 2:
                         $error = '用户绑定后才能参与';
                         break;
                     case 3:
                         $error = '领取会员卡后才能参与';
                         break;
                 }
             } else {
                 if ($data['credit_conditon'] > intval($follow['score']) && !$is_admin) {
                     $error = '您的财富值不足';
                 } else {
                     if ($data['credit_bug'] > intval($follow['score']) && !$is_admin) {
                         $error = '您的财富值不够扣除';
                     } else {
                         if (!empty($data['addon_condition'])) {
                             addon_condition_check($data['addon_condition']) || ($error = '您没权限参与');
                         }
                     }
                 }
             }
         }
     }
     $this->assign('error', $error);
     // 抽奖算法
     empty($error) || $this->_lottery($data, $prizes, $new_prizes, $my_count, $has, $no_count);
     $this->display('show');
 }
 function getPackageData($id)
 {
     $return['public_info'] = $public_info = get_token_appinfo();
     $id = $map['target_id'] = I('id');
     $return['data'] = $data = M('scratch')->find($id);
     // dump($data);
     // 奖项
     $map['addon'] = 'Scratch';
     $return['prizes'] = $prizes = M('prize')->where($map)->select();
     // 抽奖记录
     $all_prizes = M('sn_code')->where($map)->order('id desc')->select();
     // dump ( $all_prizes );
     foreach ($all_prizes as $all) {
         if ($all['prize_id'] > 0) {
             $has[$all['prize_id']] += 1;
             // 每个奖项已经中过的次数
             $new_prizes[] = $all;
             // 最新中奖记录
             $all['uid'] == $GLOBALS['mid'] && ($my_prizes[] = $all);
             // 我的中奖记录
         } else {
             $no_count += 1;
             // 没有中奖的次数
         }
         // 记录我已抽奖的次数
         $all['uid'] == $GLOBALS['mid'] && ($my_count += 1);
     }
     $return['new_prizes'] = $new_prizes;
     $return['my_prizes'] = $my_prizes;
     // dump ( $new_prizes );
     // dump ( $my_prizes );
     // 权限判断
     unset($map);
     $follow = get_followinfo($GLOBALS['mid']);
     $is_admin = is_login();
     $error = '';
     if ($data['end_time'] <= time()) {
         $error = '活动已结束';
     } else {
         if ($data['max_num'] > 0 && $data['max_num'] <= $my_count) {
             $error = '您的刮卡机会已用完啦';
         } else {
             if ($data['follower_condtion'] > intval($follow['status']) && !$is_admin) {
                 switch ($data['follower_condtion']) {
                     case 1:
                         $error = '关注后才能参与';
                         break;
                     case 2:
                         $error = '用户绑定后才能参与';
                         break;
                     case 3:
                         $error = '领取会员卡后才能参与';
                         break;
                 }
             } else {
                 if ($data['credit_conditon'] > intval($follow['score']) && !$is_admin) {
                     $error = '您的金币值不足';
                 } else {
                     if ($data['credit_bug'] > intval($follow['score']) && !$is_admin) {
                         $error = '您的金币值不够扣除';
                     } else {
                         if (!empty($data['addon_condition'])) {
                             addon_condition_check($data['addon_condition']) || ($error = '您没权限参与');
                         }
                     }
                 }
             }
         }
     }
     $return['error'] = $error;
     // 抽奖算法
     if (empty($error)) {
         $return['prize'] = $this->_lottery($data, $prizes, $new_prizes, $my_count, $has, $no_count);
     }
     // 添加模板目录
     $return['template'] = $data['template'] == "" ? "default" : $data['template'];
     return $return;
 }
 function _get_error($data, $my_count = false)
 {
     $error = '';
     // 抽奖记录
     if ($my_count === false) {
         $map2['target_id'] = $data['id'];
         $map2['uid'] = $this->mid;
         $my_count = M('sn_code')->where($map2)->count();
     }
     // 权限判断
     $map['token'] = get_token();
     $map['openid'] = get_openid();
     $follow = M('follow')->where($map)->find();
     $is_admin = is_login();
     if (!empty($data['end_time']) && $data['end_time'] <= time()) {
         $error = '您来晚啦';
     } else {
         if ($data['max_num'] > 0 && $data['max_num'] <= $my_count) {
             $error = '您的领取名额已用完啦';
         } else {
             if ($data['follower_condtion'] > intval($follow['status']) && !$is_admin) {
                 switch ($data['follower_condtion']) {
                     case 1:
                         $error = '关注后才能领取';
                         break;
                     case 2:
                         $error = '用户绑定后才能领取';
                         break;
                     case 3:
                         $error = '领取会员卡后才能领取';
                         break;
                 }
             } else {
                 if ($data['credit_conditon'] > intval($follow['score']) && !$is_admin) {
                     $error = '您的财富值不足';
                 } else {
                     if ($data['credit_bug'] > intval($follow['score']) && !$is_admin) {
                         $error = '您的财富值不够扣除';
                     } else {
                         if (!empty($data['addon_condition'])) {
                             addon_condition_check($data['addon_condition']) || ($error = '权限不足');
                         }
                     }
                 }
             }
         }
     }
     $this->assign('error', $error);
     // dump ( $error );
     return $error;
 }