ChannelCps.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: edison
  5. * Date: 2018/3/26
  6. * Time: 上午11:50
  7. */
  8. namespace app\admin\controller;
  9. use app\common\library\FileUpload;
  10. use app\common\model\CpsTemplate;
  11. use app\common\model\CpsChannel;
  12. use app\common\model\Agent;
  13. use app\common\model\CpsChannelGame;
  14. use think\Db;
  15. class ChannelCps extends Admin
  16. {
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->CPSTemplate = $this->cyModel('cps_template');
  21. $this->CPSChannel = $this->cyModel('cps_channel');
  22. }
  23. /**
  24. * 加载 模板列表 页面
  25. */
  26. public function index()
  27. {
  28. $where =[];
  29. $list = $this->CPSTemplate
  30. ->where($where)
  31. ->order("id DESC")
  32. ->paginate(10, false, ['query' => $where]);
  33. // 获取分页显示
  34. $page = $list->render();
  35. $this->assign("page", $page);
  36. $this->assign('list', $list);
  37. return $this->fetch();
  38. }
  39. /**
  40. * 加载模板添加页
  41. */
  42. public function templateAdd(){
  43. if (request()->isPost()) {
  44. $data = [
  45. 'name' => input('post.name'),
  46. 'html_code' => input('post.html_code'),
  47. ];
  48. $result = $this->validate($data, [
  49. ['name', 'require', '模板名不能为空'],
  50. ['html_code', 'require', 'HTML代码不能为空'],
  51. ]);
  52. if (true !== $result) {
  53. $this->error($result);
  54. } else {
  55. $where['name'] = $data['name'];
  56. $where['html_code'] = $data['html_code'];
  57. $where['create_time'] = time();
  58. $file = request()->file('img');
  59. if ($file) {
  60. $fl = new FileUpload();
  61. // 1.校验
  62. $fl->set('allowExt', 'jpg,png');
  63. // 2.上传
  64. $path = $fl->upload($file);
  65. if ($path) {
  66. $where['icon_url'] = $path;
  67. } else {
  68. $this->error('上传失败! ' . $fl->getError());
  69. };
  70. }
  71. $id = $this->CPSTemplate->insertGetId($where);
  72. if (empty($id)) {
  73. $this->error('添加失败!');
  74. }
  75. $this->success('CPS渠道模板添加成功!', 'index');
  76. }
  77. }
  78. return $this->fetch('template_add');
  79. }
  80. /**
  81. * 加载模板编辑页
  82. */
  83. public function templateEdit(){
  84. $id = $this->request->param('id');
  85. if (empty($id)){
  86. $this->error("参数错误");
  87. }
  88. $list = $this->CPSTemplate->where('id',$id)->find();
  89. $this->assign('list', $list);
  90. return $this->fetch('template_edit');
  91. }
  92. /**
  93. * 更新 CPS渠道模板内容
  94. */
  95. public function templateEditPost(){
  96. $data = [
  97. 'id' => input('post.id'),
  98. 'name' => input('post.name'),
  99. 'html_code' => input('post.html_code'),
  100. ];
  101. $result = $this->validate($data, [
  102. ['name', 'require', '模板名不能为空'],
  103. ['html_code', 'require', 'HTML代码不能为空'],
  104. ]);
  105. if (true !== $result) {
  106. $this->error($result);
  107. } else {
  108. $where['name'] = $data['name'];
  109. $where['html_code'] = $data['html_code'];
  110. $where['update_time'] = time();
  111. $file = request()->file('img');
  112. if ($file) {
  113. $fl = new FileUpload();
  114. // 1.校验
  115. $fl->set('allowExt' , 'jpg,png');
  116. // 2.上传
  117. $path = $fl->upload($file);
  118. if ($path) {
  119. $where['icon_url'] = $path;
  120. } else {
  121. $this->error('上传失败! ' . $fl->getError());
  122. };
  123. }
  124. $this->CPSTemplate->where(['id' => $data['id']])->update($where);
  125. $this->success('CPS渠道模板更新成功!' , 'index');
  126. }
  127. }
  128. /**
  129. * 加载 渠道列表 页面
  130. */
  131. public function channelList()
  132. {
  133. $where =[];
  134. $list = $this->CPSChannel
  135. ->alias("a")
  136. ->join("nw_channel b", 'a.channel_id = b.id', 'left')
  137. ->field("a.*, b.name as channel_name")
  138. ->where($where)
  139. ->order("a.id asc")
  140. ->paginate(10, false, ['query' => $where])->each(function($item, $key){
  141. // 分别统计每个渠道加了几款游戏
  142. $item['hot_game'] = model('CpsChannelGame')->where(['channel_id' => $item['channel_id'], 'game_type' => '2'])->count();
  143. $item['new_game'] = model('CpsChannelGame')->where(['channel_id' => $item['channel_id'], 'game_type' => '1'])->count();
  144. return $item;
  145. });
  146. $page = $list->render();
  147. $this->assign("page", $page);
  148. $this->assign('list', $list);
  149. return $this->fetch('channel_list');
  150. }
  151. /**
  152. * 加载 渠道列表添加渠道 页面
  153. */
  154. public function channelAdd(){
  155. $channel_list = model('Department')
  156. ->where(['flag' => 3])
  157. ->field('id,name')
  158. ->select();
  159. $this->assign('channel_list', $channel_list);
  160. return $this->fetch('channel_add');
  161. }
  162. /**
  163. * 添加 渠道
  164. */
  165. public function channelAddPost(){
  166. $channel_id = input('post.channel_id');
  167. if (empty($channel_id)){
  168. $this->error('请选择渠道');
  169. }
  170. $user = new CpsChannel;
  171. $res_is = $user->where('channel_id', $channel_id)->find();
  172. if(!empty($res_is)){
  173. $this->error('渠道已存在,请勿重复添加');
  174. }
  175. $user->channel_id = $channel_id;
  176. $user->create_time = time();
  177. $user->save();
  178. if($user->id) {
  179. $this->success('添加渠道成功', 'channelList');
  180. }
  181. }
  182. /**
  183. * 删除CPS渠道
  184. */
  185. public function channelDelete(){
  186. $id = $this->request->param('id');
  187. empty($id) && $this->error('参数错误');
  188. // 获取该条记录的渠道信息
  189. $channel_id = $this->CPSChannel->where('id',$id)->column('channel_id');
  190. $channel_id = $channel_id[0];
  191. // 判断一下,该渠道是否已经添加了游戏
  192. $count = model('CpsChannelGame')->where(['channel_id' => $channel_id])->count();
  193. if($count) {
  194. $this->error('该渠道已添加游戏,请先删除');
  195. }
  196. $res = $this->CPSChannel->delete($id);
  197. if($res) {
  198. $this->success('删除成功');
  199. }else{
  200. $this->error('删除失败');
  201. }
  202. }
  203. /**
  204. * 渠道游戏表
  205. */
  206. public function channelGameList(){
  207. $channel_id = $this->request->param('channel_id');
  208. empty($channel_id) && $this->error('参数错误');
  209. $model = model('CpsChannelGame');
  210. $map = ['a.channel_id' => $channel_id];
  211. $game_type = $this->request->param('game_type');
  212. !empty($game_type) && $map['a.game_type'] = $game_type;
  213. $list = $model
  214. ->alias("a")
  215. ->join("nw_channel b", 'a.channel_id = b.id', 'left')
  216. ->join("cy_game c", 'a.game_id = c.id', 'left')
  217. ->field("a.*, b.name as channel_name, c.name as ganme_name")
  218. ->where($map)
  219. ->paginate(10, false, ['query' => []]);
  220. $this->assign('list', $list);
  221. $this->assign('channel_id', $channel_id);
  222. $this->assign('game_type', $game_type);
  223. $this->assign('page', $list->render());
  224. return $this->fetch('channel_game_list');
  225. }
  226. /**
  227. * 渠道游戏添加
  228. */
  229. public function channelGameAdd(){
  230. if(request()->isGet()) {
  231. $game_list = model('Game')->field('id,name')->select();
  232. $this->assign('game_list', $game_list);
  233. $channel_id = input('channel_id');
  234. $this->assign('channel_id', $channel_id);
  235. return $this->fetch('channel_game_add');
  236. }
  237. if(request()->isPost()) {
  238. $model = model('CpsChannelGame');
  239. $runnable = true;
  240. $msg = '';
  241. // 需要同步添加分包表记录
  242. $data = [
  243. 'channel_id' => input('post.channel_id') ,
  244. 'game_id' => input('post.game_id') ,
  245. 'game_type' => input('post.game_type') ,
  246. 'sort' => input('post.sort') ,
  247. 'create_time' => time()
  248. ];
  249. // channel_id 和 game_id 是联合主键,不能重复
  250. $uniquename = $model->get(['channel_id'=>$data['channel_id'], 'game_id'=>$data['game_id']]);
  251. if(sizeof($uniquename)){
  252. $runnable = false;
  253. $this->error('该游戏已经在该CPS渠道里了,请重新选择~~~');
  254. }
  255. $model->data($data);
  256. $result = $model->save();
  257. if(empty($result)) {
  258. $runnable = false;
  259. $msg = '添加游戏失败,游戏已存在';
  260. }
  261. if($runnable) {
  262. // 先判断一下cy_agent是否已经存在游戏了
  263. $agent_model = model('Agent');
  264. $has_record = $agent_model
  265. ->where(['gameid' => $data['game_id'], 'departmentid' => $data['channel_id']])
  266. ->count('id');
  267. if(1) { // 不存在记录,创建一条
  268. $agent_data = [];
  269. $agent_data['gameid'] = $data['game_id'];
  270. $agent_data['departmentid'] = $data['channel_id'];
  271. $agent_data['create_time'] = time();
  272. $agent_data['agent'] = date('YmdHis').rand(10, 99);
  273. // channel_id 和 game_id 是联合主键,不能重复
  274. $uniqueKey = $agent_model->get(['departmentid'=>$data['channel_id'], 'gameid'=>$data['game_id']]);
  275. if(sizeof($uniqueKey)){
  276. $runnable = false;
  277. $this->error('创建分包记录失败');
  278. }
  279. $agent_model->data($agent_data);
  280. $agent_result = $agent_model->save($agent_data);
  281. if(empty($agent_result)) {
  282. $runnable = false;
  283. $msg = '创建分包记录失败';
  284. }
  285. }
  286. }
  287. if($runnable) {
  288. $this->success('添加游戏成功', url('channelGameList', ['channel_id' => input('post.channel_id')]));
  289. }else{
  290. $this->error($msg);
  291. }
  292. }
  293. }
  294. /**
  295. * 渠道游戏编辑
  296. */
  297. public function channelGameEdit(){
  298. if(request()->isGet()) {
  299. $id = input('id', 0, 'intval');
  300. empty($id) && $this->error('参数错误');
  301. $data = model('CpsChannelGame')->find($id);
  302. $game_name = model('Game')->getName($data['game_id']);
  303. $this->assign('data', $data);
  304. $this->assign('game_name', $game_name);
  305. /*$channel_id = input('channel_id');
  306. $this->assign('channel_id', $channel_id);*/
  307. return $this->fetch('channel_game_edit');
  308. }
  309. if(request()->isPost()) {
  310. $model = model('CpsChannelGame');
  311. $data = [];
  312. $data['game_type'] = input('post.game_type' , 1 , 'intval');
  313. $data['sort'] = input('post.sort' , 0 , 'intval');
  314. $data['update_time'] = NOW_TIMESTAMP;
  315. $id = input('post.id' , 0 , 'intval');
  316. $res = $model->where('id', $id)->update($data);
  317. if($res === false) {
  318. $this->error('游戏修改失败');
  319. }else{
  320. $this->success('游戏修改成功', url('channelGameList', ['channel_id' => input('post.channel_id')]));
  321. }
  322. }
  323. }
  324. /**
  325. * 渠道游戏记录删除
  326. */
  327. public function channelGameDelete(){
  328. $id = input('id', 0, 'intval');
  329. empty($id) && $this->error('参数错误');
  330. $model = CpsChannelGame::get($id);
  331. $res = $model->delete();
  332. if($res === false) {
  333. $this->error('游戏删除失败');
  334. }else{
  335. $this->success('游戏删除成功');
  336. }
  337. }
  338. /**
  339. * 加载 渠道推广列表 页面
  340. */
  341. public function channelListTg()
  342. {
  343. $where =[];
  344. $list = $this->CPSChannel
  345. ->alias("a")
  346. ->join("nw_channel b", 'a.channel_id = b.id', 'left')
  347. ->field("a.*, b.name as channel_name")
  348. ->where($where)
  349. ->order("a.id asc")
  350. ->paginate(10, false, ['query' => $where])->each(function($item, $key){
  351. // 分别统计每个渠道加了几款游戏
  352. $item['hot_game'] = model('CpsChannelGame')->where(['channel_id' => $item['channel_id'], 'game_type' => '2'])->count();
  353. $item['new_game'] = model('CpsChannelGame')->where(['channel_id' => $item['channel_id'], 'game_type' => '1'])->count();
  354. return $item;
  355. });
  356. $page = $list->render();
  357. $this->assign("page", $page);
  358. $this->assign('list', $list);
  359. return $this->fetch('channel_list_tg');
  360. }
  361. /**
  362. * CPS模板选择页面
  363. */
  364. public function CPSTemplateSelect(){
  365. if(request()->isGet()) {
  366. $id = input('id', 0, 'intval');
  367. empty($id) && $this->error('参数错误');
  368. $info = model('CpsChannel')->find($id);
  369. $template_list = model('CpsTemplate')->select();
  370. $this->assign('info', $info);
  371. $this->assign('template_list', $template_list);
  372. return $this->fetch('CPSTemplateSelect');
  373. }
  374. if(request()->isPost()) {
  375. $id = input('post.id', 0, 'intval');
  376. $template_id = input('post.template_id', 0, 'intval');
  377. // $res = model('CpsChannel')->save(['id' => $id, 'template_id' => $template_id]);
  378. $res = model('CpsChannel')->save([
  379. 'template_id' => $template_id,
  380. 'update_time' => time()
  381. ],['id' => $id]);
  382. if($res === false) {
  383. $this->error('模板选择失败');
  384. }else{
  385. $this->success('模板选择成功', url('channelListTg'));
  386. }
  387. }
  388. }
  389. /**
  390. * CPS游戏列表页面
  391. */
  392. public function CPSGameList(){
  393. $channel_id = input('channel_id', 0, 'intval');
  394. empty($channel_id) && $this->error('参数错误');
  395. $game_type = input('game_type', 0, 'intval');
  396. $map = [];
  397. $map['a.channel_id'] = $channel_id;
  398. !empty($game_type) && $map['a.game_type'] = $game_type;
  399. $model = model('CpsChannelGame');
  400. $game_list = $model
  401. ->alias("a")
  402. ->join("cy_game b", "a.game_id = b.id", 'left')
  403. ->field("a.*, b.name as game_name")
  404. ->where($map)
  405. ->order('a.sort DESC, a.id DESC')
  406. ->paginate(10, false, ['query' => $map]);
  407. $page = $game_list->render();
  408. $this->assign('page', $page);
  409. $this->assign('list', $game_list);
  410. $this->assign('game_type', $game_type);
  411. $this->assign('channel_id', $channel_id);
  412. return $this->fetch('CPSGameList');
  413. }
  414. }