Gift.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /** 礼包 -- 控制器
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/10/24
  6. * Time: 10:47
  7. */
  8. namespace app\home\controller;
  9. use think\Db;
  10. class Gift extends Home
  11. {
  12. /**
  13. * 初始化操作
  14. */
  15. protected function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 礼包--热门礼包
  21. */
  22. public function index(){
  23. $this->getList(1);
  24. return $this->fetch();
  25. }
  26. /**
  27. * 礼包--最新礼包
  28. */
  29. public function news(){
  30. $this->getList(0);
  31. return $this->fetch('index');
  32. }
  33. /**
  34. * 礼包--列表
  35. */
  36. public function getList($type) {
  37. /*$type = $this->request->param('t',1,'intval');*/
  38. if (!in_array($type,[0,1])) $type = 1;
  39. $where = [
  40. /*'a.is_top' => $type,*/
  41. 'a.isdelete' => 0,
  42. 'a.endtime' => ['gt',NOW_TIMESTAMP],
  43. 'b.is_show' => 1,
  44. 'b.cooperation_status' => ['in',[0,1,2]]
  45. ];
  46. if ($type){
  47. $orderStr = ' a.used / a.total desc, a.endtime asc';
  48. }else{
  49. $orderStr = 'a.create_time desc';
  50. }
  51. $list = Db::table('cy_libaoinfo')->alias('a')
  52. ->join('cy_game b','a.gameid = b.id')
  53. ->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
  54. ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,info.mobileicon')
  55. ->where($where)
  56. ->where('a.total > a.used')
  57. ->orderRaw($orderStr)
  58. ->paginate(10, false, array('query' => input('get.')))
  59. ->each(function ($item, $key){
  60. $used = $item['used'];
  61. $total = $item['total'];
  62. $item['percent'] = '100%';
  63. if ( $used >= $total ) {
  64. $item['percent'] = '0%';
  65. } else if ( $used && $total ) {
  66. $item['percent'] = (bcsub( 1, bcdiv($used, $total, 2), 2 ) * 100) . '%';
  67. }
  68. $item['mobileicon'] = STATIC_DOMAIN.$item['mobileicon'];
  69. return $item;
  70. });
  71. $adModel = Db::name('cy_ad');
  72. $adInfo = $adModel->where(['type'=>99])->order('sort asc,create_time asc')->find();
  73. $this->assign('adInfo',$adInfo);
  74. $this->assign('list',$list);
  75. $this->assign('type',$type);
  76. $this->assign('page',$list->render());
  77. }
  78. /**
  79. * 礼包详情页
  80. */
  81. public function detail(){
  82. $id = $this->request->param('id',0,'intval');
  83. if (empty($id)) $this->_abort404();
  84. $where = [
  85. 'a.id' => $id ,
  86. 'b.is_show' => 1 ,
  87. 'b.cooperation_status' => ['in' , [0 , 1 , 2]]
  88. ];
  89. $info = Db::table('cy_libaoinfo')->alias('a')
  90. ->join('cy_game b','a.gameid = b.id')
  91. ->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
  92. ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,info.mobileicon,b.nickname')
  93. ->where($where)
  94. ->find();
  95. if (empty($info)) $this->_abort404();
  96. $info['mobileicon'] = STATIC_DOMAIN.$info['mobileicon'];
  97. $info['game_url'] = getGameUrl($info['gameid']);
  98. $info['percent'] = '100%';
  99. if ( $info['used'] >= $info['total'] ) {
  100. $info['percent'] = '0%';
  101. } else if ( $info['used'] && $info['total'] ) {
  102. $info['percent'] = (bcsub( 1, bcdiv($info['used'], $info['total'], 2), 2 ) * 100) . '%';
  103. }
  104. $libaoList = Db::table('cy_libaoinfo')
  105. ->field('id,title')
  106. ->where(['id'=>['neq',$id],'isdelete'=>0,'endtime' => ['gt',NOW_TIMESTAMP],'gameid'=>$info['gameid']])
  107. ->where('total > used')
  108. ->order('create_time desc')
  109. ->limit(10)
  110. ->select();
  111. $downPage = model('Setting')->where("name = 'APP_LINK'")->value('value');
  112. $adModel = Db::name('cy_ad');
  113. $adInfo = $adModel->where(['type'=>99])->order('sort asc,create_time asc')->find();
  114. $this->assign('adInfo',$adInfo);
  115. $this->assign('libaoList',$libaoList);
  116. $this->assign('downPage',$downPage);
  117. $this->assign('info',$info);
  118. return $this->fetch();
  119. }
  120. /**
  121. * 领取礼包
  122. */
  123. public function receiveGift(){
  124. $this->error('请到游戏中领取!');
  125. $id = $this->request->param('id',0,'intval');
  126. if (empty($id)) $this->error('参数错误!');
  127. if (empty(session('?front_userid'))) $this->jsonResult(getEncodeUrl('login'),-3,'请先登录');
  128. $username = session('front_account');
  129. // 1 判断是否领取过
  130. $hasReceive = Db::table('cy_libaolog')->alias('a')
  131. ->field('a.code ,a.id as logid')
  132. ->where(['a.libaoid'=>$id,'a.username'=>$username])
  133. ->find();
  134. if (!empty($hasReceive)) $this->jsonResult($hasReceive['code'],2,'您已领取过该礼包');
  135. $info = Db::table('cy_libaoinfo')->alias('a')
  136. ->join('cy_libao b','a.id = b.infoid','left')
  137. ->field('b.code,a.endtime,a.used,a.total,b.id as lid')
  138. ->where(['a.id'=>$id,'b.status'=>0])->find();
  139. if ($info['used'] >= $info['total'] || empty($info['code'])) $this->jsonResult([],-2,'该礼包已被领取完');
  140. if ($info['endtime'] <= NOW_TIMESTAMP) $this->jsonResult([],-1,'该礼包已过期');
  141. Db::startTrans();
  142. try{
  143. Db::table('cy_libao')->where( array('id'=>$info['lid']) )->update( array('status'=>1,'update_time'=>NOW_TIMESTAMP) );
  144. Db::table('cy_libaoinfo')->where( array('id'=>$id) )->setInc('used');
  145. Db::table('cy_libaolog')->insertGetId( array('username'=>$username,'libaoid'=>$id,'code'=>$info['code'],'create_time'=>NOW_TIMESTAMP) );
  146. // 提交事务
  147. Db::commit();
  148. } catch (\Exception $e) {
  149. // 回滚事务
  150. Db::rollback();
  151. $this->jsonResult([],-2,'该礼包该礼包暂时无法领取或已被领取完'.$e);
  152. }
  153. $this->jsonResult($info['code'],1,'领取成功!');
  154. }
  155. }