| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- /**
- * api获取礼包控制器
- */
- namespace app\api\controller\v1;
- use app\common\model\Gift as GiftModel;
- use think\Db;
- class Gift extends UserCenter
- {
- const SIZE = 50;
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 游戏礼包列表
- */
- public function index()
- {
- $page = input('page', 0, 'intval'); //页码
- if ($this->request->isAjax()) {
- if ($page > 1) {
- $gift_list = $this->getGiftData($this->game_id);
- $gift_list = $gift_list->toArray()['data'];
- if (!empty($gift_list)) {
- return $this->json($gift_list, 1);
- }
- return $this->json([], 0, '没有了!');
- }
- return $this->json([], 0, '没有了!');
- }
- $giftList = $this->getGiftData($this->game_id);
- $giftBox = $this->getBoxData($this->userInfo['id'], $this->game_id);
- $this->assign('username', $this->username);
- $this->assign('gameid', $this->game_id);
- $this->assign('gift_list', $giftList); //礼包列表
- $this->assign('gift_box', $giftBox); //存号箱
- return $this->fetch('list');
- }
- /*
- * 获取礼包数据
- */
- public function getGiftData($game_id)
- {
- $nowtime = NOW_TIMESTAMP;
- //$yearAgo = strtotime("-1 year");
- $GiftModel = new GiftModel();
- $giftList = $GiftModel->alias('gift')
- ->field('gift.id,gift.title,gift.consume,gift.content,gift.total,gift.used,gift.endtime,info.mobileicon')
- ->join('cy_gameinfo info', 'info.game_id = gift.gameid','left')
- ->where(['gift.isdelete'=>0])
- ->where(function ($query) use ($game_id) {
- if (isset($game_id) && !empty($game_id)) {
- $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $game_id));
- }
- })
- ->where('gift.starttime', '<=', $nowtime)
- ->where('gift.endtime', '>=', $nowtime)
- ->order('gift.create_time desc')
- ->paginate(self::SIZE);
- if (!empty($giftList)) { // 标识玩家已领取的礼包
- foreach ($giftList as &$gift) {
- // 剩余可领的数量占比
- $gift['percent'] = round(($gift['total'] - $gift['used']) / $gift['total'], 2) * 100;
- //$gift['image'] = STATIC_DOMAIN.$gift['image'];
- }
- }
- return $giftList;
- }
- /**
- * 获取存号箱数据
- */
- public function getBoxData($userid, $game_id)
- {
- // $list = Db::table('cy_libaolog log,cy_libaoinfo info,cy_game game')
- // ->join('cy_gameinfo gameinfo', 'gameinfo.game_id = game.id','left')
- // ->field('log.code,info.title,info.endtime,game.`name`,gameinfo.mobileicon as image')
- // ->where('log.`libaoid`=info.`id` and info.`gameid`=game.`id`')
- // ->where(['log.user_id' => $userid, 'info.gameid' => $game_id])
- // ->order('info.create_time desc')
- // ->paginate(self::SIZE)
- // ->each(function($item, $key){
- // $item['endtime'] = date('Y-m-d', $item['endtime']);
- // return $item;
- // });
- $where = [
- 'll.user_id' => $userid,
- 'a.isdelete' => 0,
- ];
- $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
- ->where('b.id',$game_id)
- ->field('info.mobileicon,b.name')->find();
- $list = model('libao')->alias('ll')
- ->join('cy_libaoinfo a', 'll.infoid=a.id and ll.user_id="' . $userid . '"')
- ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume,ll.code')
- ->where($where)
- ->where(function ($query) use ($game_id) {
- if (isset($game_id) && !empty($game_id)) {
- $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $game_id));
- }
- })
- ->orderRaw('a.create_time desc')
- ->paginate(self::SIZE)
- ->each(function($item, $key) use($gameInfo){
- $item['endtime'] = date('Y-m-d', $item['endtime']);
- $item['name'] = $gameInfo['name'];
- $item['image'] = $gameInfo['mobileicon'];
- return $item;
- });
- // Db 查询分页,foreach修改无效 使用each
- return $list;
- }
- public function getBox()
- {
- if ($this->request->isAjax()) {
- $page = input('page', 0, 'intval');
- if ($page > 1) {
- $gift_box = $this->getBoxData($this->username, $this->game_id);
- $gift_box = $gift_box->toArray()['data'];
- if (!empty($gift_box)) {
- return $this->json($gift_box, 1);
- }
- return $this->json([], 0, '没有了!');
- }
- return $this->json([], 0, '没有了!');
- }
- }
- /**
- * 领取礼包
- */
- public function getGift()
- {
- $username = $this->username; //用户名
- $gift_id = input('giftid'); //礼包ID
- if (empty($gift_id)) return $this->json('', 0, '礼包ID不能为空');
- if (empty($username)) return $this->json('', 0, '用户名不能为空');
- // 1 判断是否领取过
- $hasReceive = Db::table('cy_libao')->alias('a')
- ->field('a.code ,a.id as logid')
- ->where(['a.infoid' => $gift_id, 'a.user_id' => $this->userInfo['id']])
- ->find();
- if (!empty($hasReceive)) {
- return $this->json('', 0, '您已领取过该礼包');
- }
- $info = Db::table('cy_libaoinfo')->alias('a')
- ->join('cy_libao b', 'a.id = b.infoid', 'left')
- ->field('b.code,a.endtime,a.used,a.total,b.id as lid,a.consume')
- ->where(['a.id' => $gift_id, 'b.status' => 0])->find();
- if ($info['used'] >= $info['total'] || empty($info['code'])) {
- return $this->json('', 0, '该礼包已被领取完');
- }
- if ($info['endtime'] <= NOW_TIMESTAMP - 86400) {
- return $this->json('', 0, '该礼包已过期');
- }
- if ($info['consume'] > 0) {
- $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->userInfo['id'] . ':' . $this->game_id, 30)
- ->where(['userid' => $this->userInfo['id'], 'gameid' => $this->game_id, 'status' => 1])->sum('amount');
- if ($pay < $info['consume']) {
- return $this->json('', 0, '暂无资格领取');
- }
- }
- Db::startTrans();
- try {
- Db::table('cy_libao')->where(array('id' => $info['lid']))->update(array('status' => 1, 'update_time' => NOW_TIMESTAMP, 'user_id' => $this->userInfo['id']));
- Db::table('cy_libaoinfo')->where(array('id' =>$gift_id))->setInc('used');
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return $this->json('', 0, '该礼包该礼包暂时无法领取或已被领取完'. $e);
- }
- return $this->json('', 1, '领取成功,请返回存号箱查看。');
- // if (!$gift_info = Db::table('cy_libaoinfo')->where(['id' => $gift_id])->find()) {
- // return $this->json('', 0, '礼包不存在');
- // }
- //
- // $nowtime = time();
- //
- // //时间判断
- // if ($gift_info['starttime'] > $nowtime) {
- // return $this->json('', 0, '领取条件未满足');
- // }
- // if ($gift_info['endtime'] < $nowtime) {
- // return $this->json('', 0, '领取条件未满足');
- // }
- //
- // //是否领取判断
- // $res = Db::table('cy_libaolog')->where(['username' => $username, 'libaoid' => $gift_id])->find();
- // if ($res) {
- // return $this->json('', 0, '已领取过礼包');
- // }
- // // 领取金额限制: 累计成功消费金额
- // $userPay = Db::table('cy_pay')->field(['sum(amount)' => 'amount'])->where(['username' => $username])
- // ->where('status', '=', 1)->find();
- // $user_consume = ! empty($userPay) ? $userPay['amount'] : 0;
- //
- // //判断消费
- // if ($gift_info['consume'] != 0 && $user_consume < $gift_info['consume']) {
- // return $this->json('', 0, '领取条件未满足');
- // }
- // //指定时间内,禁止重复领取
- // if(!requestDuplicateCheck('gift_duplicate_'.$username.'_'.$gift_id,10)){
- //
- // return $this->json('', 0, '相同的礼包领取请求过多,请于10s以后,再次对该礼包的进行领取操作');
- // }
- //
- // // todo 并发情况下 重复领取情况 是否加上lock
- // $info = Db::table('cy_libaoinfo info,cy_libao libao')->field('libao.id,libao.code')
- // ->where('libao.`infoid`=info.id')->where([
- // 'info.id' => $gift_id,
- // 'libao.status' => 0,
- // ])->where(['info.isdelete'=>0])->order('info.starttime ASC')->find();
- //
- // if ($info) {
- // // 启动事务
- // Db::startTrans();
- //
- // try {
- // $code = $info['code']; // 激活码
- // // 更新激活码状态
- // Db::table('cy_libao')->where(['infoid' => $gift_id, 'id' => $info['id']])->update([
- // 'status' => 1,
- // 'update_time' => $nowtime,
- // ]);
- // // 更新礼包表领取个数
- // Db::table('cy_libaoinfo')->where(['id' => $gift_id])->update(['used' => Db::raw('used+1')]);
- // // 插入领取记录
- // Db::table('cy_libaolog')->insert([
- // 'username' => $username,
- // 'libaoid' => $gift_id,
- // 'code' => $code,
- // 'create_time' => $nowtime,
- // ]);
- // // 提交事务
- // Db::commit();
- //
- // } catch (\Exception $e) {
- // // 回滚事务
- // Db::rollback();
- //
- // return $this->json('', 0, '领取失败');
- // }
- //
- // return $this->json('', 1, '领取成功');
- // } else {
- // return $this->json('', 0, '领取失败');
- // }
- }
- /**
- * 礼包详情页面
- */
- public function giftDetail()
- {
- //session('gift_list', true);
- //$game_id = input('game_id'); //游戏ID
- $username = $this->username; //用户名
- $gift_id = input('gift_id');
- //参数校验
- if (empty($username) || empty($gift_id)) {
- $this->jsonResult('', 0, '礼包参数不能为空');
- }
- $giftInfo = Db::table('cy_libaoinfo')
- ->alias('gift')
- ->join('cy_gameinfo gameinfo', 'gameinfo.game_id = gift.gameid','left')
- ->field('gift.id,gift.title,gift.consume,gift.content,gift.total,gift.used,gift.starttime,gift.endtime,
- gift.gameid,gift.description,gameinfo.mobileicon')
- ->where(['gift.id' => $gift_id])
- ->find();
- //判断用户是否已经领取了该利好
- //已领取礼包记录
- $isReceived = Db::table('cy_libao')->field('code')->where([
- 'infoid' => $gift_id,
- 'user_id' => $this->userInfo['id'],
- ])->find();
- $giftInfo['percent']= round(($giftInfo['total'] - $giftInfo['used']) / $giftInfo['total'], 2) * 100;
- $giftInfo['mobileicon'] = $giftInfo['mobileicon'];
- if (!empty($isReceived)) {
- $giftInfo['received'] = true;
- } else {
- $giftInfo['received'] = false;
- }
- $this->assign('username', $username);
- $this->assign('gift_id', $gift_id);
- $this->assign('gameid', $giftInfo['gameid']);
- $this->assign('gift_info', $giftInfo);
- return $this->fetch('detail');
- }
- }
|