You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.9 KiB
PHTML

5 years ago
<?php
namespace Sdk\Controller;
use Think\Controller;
use Common\Api\GaemApi;
5 years ago
class GameGiftPageController extends Controller{
5 years ago
5 years ago
public $userId;
public function _initialize()
{
$userToken = I("request.user_token");
if (!$userToken) {
re_msg(11, "参数有误,请重新登入获取!");
}
$this->userId = M("user", "tab_")->where(['user_token' => $userToken])->getField("id");
$this->userToken = $userToken;
if (!$this->userId) {
re_msg(12, $userToken."Token失效");
}
}
5 years ago
public function gift_list(){
5 years ago
$gameId = I('game_id');
5 years ago
$sdkVersion = I('sdk_version', -1);
if (!$gameId || $sdkVersion == -1) {
5 years ago
$this->error("参数有误");
}
5 years ago
if (I("debug") == 1) {
$data['user_token'] = 'b9c9739f7e3dc0b338dc3f4a4f02cb29';
}
5 years ago
$gift = M("Giftbag","tab_");
5 years ago
$map['game_id'] = $gameId;
5 years ago
$map['status']=1;
5 years ago
switch ($sdkVersion) {
5 years ago
case 1:
case 2:
5 years ago
$map['sdk_version'] = $sdkVersion;
5 years ago
break;
}
$list = $gift
->field("id,giftbag_name,novice,start_time,end_time,desribe,game_name")
->where($map)
->select();
//遍历数据获取游戏图片地址
foreach ($list as $key => $val) {
5 years ago
$res = M('gift_record','tab_')->field('id')->where(['gift_id' => $val['id'],'user_id' => $this->userId])->find();
5 years ago
if($res){
$list[$key]['received']=1;
}else{
$list[$key]['received']=0;
}
5 years ago
if($val['end_time'] != 0 && NOW_TIME > $val['end_time']) {
5 years ago
unset($list[$key]);
}
unset($list[$key]['novice']);
$list[$key]['icon'] = new_icon_url($val['icon']);
$list[$key]['now_time'] = NOW_TIME;
}
5 years ago
$game = M('game','tab_')->where(['id' => $gameId])->field('icon,game_name')->find();
5 years ago
$game['icon'] = new_icon_url($game['icon']);
$game['gift_num'] = count($list);
5 years ago
5 years ago
$this->assign('user_token', I('user_token'));
$this->assign('game_id',$gameId);
5 years ago
$this->assign('game',$game);
$this->assign('data',$list);
$this->display('lists');
}
5 years ago
public function details($gift_id = '',$user_token = '')
5 years ago
{
if(empty($gift_id))
{
$this->error('礼包不存在');
}
$content = M('Giftbag','tab_')->where(['id' => $gift_id])->field('id,novice,giftbag_name,start_time,end_time,desribe,digest')->find();
5 years ago
$novice = M('gift_record','tab_')->where(['gift_id' => $gift_id,'user_id' => $this->userId])->getField('novice');
5 years ago
$content['start_time'] = date('Y-m-d H:i:s',$content['start_time']);
$content['end_time'] = $content['end_time'] == 0?'长期有效':date('Y-m-d H:i:s',$content['start_time']);
if($content['novice']!='')
{
$novice_num = count(explode(',', $content['novice']));
$content['novice_num'] = $novice_num;//是否有剩余
}else{
$content['novice_num'] = 0;//是否剩余
}
5 years ago
$this->assign('user_token',$user_token);
5 years ago
$this->assign('content',$content);
$this->assign('novice',$novice);
$this->display('details');
}
5 years ago
/**
* 礼包领取
* @param number $gift_id
* @param number $user_token
* @param number $game_id
* @param string $game_name
*/
5 years ago
public function receive($gift_id = 0,$user_token = 0,$game_id = 0,$game_name = "")
5 years ago
{
5 years ago
5 years ago
if(empty($gift_id))
{
$this->error('礼包不存在');
}
$content = M('Giftbag','tab_')->where(['id' => $gift_id])->field('id,novice,giftbag_name,start_time,end_time,desribe,digest')->find();
$content['start_time'] = date('Y-m-d H:i:s',$content['start_time']);
$content['end_time'] = $content['end_time'] == 0?'长期有效':date('Y-m-d H:i:s',$content['start_time']);
if($content['novice']!='')
{
$novice = count(explode(',', $content['novice']));
$content['novice_num'] = $novice;//是否有剩余
}else{
$content['novice_num'] = 0;//是否剩余
}
5 years ago
$res = M('gift_record','tab_')->field('id')->where(['gift_id' => $gift_id,'user_id' => $this->userId])->find();
5 years ago
if($res){
$content['received']=1;
}else{
$content['received']=0;
}
5 years ago
$this->assign('user_token',$user_token);
5 years ago
$this->assign('gift_id',$gift_id);
$this->assign('game_id',$game_id);
$this->assign('game_name',$game_name);
$this->assign('content',$content);
$this->display('receive');
}
/**
*领取礼包
*/
5 years ago
public function receive_gift($user_token = 0,$gift_id = 0,$game_id = 0,$game_name = ""){
5 years ago
5 years ago
#获取SDK上POST方式传过来的数据 然后base64解密 然后将json字符串转化成数组
$post = I('post.');
5 years ago
$user_id = $this->userId;
5 years ago
$gift_id = $post['gift_id'];
$game_id = $post['game_id'];
$game_name = $post['game_name'];
$gift = M('giftbag','tab_');
$novice = $gift->where(["id" => $gift_id])->field("id,giftbag_name,novice")->find();
if(empty($novice['novice'])){
echo "该礼包已被领完";die();
}
else{
#将激活码分成数据
$novice_arr = explode(",",$novice['novice']);
#礼包记录数据
$data_record['user_id'] = $user_id;
$data_record['game_id'] = $game_id;
$data_record['game_name'] = $game_name;
$data_record['gift_id'] = $gift_id;
$data_record['gift_name'] = $novice['giftbag_name'];
$data_record['novice'] = $novice_arr[0];
$this->add_gift_record($data_record);
#领取成功后移除这个激活码
unset($novice_arr[0]);
#将新的激活码转换成字符串 保存
$act['novice']=implode(",", $novice_arr);
$act['novice_num'] = count($novice_arr);
$gift->where("id=".$gift_id)->save($act);
$ddd['novice'] = $data_record['novice'];
echo "领取成功";
}
}
/**
*添加礼包领取记录
*/
private function add_gift_record($data = array()){
$record = M('GiftRecord',"tab_");
5 years ago
$map['user_id'] = $this->userId;
5 years ago
$map['gift_id'] = $data['gift_id'];
$isReceive = $record->where($map)->find();
if(!empty($isReceive)){
$ddd['novice'] = $isReceive['novice'];
echo "您已经领取过该礼包";die();
}
$user_data = get_user_entity($data['user_id']);
$data_record['game_id'] = $data['game_id'];
$data_record['game_name'] = $data['game_name'];
$data_record['server_id'] = 0;
$data_record['server_name'] = "";
$data_record['gift_id'] = $data['gift_id'];
$data_record['gift_name'] = $data['gift_name'];
$data_record['status'] = 0;
$data_record['novice'] = $data['novice'];
$data_record['user_id'] = $data['user_id'];
$data_record['user_account'] = $user_data['account'];
$data_record['user_nickname'] = $user_data['nickname'];
$data_record['create_time'] = NOW_TIME;
5 years ago
// pp($data_record);
5 years ago
return $record->add($data_record);
}
}