ChoiceController.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Admin\Controller;
  3. use Common\Controller\AdminController;
  4. class ChoiceController extends AdminController {
  5. public function __construct(){
  6. parent::__construct();
  7. }
  8. //精选列表
  9. public function index(){
  10. $sort = I('sort','1');
  11. $choiceStatus = I('choice_status','');
  12. $choice_type = I('choice_type','');
  13. if($choice_type){
  14. $_SESSION['choice_type']=$choice_type;
  15. }elseif($_SESSION['choice_type']){
  16. $choice_type=$_SESSION['choice_type'];
  17. }else{
  18. $choice_type=1;
  19. }
  20. $keyword = I('get.keyword','');
  21. $condition = array('t_choice.is_delete'=>1,'t_choice.choice_type'=>$choice_type);
  22. $orderList = array(
  23. 1=>array('t_choice.operate_dt desc','创建时间从近到远',1),
  24. 2=>array('t_choice.operate_dt asc','创建时间从远到近',2),
  25. 3=>array('reply desc','评论数从高到低',3),
  26. 4=>array('reply asc','评论数从低到高',4),
  27. 5=>array('praise desc','点赞数从高到低',5),
  28. 6=>array('praise asc','点赞数从低到高',6),
  29. );
  30. $statusList = array('全部状态',1=>'未上架',3=>'已上架');
  31. $orderby = $orderList[$sort][0];
  32. if (!empty($keyword)){
  33. $condition['t_choice.choice_name'] = array('LIKE','%'.$keyword.'%');
  34. }
  35. if (!empty($choiceStatus)){
  36. $condition['t_choice.choice_status'] = $choiceStatus;
  37. }
  38. $this->assign('sort',$sort);
  39. $this->assign('orderby',$orderby);
  40. $this->assign('keyword',$keyword);
  41. $this->assign('orderList',$orderList);
  42. $this->assign('statusList',$statusList);
  43. $this->assign('choice_source',$choice_source);
  44. $this->assign('choiceStatus',$choiceStatus);
  45. $count = M('choice')->where($condition)->count();
  46. //分页
  47. $Page = new \Think\Page($count);
  48. $show = $Page->show();
  49. $Model = M('choice');
  50. $choiceList = $Model->join('left join t_user on t_user.user_id = t_choice.user_id')->join('left join (select choice_id,count(uc_id) as collect from t_user_choices group by choice_id) as a on a.choice_id = t_choice.choice_id')->join('left join (select choice_id,count(cr_id) as reply from t_choice_reply group by choice_id) as b on b.choice_id = t_choice.choice_id')->join('left join (select choice_id,count(cp_id) as praise from t_choice_praise group by choice_id) as c on c.choice_id = t_choice.choice_id')->where($condition)->order($orderby)->group('t_choice.choice_id')->limit($Page->firstRow.','.$Page->listRows)->field('t_choice.*,t_user.user_name,ifnull(a.collect,0) as collect,ifnull(b.reply,0) as reply,ifnull(c.praise,0) as praise')->select();
  51. foreach($choiceList as $k=>$v){
  52. $tmp=explode(',',$v['choice_img']);
  53. $choiceList[$k]['choice_img']=$tmp[0];
  54. }
  55. $this->assign('page',$show);
  56. $this->assign('list',$choiceList);
  57. $this->display();
  58. }
  59. //修改精选状态
  60. public function status(){
  61. $choiceId = I('choice_id','');
  62. $choiceStatus = I('choice_status','');
  63. if (empty($choiceId) || $choiceId<1000000){
  64. $this->error("精选ID错误",'/admin/choice/index');
  65. }
  66. M('choice')->where(array('choice_id'=>$choiceId))->save(array('choice_status'=>$choiceStatus));
  67. $this->success('精选状态修改成功','/admin/choice/index');
  68. }
  69. //修改精选类型
  70. public function type(){
  71. $choiceId = I('choice_id','');
  72. $choiceType = I('choice_type','');
  73. if (empty($choiceId) || $choiceId<1000000){
  74. $this->error("精选ID错误",'/admin/choice/index');
  75. }
  76. if($choiceType==2){
  77. $info = M('choice')->where(array('choice_id'=>$choiceId))->find();
  78. $choice_name=$info['choice_name'];
  79. $uid=$info['user_id'];
  80. $noticeContent = '被评为精选';
  81. $temp=array('choice_id'=>$choiceId,'choice_name'=>$choice_name,'choice_type'=>2);
  82. M('Notice')->add(array('ref_user_id'=>$uid,'user_id'=>0,'notice_flag'=>0,'notice_status'=>1,'notice_type'=>0,'notice_title'=>'被评为精选','notice_content'=>$noticeContent,'operate_dt'=>time(),'extra'=>json_encode($temp)));
  83. M('User')->where(array('user_id'=>$uid))->setInc('sys_notice');
  84. $data = array('type'=>0,'msg'=>$noticeContent,'data'=>array('title'=>$choice_name,'choice_id'=>$choiceId));
  85. $url="http://www.hanlinyuanonline.com/api/choice/push?uid=".$uid."&data=".urlencode(base64_encode(json_encode($data)));
  86. $ch = curl_init();
  87. curl_setopt($ch, CURLOPT_URL, $url);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89. $output = curl_exec($ch);
  90. curl_close($ch);
  91. }
  92. M('choice')->where(array('choice_id'=>$choiceId))->save(array('choice_type'=>$choiceType));
  93. $this->success('精选状态修改成功','/admin/choice/index?choice_type='.$choiceType);
  94. }
  95. //删除精选
  96. public function del(){
  97. $choiceId = I('choice_id','');
  98. if (empty($choiceId) || $choiceId<1000000){
  99. $this->error("精选ID错误",'/admin/choice/index');
  100. }
  101. M('choice')->where(array('choice_id'=>$choiceId))->delete();
  102. $this->success('精选删除成功','/admin/choice/index');
  103. }
  104. //编辑精选
  105. public function edit(){
  106. $step = I('step','');
  107. $choiceId = I('choice_id','');
  108. if (empty($step)){
  109. $info = array();
  110. if (!empty($choiceId)){
  111. $info = M('choice')->where(array('choice_id'=>$choiceId))->find();
  112. }
  113. $this->assign('info',$info);
  114. $this->display();
  115. }else if($step==2){
  116. $choiceName = I('choice_name','');
  117. $choice_img = I('choice_img','');
  118. $choice_content = I('choice_content','');
  119. if (mb_strlen($choiceName,'UTF8')<2 || mb_strlen($choiceName,'UTF8')>30){
  120. $this->error('精选名称2到30个汉字');
  121. }
  122. if ( mb_strlen(html_entity_decode(strip_tags($choice_content)),'UTF8')>100020){
  123. $this->error('精选内容限制10000个汉字');
  124. }
  125. if ( !$choice_img){
  126. $this->error('请上传精选封面');
  127. }
  128. $res = M('choice')->where(array('choice_name'=>$choiceName,'is_delete'=>1))->field('choice_id')->find();
  129. //新增
  130. if (!$choiceId && $res){
  131. $this->error('精选名称重复');
  132. }
  133. //更新
  134. if ($choiceId && $res && $choiceId!=$res['choice_id']){
  135. $this->error('精选名称重复');
  136. }
  137. if (empty($choiceId)){
  138. M('choice')->add(array('choice_type'=>2,'choice_img'=>$choice_img,'choice_content'=>$choice_content,'choice_name'=>$choiceName,'operate_dt'=>time()));
  139. }else{
  140. M('choice')->where(array('choice_id'=>$choiceId))->save(array('choice_img'=>$choice_img,'choice_content'=>$choice_content,'choice_name'=>$choiceName,'operate_dt'=>time()));
  141. }
  142. $this->success('精选操作成功','/admin/choice/index');
  143. }
  144. }
  145. //获取精选的详情
  146. public function detail(){
  147. $choiceId = I('choice_id','');
  148. if (empty($choiceId) || $choiceId<1000000){
  149. $this->error("精选ID错误",'/admin/choice/index');
  150. }
  151. $condition = array('is_delete'=>1,'choice_id'=>$choiceId);
  152. $Info = M('choice')->where(array('choice_id'=>$choiceId))->find();
  153. $Info['choice_content']=htmlspecialchars_decode($Info['choice_content']);
  154. $tmp=explode(',',$Info['choice_img']);
  155. $Info['choice_img']=$tmp;
  156. $this->assign('info',$Info);
  157. $this->assign('choiceId', $choiceId);
  158. $this->display();
  159. }
  160. }