| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace app\api\controller\v2;
- use app\api\controller\Api;
- use think\Cache;
- use think\Db;
- use think\Model;
- class Gift extends Api
- {
- public function giftList()
- {
- $checkResult = $this->validate($this->input,
- [
- ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
- ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
- ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
- ]);
- if (true !== $checkResult) {
- $this->jsonResult('', 0, $checkResult, 'json');
- }
- $gameid = $this->input['gameid'];
- $where = [
- 'a.isdelete' => 0,
- 'a.endtime' => ['egt', NOW_TIMESTAMP - 86400],
- ];
- $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
- ->where(['b.cooperation_status' => ['in', [0, 1, 2]]])
- ->field('info.mobileicon,b.name')->column('info.mobileicon,b.name', 'b.id');
- $orderStr = 'a.create_time desc';
-
- $list = Db::table('cy_libaoinfo')->alias('a')
- ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume')
- ->where($where)
- ->where(function ($query) use ($gameid) {
- if (isset($gameid) && !empty($gameid)) {
- $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $gameid));
- }
- })
- // ->whereIn('gameid',array_keys($gameInfo))
- // ->where('a.total > a.used')
- ->orderRaw($orderStr)
- // ->page($data['page'], $data['pageSize'])
- ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
- ->toArray();
- if (!$list) {
- $this->jsonResult([], 1, '发送成功');
- }
- $ids = array_column($list['data'], 'id');
- $libaolog = model('libao')->whereIn('infoid', $ids)->where('user_id', '=', $this->input['userid'])
- ->column('code', 'infoid');
- foreach ($list['data'] as $k => $v) {
- $used = $v['used'];
- $total = $v['total'];
- $list['data'][$k]['percent'] = '100';
- if ($used >= $total) {
- $list['data'][$k]['percent'] = '0';
- } else if ($used && $total) {
- $list['data'][$k]['percent'] = (bcsub(1, bcdiv($used, $total, 2), 2) * 100);
- }
- $game_ids = explode(',', $v['gameid']);
- $tmp = [];
- foreach ($game_ids as $kk => $vv) {
- $tmp[] = isset($gameInfo[$vv]['name']) ? $gameInfo[$vv]['name'] : '';
- }
- $list['data'][$k]['name'] = implode(' ', $tmp);
- $list['data'][$k]['mobileicon'] = STATIC_DOMAIN . '/' . $gameInfo[$game_ids[0]]['mobileicon'];
- // $list['data'][$k]['name'] = $gameInfo[$v['gameid']]['name'];
- $list['data'][$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
- $list['data'][$k]['endtime'] = date('Y-m-d H:i:s', $v['endtime'] + 86399);
- $list['data'][$k]['status'] = isset($libaolog[$v['id']]) ? '2' : '1'; //1 未领取 2已领取
- if ($v['consume'] > 0) {
- $list['data'][$k]['consumeStr'] = '累计充值达到' . $v['consume'] . '元可领取';
- $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->input['userid'] . ':' . $gameid, 30)
- ->where(['userid' => $this->input['userid'], 'gameid' => $gameid, 'status' => 1])->sum('amount');
- if ($pay >= $v['consume']) {
- $list['data'][$k]['eligibility'] = 1;//领取资格 1有资格 2无资格
- } else {
- $list['data'][$k]['eligibility'] = 2;//领取资格 1有资格 2无资格
- }
- } else {
- $list['data'][$k]['consumeStr'] = '进游领取';
- $list['data'][$k]['eligibility'] = 1;//领取资格 1有资格 2无资格
- }
- }
- $this->jsonResult($list, 1, '发送成功');
- }
- public function giftPack()
- {
- $checkResult = $this->validate($this->input,
- [
- ['id', 'require|integer|egt:1', '礼包id不能为空|礼包id必须是整数礼包id必须大于1'],
- ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
- ]);
- if (true !== $checkResult) {
- $this->jsonResult('', 0, $checkResult, 'json');
- }
- $data['id'] = $this->input['id'];
- $data['gameid'] = $this->input['gameid'];
- // 1 判断是否领取过
- $hasReceive = Db::table('cy_libao')->alias('a')
- ->field('a.code, a.id as logid')
- ->where(['a.infoid' => $data['id'], 'a.user_id' => $this->input['userid']])
- ->find();
- if (!empty($hasReceive)) {
- $this->jsonResult(['code' => $hasReceive['code']], 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, a.title')
- ->where(['a.id' => $data['id'], 'b.status' => 0])->find();
- if ($info['used'] >= $info['total'] || empty($info['code'])) {
- $this->jsonResult([], 0, '该礼包已被领取完');
- }
- if ($info['endtime'] <= NOW_TIMESTAMP - 86400) {
- $this->jsonResult([], 0, '该礼包已过期');
- }
- if ($info['consume'] > 0) {
- $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->input['userid'] . ':' . $data['gameid'], 30)
- ->where(['userid' => $this->input['userid'], 'gameid' => $data['gameid'], 'status' => 1])->sum('amount');
- if ($pay < $info['consume']) {
- $this->jsonResult([], 0, '暂无资格领取');
- }
- }
-
- Db::startTrans();
- try {
- $logData = [
- 'username' => $this->input['username'],
- 'title' => $info['title'],
- 'create_time' => NOW_TIMESTAMP,
- 'libaoid' => $data['id'],
- 'code' => $info['code'],
- ];
- Db::table('cy_libao')->where(array('id' => $info['lid']))->update(array('status' => 1, 'update_time' => NOW_TIMESTAMP, 'user_id' => $this->input['userid']));
- Db::table('cy_libaoinfo')->where(array('id' => $data['id']))->setInc('used');
- Db::table('cy_libaolog')->insert($logData);
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->jsonResult([], 0, '该礼包该礼包暂时无法领取或已被领取完 ' . $e);
- }
- $this->jsonResult(['code' => $info['code']], 1, '领取成功!');
- }
- public function getMyGiftList()
- {
- $checkResult = $this->validate($this->input,
- [
- ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
- ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
- ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
- ['type', 'require|integer|in:1,2', '类型不能为空|类型必须是整数|类型必须大于1'],
- ]);
- if (true !== $checkResult) {
- $this->jsonResult('', 0, $checkResult, 'json');
- }
- $data['page'] = $this->input['page'];
- $data['gameid'] = $this->input['gameid'];
- $where = [
- 'll.user_id' => $this->input['userid'],
- // 'a.gameid' => $data['gameid'],
- 'a.isdelete' => 0,
- ];
- $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
- ->where(['b.cooperation_status' => ['in', [0, 1, 2]]])
- ->field('info.mobileicon,b.name')->column('info.mobileicon,b.name', 'b.id');
- if ($this->input['type'] == 1) { //1 未过期 2已过期
- $where['a.endtime'] = ['egt', NOW_TIMESTAMP - 86400];
- } else {
- $where['a.endtime'] = ['lt', NOW_TIMESTAMP - 86400];
- }
-
- $orderStr = 'a.create_time desc';
- $list = model('libao')->alias('ll')
- ->join('cy_libaoinfo a', 'll.infoid=a.id and ll.user_id="' . $this->input['userid'] . '"')
- ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume,ll.code,ll.update_time')
- ->where($where)
- ->orderRaw($orderStr)
- ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
- ->toArray();
- if (!$list) {
- $this->jsonResult([], 1, '发送成功');
- }
- foreach ($list['data'] as $k => $v) {
- $game_ids = explode(',', $v['gameid']);
- $tmp = [];
- foreach ($game_ids as $kk => $vv) {
- $tmp[] = isset($gameInfo[$vv]['name']) ? $gameInfo[$vv]['name'] : '';
- }
- $list['data'][$k]['name'] = implode(' ', $tmp);
- $list['data'][$k]['mobileicon'] = STATIC_DOMAIN . '/' . $gameInfo[$game_ids[0]]['mobileicon'];
- $list['data'][$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
- $list['data'][$k]['endtime'] = date('Y-m-d H:i:s', $v['endtime'] + 86399);
- $list['data'][$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
- }
- $this->jsonResult($list, 1, '发送成功');
- }
- }
|