title = '列表'; $where = []; $where[] = ['f.is_deleted','=',0]; $input = input(); if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%']; if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%']; if($phone = input('phone')) $where[] = ['u.phone','=',$phone]; // if($reply_num = input('reply_num')) $where[] = ['f.r_num','=',$reply_num]; if(isset($input['reply_num']) && $input['reply_num'] != '' && $input['reply_num'] != null){ $where[] = ['f.r_num','=',$input['reply_num']]; } $arr = ['is_new' => 0]; UserForum::alias('f')->where('is_new',1)->update($arr); $query = $this->_query($this->table)->alias('f') ->field("f.*,u.name,u.phone,u.headimg,IFNULL((SELECT count(r.id) reply_num FROM dd_forum_reply as r where r.forum_id = f.id AND r.is_deleted = 0),0) reply_num") ->leftJoin('store_member u','u.id = f.user_id') ->where($where) ->order('sort desc,f.id desc')->page(); } protected function _index_page_filter(&$data){ $app_name = sysconf('app_name'); $app_logo = sysconf('app_logo'); foreach ($data as &$v) { if(!$v['user_id']) $v['name'] = $app_name; if(!$v['user_id']) $v['headimg'] = $app_logo; $v['comment_num'] = ForumReplyComment::where([ 'forum_id' => $v['id'],'reply_id'=>0])->where('is_deleted','<',2)->count(); } } /** * 添加 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function add() { $this->title = '添加'; $this->_form($this->table, 'form'); } /** * 编辑 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function edit() { $this->title = '编辑'; $this->_form($this->table, 'form'); } /** * 删除 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function del() { UserForum::where('id',input('id'))->update(['is_deleted' => '1']); UserForum::esAdd(input('id')); \app\common\model\TopSearch::saveData(input('id'),'forum'); $this->success('已删除!'); } /** * 删除 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function remove() { $ids = input('id'); foreach (explode(',',$ids) as $id) { UserForum::where('id',$id)->update(['is_deleted' => '1']); UserForum::esAdd( $id); \app\common\model\TopSearch::saveData($id,'forum'); } $this->success('已删除!'); } /** * 禁用 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function disable(){ $ids = input('id'); foreach (explode(',',$ids) as $id) { UserForum::where('id',$id)->update(['status' => 0]); UserForum::esAdd($id); \app\common\model\TopSearch::saveData($id,'forum'); } $this->success('已禁用!'); } /** * 回复 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function reply() { $this->title = '回答列表'; $where = []; $where[] = ['r.is_deleted','=',0]; $where[] = ['r.forum_id','=',input('id')]; if($title = input('content')) $where[] = ['r.content','like','%'.$title.'%']; if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%']; if($phone = input('phone')) $where[] = ['u.phone','=',$phone]; if($reply_id = input('reply_id')) $where[] = ['r.id','=',$reply_id]; $list = $this->_query('forum_reply') ->alias('r') ->field('r.*,u.name,u.phone,u.headimg') ->leftJoin('store_member u','u.id = r.user_id') ->where($where) ->order('r.is_top desc,r.id desc')->page(); $this->assign('list',$list); $this->fetch(''); } protected function _reply_page_filter(&$data){ $app_name = sysconf('app_name'); $app_logo = sysconf('app_logo'); foreach ($data as &$v) { if(!$v['user_id']) $v['name'] = $app_name; if(!$v['user_id']) $v['headimg'] = $app_logo; } } /** * 删除回答 * @auth true * @menu true * @throws \think\Exception * @throws \think\exception\PDOException */ public function del_reply() { Db::name('forum_reply')->where('id',input('id'))->update(['is_deleted'=>1]); $reply = Db::name('forum_reply')->where('id',input('id'))->field('id,forum_id')->find(); UserForum::where('id',$reply['forum_id'])->setDec('r_num',1); $this->success('删除成功'); } /** * 批量删除回答 * @auth true * @menu true * @throws \think\Exception * @throws \think\exception\PDOException */ public function remove_reply() { $ids = input('id'); foreach (explode(',',$ids) as $id) { Db::name('forum_reply')->where('id',$id)->update(['is_deleted'=>1]); } $this->success('删除成功'); } protected function _form_result(&$data) { UserForum::esAdd($data); \app\common\model\TopSearch::saveData($data,'forum'); $this->success('操作成功', 'javascript:history.back()'); } protected function _form_filter(&$data) { $this->level_arr = UserLevel::column('name','id'); if($this->request->isPost()){ list($post) = [$this->request->post()]; $data['is_new'] = 0; if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s"); //定时热搜 if(!$post['hot_num']){ $post['hot_num'] = 0; } if(isset($post['id'])){ $info = \app\common\model\UserForum::where('id',$data['id'])->find(); if(($post['regular_hot_end_time'] && $post['hot_target_num'] && $info['regular_hot_end_time'] != $post['regular_hot_end_time']) || ($info['hot_num'] != $post['hot_num'] && $post['regular_hot_end_time'] && $post['hot_target_num'])){ $data['regular_hot_start_time'] = date("Y-m-d H:i:s"); $startdate = strtotime($data['regular_hot_start_time']); $enddate = strtotime($post['regular_hot_end_time']); $diff_seconds = ($enddate-$startdate)/60; $min_num = ceil($diff_seconds/10); $hot_num = $post['hot_target_num'] - $post['hot_num']; $num = ceil($hot_num/$min_num); if($num < 0){ $num = 0; } $data['regular_num'] = $num; } }else{ if($post['regular_hot_end_time'] && $post['hot_target_num']){ $data['regular_hot_start_time'] = date("Y-m-d H:i:s"); $startdate = strtotime($data['regular_hot_start_time']); $enddate = strtotime($post['regular_hot_end_time']); $diff_seconds = ($enddate-$startdate)/60; $min_num = ceil($diff_seconds/10); $hot_num = $post['hot_target_num'] - $post['hot_num']; $num = ceil($hot_num/$min_num); if($num < 0){ $num = 0; } $data['regular_num'] = $num; } } if(!$post['regular_hot_end_time']){ unset($data['regular_hot_end_time']); } //定时热搜end } } /** * 回答问题 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function reply_forum() { if($this->request->isGet()) { $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray(); if(input('id')){ $raply_info = ForumReply::where('id',input('id'))->find()->toArray(); $this->assign('raply_info',$raply_info); } $this->assign('forum_info',$forum_info); $this->_form($this->table,'reply_forum'); }else if ($this->request->isPost()){ $id = input('post.id'); $content = input('post.re_content'); $raply_id = input('post.raply_id'); if(!$content) $this->error('请输入内容'); $issue_user = UserForum::where('id',$id)->value('user_id'); if($raply_id){ $res = ForumReply::where('id',$raply_id)->update(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]); }else{ $res = ForumReply::create(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]); } if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问'); if($raply_id){ UserForum:: sendCollectMsg($id,$raply_id); }else{ UserForum:: sendCollectMsg($id,$res->id); } UserForum::where('id',$id)->setInc('r_num',1); $this->success('回答完成'); } } /** * 置顶设置 * @auth true * @menu true * @param array $data */ public function stick() { $this->_save('forum_reply', ['is_top' => input('is_top')]); } /** * 禁用 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function forbidden() { UserForum::where('id',input('id'))->update(['status'=>0]); UserForum::esAdd(input('id')); \app\common\model\TopSearch::saveData(input('id'),'forum'); $this->success('已禁用!'); } /** * 启用 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function enable() { UserForum::where('id',input('id'))->update(['status'=>1]); UserForum::esAdd(input('id')); \app\common\model\TopSearch::saveData(input('id'),'forum'); $this->success('已启用!'); } /** * 问答导入 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function forum_import() { $file = request()->file('file'); $file_size = $_FILES['file']['size']; if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M'); //限制上传表格类型 $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1); if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!'); $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload'; if (!file_exists($dir)) mkdir($dir, 0777, true); $info = $file->move($dir); $fileName = $info->getSaveName(); $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}"; /* $reader = \PHPExcel_IOFactory::createReader('Excel2007'); if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');*/ $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath); $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始) $highestRow = $sheet->getHighestRow(); // 取得总行数 $arr = array('A','B','C','D','E','F','G','H','I','J'); // 一次读取一列 $res_arr = []; for ($row = 2; $row <= $highestRow; $row++) { $row_arr = array(); for ($column = 0 ;$column < count($arr) ; $column++) { $val = $sheet->getCellByColumnAndRow($column, $row)->getValue(); $row_arr[] = $val; } $res_arr[] = $row_arr; } $success_num = 0; foreach ($res_arr as $import) { if (!trim($import['0']) || empty($import['0'])) continue; $user_id = User::where('phone',$import['3'])->where('phone_pre',$import['2'])->value('id'); $add_data = [ 'title' => trim($import['0']), 'label' => trim($import['1']), 'user_id' => $user_id ? $user_id : 1, 'level' => intval($import['4']), 'comment_switch' => intval($import['5']), 'read_num' => $import['6'], 'content' => $import['7'], ]; $success_num++; $forum_info = UserForum::create($add_data)->toArray(); if($import['8']){ $reply_arr = [ 'forum_id' => $forum_info['id'], 'user_id' => 0, 'content' => $import['8'], ]; if($import['9']){ $user_info = User::getDefaultUser(['phone'=>$import[9]]); if($user_info){ $reply_arr['user_id'] = $user_info['id']; } } $forum_reply = ForumReply::create($reply_arr)->toArray(); } UserForum::esAdd($forum_info['id']); \app\common\model\TopSearch::saveData($forum_info['id'],'forum'); } $this->success('成功导入记录条数:'.$success_num); } /** * 答案导入 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function apply_import() { $forum_id = $this->request->param('forum_id'); $forum_info = UserForum::where('id',$forum_id)->find()->toArray(); $file = request()->file('file'); $file_size = $_FILES['file']['size']; if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M'); //限制上传表格类型 $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1); if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!'); $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload'; if (!file_exists($dir)) mkdir($dir, 0777, true); $info = $file->move($dir); $fileName = $info->getSaveName(); $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}"; $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath); $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始) $highestRow = $sheet->getHighestRow(); // 取得总行数 $arr = array('A','B','C'); // 一次读取一列 $res_arr = []; for ($row = 2; $row <= $highestRow; $row++) { $row_arr = array(); for ($column = 0 ;$column < count($arr) ; $column++) { $val = $sheet->getCellByColumnAndRow($column, $row)->getValue(); $row_arr[] = $val; } $res_arr[] = $row_arr; } $success_num = 0; foreach ($res_arr as $import) { if (!trim($import['2']) || empty($import['2'])) continue; $user_info = User::getDefaultUser(['phone_pre'=>$import[0],'phone'=>$import[1]]); $res = ForumReply::create([ 'user_id' => $user_info['id'], 'content' => $import['2'], 'issue_user' => $forum_info['user_id'], 'forum_id' => $forum_id, 'is_read' =>$user_info['id'] == $forum_info['user_id'] ? 1 : 0, ])->toArray(); UserMessage:: sendUserMessage($forum_info['user_id'], 'forum', 5, 0, $user_info['id'], $forum_id, $user_info['name'] . '回复了您的提问'); // 有新的回答时给关注问题的会员推送 UserForum:: sendCollectMsg($forum_id,$res['id']); $success_num++; } $this->success('成功导入记录条数:'.$success_num); } }