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.
jy-sdk/Application/Sdk/Controller/GameGiftPageController.clas...

227 lines
7.5 KiB
PHTML

2 years ago
<?php
namespace Sdk\Controller;
use Think\Controller;
use Common\Api\GaemApi;
class GameGiftPageController extends Controller{
public $userId;
public $userToken;
public function _initialize()
{
// if (I("request.api_ver") == 2) {
$user = BaseController::webH5Verify();
$this->userId = $user['userId'];
$this->userToken = $user['userToken'];
// }
}
public function gift_list(){
$gameId = I('game_id');
$sdkVersion = I('sdk_version', -1);
if (!$gameId || $sdkVersion == -1) {
$this->error("参数有误");
}
/* if (I("debug") == 1) {
$data['user_token'] = 'b9c9739f7e3dc0b338dc3f4a4f02cb29';
} */
$gift = M("Giftbag","tab_");
$map['game_id'] = $gameId;
$map['status']=1;
$map['start_time'] = ['lt',time()];
switch ($sdkVersion) {
case 1:
case 2:
$map['giftbag_version'] = ['in', [$sdkVersion, 4]];
break;
}
$list = $gift
->field("id,giftbag_name,novice,start_time,end_time,desribe,game_name")
->where($map)
->select();
//遍历数据获取游戏图片地址
foreach ($list as $key => $val) {
$res = M('gift_record','tab_')->field('id')->where(['gift_id' => $val['id'],'user_id' => $this->userId])->find();
if($res){
$list[$key]['received']=1;
}else{
$list[$key]['received']=0;
}
if($val['end_time'] != 0 && NOW_TIME > $val['end_time']) {
unset($list[$key]);
continue;
}
unset($list[$key]['novice']);
$list[$key]['icon'] = new_icon_url($val['icon']);
$list[$key]['now_time'] = NOW_TIME;
}
$game = M('game','tab_')->where(['id' => $gameId])->field('icon,game_name,icon')->find();
$icon = get_cover($game['icon'],"path");
$game['icon'] = (substr($icon, 0, 7) == "http://" || substr($icon, 0, 8) == "https://" ) ? $icon : C('ADMIN_DOMAIN') . $icon;
$game['gift_num'] = count($list);
$this->assign('user_token', I('user_token'));
$this->assign('game_id',$gameId);
$this->assign('game',$game);
$this->assign('data',$list);
$this->display('lists');
}
public function details($gift_id = '',$user_token = '')
{
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();
$novice = M('gift_record','tab_')->where(['gift_id' => $gift_id,'user_id' => $this->userId])->getField('novice');
$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['end_time']);
if($content['novice']!='')
{
$novice_num = count(explode(',', $content['novice']));
$content['novice_num'] = $novice_num;//是否有剩余
}else{
$content['novice_num'] = 0;//是否剩余
}
$this->assign('user_token',$user_token);
$this->assign('content',$content);
$this->assign('novice',$novice);
$this->display('details');
}
/**
* 礼包领取
* @param number $gift_id
* @param number $user_token
* @param number $game_id
* @param string $game_name
*/
public function receive($gift_id = 0,$user_token = 0,$game_id = 0,$game_name = "")
{
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['end_time']);
if($content['novice']!='')
{
$novice = count(explode(',', $content['novice']));
$content['novice_num'] = $novice;//是否有剩余
}else{
$content['novice_num'] = 0;//是否剩余
}
$res = M('gift_record','tab_')->field('id')->where(['gift_id' => $gift_id,'user_id' => $this->userId])->find();
if($res){
$content['received']=1;
}else{
$content['received']=0;
}
$this->assign('user_token',$user_token);
$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');
}
/**
*领取礼包
*/
public function receive_gift($user_token = 0,$gift_id = 0,$game_id = 0,$game_name = ""){
#获取SDK上POST方式传过来的数据 然后base64解密 然后将json字符串转化成数组
$post = I('request.');
$user_id = $this->userId;
$gift_id = $post['gift_id'];
$game_id = $post['game_id'];
$game_name = $post['game_name'];
$gift = M('giftbag','tab_');
$user_token = $this->userToken;
$novice = $gift->where(["id" => $gift_id])->field("id,giftbag_name,novice")->find();
if(empty($novice['novice'])){
echo json_encode([
'code' => 0,
'status' => 1,
'msg' => '礼包已被领完'
]);
exit();
}
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 json_encode([
'code' => 1,
'status' => 1,
'msg' => '领取成功'
]);
exit();
}
}
/**
*添加礼包领取记录
*/
private function add_gift_record($data = array()){
$record = M('GiftRecord',"tab_");
$map['user_id'] = $this->userId;
$map['gift_id'] = $data['gift_id'];
$isReceive = $record->where($map)->find();
if(!empty($isReceive)){
$ddd['novice'] = $isReceive['novice'];
echo json_encode([
'code' => 0,
'status' => 1,
'msg' => '您已领取过该礼包'
]);
exit();
}
$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;
// pp($data_record);
return $record->add($data_record);
}
}