GameVip.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use app\common\model\Game;
  5. use app\common\library\FileUpload;
  6. class GameVip extends Admin
  7. {
  8. protected function _initialize()
  9. {
  10. parent::_initialize();
  11. $this->gameVipModel = Db::name('cy_game_vip_price');
  12. }
  13. public function index()
  14. {
  15. $where = [];
  16. //游戏名称
  17. if (input('request.game_id') != '') {
  18. $where['v.gameid'] = ['=', input('request.game_id')];
  19. }
  20. //开始时间和结束时间都有时
  21. if(input('request.start') != '' && input('request.end') != ''){
  22. $where['v.online_time'] = [
  23. ['>=', strtotime(input('request.start'))],
  24. ['<=', strtotime(input('request.end').' 23:59:59')],
  25. ];
  26. }
  27. //开始时间
  28. elseif (input('request.start') != '') {
  29. $where['v.online_time'] = ['>=', strtotime(input('request.start'))];
  30. }
  31. //结束时间
  32. elseif (input('request.end') != '') {
  33. $where['v.online_time'] = ['<=', strtotime(input('request.end').' 23:59:59')];
  34. }
  35. $param = input('get.');
  36. $gameVip = $this->gameVipModel
  37. ->alias('v')
  38. ->join('cy_game g', 'v.gameid=g.id', 'left')
  39. ->field('v.*,g.id as gameid2,g.name as game_name')
  40. ->where($where)
  41. ->order('v.id desc')->paginate(10, false, array('query' => $param));
  42. $gameList = model('Common/Game')->getAllByCondition('id,name',[],'','self');
  43. $this->assign('game_list', $gameList);
  44. $this->assign('game_vip_list', $gameVip);
  45. $this->assign('page', $gameVip->render());
  46. return $this->fetch();
  47. }
  48. public function add()
  49. {
  50. if (request()->isPost()) {
  51. $data = [
  52. 'gameid' => input('post.game_id'),
  53. 'online_time' => input('post.game_online_date'),
  54. ];
  55. $result = $this->validate($data, [
  56. ['gameid', 'require', '游戏名不能为空'],
  57. ['online_time', 'require', '上线时间不能为空'],
  58. ]);
  59. if (true !== $result) {
  60. $this->error($result);
  61. } else {
  62. $where['gameid'] = $data['gameid'];
  63. $where['online_time'] = $where['update_time'] = $where['create_time'] = strtotime($data['online_time']);
  64. $file = request()->file('img');
  65. if (!$file) {
  66. $this->error('价格图片不能为空! ');
  67. }
  68. $fl = new FileUpload();
  69. // 1.校验
  70. $fl->set('allowExt', 'jpg,png')->set('dir','image/vip/pic/');
  71. // 2.上传
  72. $path = $fl->upload($file);
  73. if ($path) {
  74. $where['image'] = $path;
  75. } else {
  76. $this->error('上传失败! ' . $fl->getError());
  77. };
  78. $id = $this->gameVipModel->insertGetId($where);
  79. if (empty($id)) {
  80. $this->error('添加失败!');
  81. }
  82. $this->success('VIP信息添加成功!', 'index');
  83. }
  84. }
  85. // 已经添加过VIP信息的游戏,不会出现在下拉框中。
  86. $vipIds = $this->gameVipModel->distinct(true)->column('gameid');
  87. $gameList = model('Common/Game')->getAllByCondition('id,name',['id' => ['not in', $vipIds]],'name asc','self');
  88. $this->assign('game_list', $gameList);
  89. return $this->fetch();
  90. }
  91. public function edit($id)
  92. {
  93. $id = (int)$id;
  94. if (empty($id)) {
  95. $this->error('游戏ID不能为空');
  96. }
  97. $gameVipInfo = $this->gameVipModel->where(['id' => $id])->find();
  98. if (empty($gameVipInfo)) {
  99. $this->error('vip信息不能为空! ');
  100. }
  101. if (request()->isPost()) {
  102. $data = [
  103. 'online_time' => input('post.game_online_date'),
  104. ];
  105. $result = $this->validate($data, [
  106. ['online_time', 'require', '上线时间不能为空'],
  107. ]);
  108. if (true !== $result) {
  109. $this->error($result);
  110. } else {
  111. $where['online_time'] = $where['update_time'] = strtotime($data['online_time']);
  112. $file = request()->file('img');
  113. if ($file) {
  114. $fl = new FileUpload();
  115. // 1.校验
  116. $fl->set('allowExt', 'jpg,png')->set('dir','image/vip/pic/');
  117. // 2.上传
  118. $path = $fl->upload($file);
  119. if ($path) {
  120. $where['image'] = $path;
  121. } else {
  122. $this->error('上传失败! ' . $fl->getError());
  123. };
  124. }
  125. $this->gameVipModel->where(['id' => $id])->update($where);
  126. $this->success('VIP信息编辑成功!', 'index');
  127. }
  128. }
  129. $gameInfo = Db::name('cy_game')->field('id,name,image')->where(['id' => $gameVipInfo['gameid']])->find();
  130. $this->assign('gamevip_info', $gameVipInfo);
  131. $this->assign('game_info', $gameInfo);
  132. return $this->fetch();
  133. }
  134. /**
  135. * 删除
  136. */
  137. public function delete()
  138. {
  139. $id = input('id', 0, 'intval');
  140. if(empty($id))
  141. $this->error('删除记录的ID不能为空');
  142. $info = $this->gameVipModel->where(['id'=>$id])->find();
  143. if(empty($info)) {
  144. $this->error('记录不存在');
  145. }
  146. if($this->gameVipModel->where(['id'=>$id])->delete()){
  147. $this->success('删除成功');
  148. }else{
  149. $this->error('删除失败');
  150. }
  151. }
  152. }