Search.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // 搜索页
  3. namespace app\mobile\controller;
  4. use think\Db;
  5. use think\Config;
  6. use app\common\model\GameHome as GameModel;
  7. use app\common\model\News as NewsModel;
  8. class Search extends Mobile{
  9. /**
  10. * 初始化操作
  11. */
  12. protected function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. /**
  17. * 搜索
  18. */
  19. public function index(){
  20. // 热搜词
  21. $trendingSearch = Db::name('cy_keywords')->cache(Config::get('QUERY_RESULT_CACHE_TIME'))->where('type',1)->order('create_time desc')->value('title');
  22. // 热门搜索
  23. $data = [
  24. 'a.type' => 2 ,
  25. 'b.is_show' => 1 ,
  26. 'b.cooperation_status' => ['in' , [1 , 2]]
  27. ];
  28. $list = Db::name('cy_keywords')->alias('a')
  29. ->cache(Config::get('QUERY_RESULT_CACHE_TIME'))
  30. ->join('cy_game b','a.title = b.id')
  31. ->field('b.nickname as title,b.id as gameid')
  32. ->where($data)->order('a.create_time desc')
  33. ->limit(10)
  34. ->select();
  35. $this->assign('list',$list);
  36. $this->assign('trendingSearch',$trendingSearch);
  37. return $this->fetch();
  38. }
  39. /**
  40. * 搜索结果
  41. */
  42. public function search(){
  43. $keyword = input('keyword');
  44. $gameList = $this->_getGameList($keyword);
  45. $newsList = $this->_getNewsList($keyword);
  46. $this->assign('list',$gameList['list']);
  47. $this->assign('newsList',$newsList['list']);
  48. $this->assign('page',$gameList['page']);
  49. $this->assign('newsPage',$newsList['page']);
  50. $this->assign('keyword',$keyword);
  51. return $this->fetch();
  52. }
  53. // 游戏列表
  54. public function _getGameList($keyword) {
  55. $list = (new GameModel())->alias('game')->cache(Config::get('QUERY_RESULT_CACHE_TIME'))
  56. ->join('cy_gameinfo info', 'game.id = info.game_id')
  57. ->join('cy_sdkgamelist sdk', 'game.id=sdk.gameid and sdk.package_type=0 and sdk.upload_status=1 and sdk.channel_id='.MEMBER_DEFAULT_CHANNEL_ID,'left')
  58. ->field('LENGTH(game.nickname) as tLen,LOCATE("'.$keyword.'",game.nickname) as tIndex,LOCATE("'.$keyword.'",game.pinyin) as cIndex,game.id,game.nickname,game.power,info.androidurl,game.pinyin,info.mobileicon,game.type,game.subject,sdk.filename,game.publicity,info.platform')
  59. ->where(['game.cooperation_status' => ['in','1,2'],'game.is_show' => 1])
  60. ->where('game.nickname|game.pinyin','like','%'. $keyword .'%')
  61. ->order('tIndex, cIndex,tLen,game.create_time desc')
  62. ->paginate(10, false, array('path'=>'javascript:AjaxGamePage([PAGE]);',))
  63. ->each(function ($item, $key){
  64. $item['icon'] = getGameIcon($item['mobileicon']);
  65. $item['size'] = getSize($item);
  66. if($item['platform']==1){
  67. $item['download'] = '';
  68. $item['ios_download'] = getIosDownload($item);
  69. }
  70. else{
  71. $item['download'] = getDownload($item);
  72. $item['ios_download'] = '';
  73. }
  74. $item['subject'] = Db::connect(config('database_slave'))->cache(Config::get('QUERY_RESULT_CACHE_TIME'))->table('cy_gamesubject')->where('id',$item['subject'])->value('name') ? : '未知';
  75. $item['type'] = Db::connect(config('database_slave'))->cache(Config::get('QUERY_RESULT_CACHE_TIME'))->table('cy_gametype')->where('id',$item['type'])->value('name') ? : '未知';
  76. $item['gameUrl'] = getGameUrl($item['id'],2);
  77. $item['libaonum'] = model('Gift')->where(['isdelete'=>0,'gameid'=>$item['id'],'endtime'=>['gt',NOW_TIMESTAMP]])->where('total > used')->count('id');
  78. return $item;
  79. });
  80. return ['list'=>$list,'page'=>$list->render()];
  81. }
  82. public function _getNewsList($keyword) {
  83. $newsList = (new NewsModel())->cache(Config::get('QUERY_RESULT_CACHE_TIME'))
  84. ->alias('a')
  85. ->join('cy_content b', 'a.id = b.id','left')
  86. ->field('a.id,a.title,a.image,a.jlink,a.create_time,a.total,a.zhiding,a.type,a.gameid,b.content')
  87. ->where(['a.isdelete' => 0,'a.type' => ['in',[1,2,3,4,5]]])
  88. ->where('a.title','like','%'. $keyword .'%')
  89. ->order('a.zhiding desc,a.id desc')
  90. ->paginate(10, false, array('path'=>'javascript:AjaxPage([PAGE]);',))
  91. ->each(function ($item, $key){
  92. $item['image'] = getNewsImage($item['image']);
  93. $item['url'] = getMobileNewsUrl($item['id'],$item['jlink']);
  94. $item['createtime'] = date('Y-m-d H:i:s',$item['create_time']);
  95. $item['cutcontent'] = getCutcontent($item['content']);
  96. return $item;
  97. });
  98. return ['list'=>$newsList,'page'=>$newsList->render(),'keyword'=>$keyword];
  99. }
  100. }