Gift.php 6.4 KB

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