CollectController.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 样式管理
  5. * @author brent
  6. * @version 0.0.1
  7. */
  8. class CollectController extends CommonController {
  9. protected $table = ''; //表名
  10. /**
  11. * [_initialize 前置操作-继承公共前置方法]
  12. * @author Devil
  13. * @blog http://gong.gg/
  14. * @version 0.0.1
  15. * @datetime 2016-12-03T12:39:08+0800
  16. */
  17. public function _initialize() {
  18. // 调用父类前置方法
  19. parent::_initialize();
  20. // 登录校验
  21. $this->Is_Login();
  22. // 权限校验
  23. $this->Is_Power();
  24. //要执行的表
  25. $this->table = M('activity_collect');
  26. }
  27. /**
  28. * [Index 文章列表]
  29. * @author Devil
  30. * @blog http://gong.gg/
  31. * @version 0.0.1
  32. * @datetime 2016-12-06T21:31:53+0800
  33. */
  34. public function Index() {
  35. //关键字
  36. $keyword = I('keyword');
  37. if($keyword){
  38. $where['uid'] = ['like',"%$keyword%"];
  39. }
  40. $listRows = I('listRows',100,'intval');
  41. if($p = I('p',1,'intval')){
  42. $offset = ($p-1)*$listRows;
  43. }
  44. $acts = M('activity_v2')->field('id,activity_name')->select();
  45. $this->assign('acts', $acts);
  46. //活动id
  47. $act_id = trim(I('act_id'));
  48. if($act_id){
  49. $where['act_id'] = $act_id;
  50. }else{
  51. $act_id = M('activity_v2')->max('id');
  52. }
  53. $List = $this->table
  54. ->where($where)
  55. ->order('id desc')
  56. ->limit($offset,$listRows)
  57. ->select();
  58. $activitys = M('activity_v2')->where(['id'=>$act_id])->find();
  59. if($activitys['collect_list']){
  60. $collects = json_decode($activitys['collect_list'],true);
  61. $collects = array_column($collects, null,'collect_id');
  62. }
  63. foreach ($List as $key=>$row) {
  64. $List[$key]['activity_name'] = $activitys['activity_name'];
  65. if($collects){
  66. $List[$key]['collect_name'] = $collects[$row['collect_id']]['collect_name'];
  67. }
  68. }
  69. $count = $this->table->where($where)->count();
  70. $Page = new \Think\Page($count, $listRows);
  71. $Page->parameter .= "&p=[PAGE]";
  72. $Page->parameter .= "&act_id=".$act_id;
  73. $Page->parameter .= "&keyword=".$keyword;
  74. $Page = $this->page_config($Page);
  75. $show = $Page->show();
  76. $this->assign('page', $show);
  77. $this->assign('List', $List);
  78. //获取所有活动
  79. $this->assign('keyword', $keyword);
  80. $this->assign('act_id', $act_id);
  81. $this->display('Index');
  82. }
  83. /**
  84. * [SaveInfo 文章添加/编辑页面]
  85. * @author Devil
  86. * @blog http://gong.gg/
  87. * @version 0.0.1
  88. * @datetime 2016-12-14T21:37:02+0800
  89. */
  90. public function SaveInfo() {
  91. // 文章信息
  92. if (empty($_REQUEST['id'])) {
  93. $data = array();
  94. } else {
  95. $data = $this->table->find(I('id'));
  96. if (empty($data)) {
  97. $data = array('id' => I('id'));
  98. }
  99. }
  100. $data = M('Activity_v2')->field('id,activity_name')->select();
  101. $this->assign('Acts', $data);
  102. $this->assign('data', $data);
  103. $this->display('SaveInfo');
  104. }
  105. /**
  106. * [Save 文章添加/编辑]
  107. * @author Devil
  108. * @blog http://gong.gg/
  109. * @version 0.0.1
  110. * @datetime 2016-12-14T21:37:02+0800
  111. */
  112. public function Save() {
  113. // 是否ajax请求
  114. if (!IS_AjAX) {
  115. $this->error(L('common_unauthorized_access'));
  116. }
  117. // 添加
  118. if (empty($_POST['id'])) {
  119. $this->Add();
  120. // 编辑
  121. } else {
  122. $this->Edit();
  123. }
  124. }
  125. /**
  126. * [Add 文章添加]
  127. * @author Devil
  128. * @blog http://gong.gg/
  129. * @version 0.0.1
  130. * @datetime 2016-12-18T16:20:59+0800
  131. */
  132. private function Add() {
  133. $m = $this->table;
  134. $data = I('post.');
  135. $data['created_at'] = $data['collect_date'] ? : date('Y-m-d H:i:s');
  136. $data['operate'] = 1;
  137. // 数据添加
  138. if ($m->add($data)) {
  139. $this->ajaxReturn('更新成功');
  140. } else {
  141. $this->ajaxReturn('更新失败',400);
  142. }
  143. }
  144. /**
  145. * [Delete 删除]
  146. * @author Devil
  147. * @blog http://gong.gg/
  148. * @version 0.0.1
  149. * @datetime 2016-12-15T11:03:30+0800
  150. */
  151. public function Delete() {
  152. // 是否ajax请求
  153. if (!IS_AJAX) {
  154. $this->error(L('common_unauthorized_access'));
  155. }
  156. // 删除数据
  157. if (!empty($_POST['id'])) {
  158. // 更新
  159. if ($this->table->delete(I('id'))) {
  160. $this->ajaxReturn(L('common_operation_delete_success'));
  161. } else {
  162. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  163. }
  164. } else {
  165. $this->ajaxReturn(L('common_param_error'), -1);
  166. }
  167. }
  168. public function Search()
  169. {
  170. $act_id = I('act_id');
  171. if(empty($act_id)){
  172. echo json_encode(array());
  173. die;
  174. }
  175. $data['activity'] = M('Activity_v2')->where(['id'=>$act_id])->find();
  176. $data['activity']['start_at'] = $data['activity']['start_at'];
  177. $data['activity']['end_at'] = $data['activity']['end_at'];
  178. // dump(json_decode($data['activity']['prize_list'],true));
  179. $data['collects'] = json_decode($data['activity']['collect_list'],true);
  180. echo json_encode($data);
  181. }
  182. }