Forum.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\ForumReply;
  4. use app\common\model\ForumReplyComment;
  5. use app\common\model\User;
  6. use app\common\model\UserForum;
  7. use app\common\model\UserLevel;
  8. use app\common\model\UserMessage;
  9. use library\Controller;
  10. use think\Db;
  11. /**
  12. * 问答
  13. * Class Forum
  14. * @package app\operate\controller
  15. */
  16. class Forum extends Controller
  17. {
  18. protected $table = 'UserForum';
  19. /**
  20. * 列表
  21. * @auth true
  22. * @menu true
  23. * @throws \think\Exception
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. * @throws \think\exception\PDOException
  28. */
  29. public function index()
  30. {
  31. $this->title = '列表';
  32. $where = [];
  33. $where[] = ['f.is_deleted','=',0];
  34. $input = input();
  35. if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%'];
  36. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  37. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  38. // if($reply_num = input('reply_num')) $where[] = ['f.r_num','=',$reply_num];
  39. if(isset($input['reply_num']) && $input['reply_num'] != '' && $input['reply_num'] != null){
  40. $where[] = ['f.r_num','=',$input['reply_num']];
  41. }
  42. $arr = ['is_new' => 0];
  43. UserForum::alias('f')->where('is_new',1)->update($arr);
  44. $query = $this->_query($this->table)->alias('f')
  45. ->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")
  46. ->leftJoin('store_member u','u.id = f.user_id')
  47. ->where($where)
  48. ->order('sort desc,f.id desc')->page();
  49. }
  50. protected function _index_page_filter(&$data){
  51. $app_name = sysconf('app_name');
  52. $app_logo = sysconf('app_logo');
  53. foreach ($data as &$v)
  54. {
  55. if(!$v['user_id']) $v['name'] = $app_name;
  56. if(!$v['user_id']) $v['headimg'] = $app_logo;
  57. $v['comment_num'] = ForumReplyComment::where([ 'forum_id' => $v['id'],'reply_id'=>0])->where('is_deleted','<',2)->count();
  58. }
  59. }
  60. /**
  61. * 添加
  62. * @auth true
  63. * @menu true
  64. * @throws \think\Exception
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. * @throws \think\exception\PDOException
  69. */
  70. public function add()
  71. {
  72. $this->title = '添加';
  73. $this->_form($this->table, 'form');
  74. }
  75. /**
  76. * 编辑
  77. * @auth true
  78. * @menu true
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. * @throws \think\exception\PDOException
  84. */
  85. public function edit()
  86. {
  87. $this->title = '编辑';
  88. $this->_form($this->table, 'form');
  89. }
  90. /**
  91. * 删除
  92. * @auth true
  93. * @throws \think\Exception
  94. * @throws \think\exception\PDOException
  95. */
  96. public function del()
  97. {
  98. UserForum::where('id',input('id'))->update(['is_deleted' => '1']);
  99. UserForum::esAdd(input('id'));
  100. \app\common\model\TopSearch::saveData(input('id'),'forum');
  101. $this->success('已删除!');
  102. }
  103. /**
  104. * 删除
  105. * @auth true
  106. * @throws \think\Exception
  107. * @throws \think\exception\PDOException
  108. */
  109. public function remove()
  110. {
  111. $ids = input('id');
  112. foreach (explode(',',$ids) as $id) {
  113. UserForum::where('id',$id)->update(['is_deleted' => '1']);
  114. UserForum::esAdd( $id);
  115. \app\common\model\TopSearch::saveData($id,'forum');
  116. }
  117. $this->success('已删除!');
  118. }
  119. /**
  120. * 禁用
  121. * @auth true
  122. * @throws \think\Exception
  123. * @throws \think\exception\PDOException
  124. */
  125. public function disable(){
  126. $ids = input('id');
  127. foreach (explode(',',$ids) as $id) {
  128. UserForum::where('id',$id)->update(['status' => 0]);
  129. UserForum::esAdd($id);
  130. \app\common\model\TopSearch::saveData($id,'forum');
  131. }
  132. $this->success('已禁用!');
  133. }
  134. /**
  135. * 回复
  136. * @auth true
  137. * @menu true
  138. * @throws \think\Exception
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @throws \think\exception\DbException
  142. * @throws \think\exception\PDOException
  143. */
  144. public function reply()
  145. {
  146. $this->title = '回答列表';
  147. $where = [];
  148. $where[] = ['r.is_deleted','=',0];
  149. $where[] = ['r.forum_id','=',input('id')];
  150. if($title = input('content')) $where[] = ['r.content','like','%'.$title.'%'];
  151. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  152. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  153. if($reply_id = input('reply_id')) $where[] = ['r.id','=',$reply_id];
  154. $list = $this->_query('forum_reply')
  155. ->alias('r')
  156. ->field('r.*,u.name,u.phone,u.headimg')
  157. ->leftJoin('store_member u','u.id = r.user_id')
  158. ->where($where)
  159. ->order('r.is_top desc,r.id desc')->page();
  160. $this->assign('list',$list);
  161. $this->fetch('');
  162. }
  163. protected function _reply_page_filter(&$data){
  164. $app_name = sysconf('app_name');
  165. $app_logo = sysconf('app_logo');
  166. foreach ($data as &$v)
  167. {
  168. if(!$v['user_id']) $v['name'] = $app_name;
  169. if(!$v['user_id']) $v['headimg'] = $app_logo;
  170. }
  171. }
  172. /**
  173. * 删除回答
  174. * @auth true
  175. * @menu true
  176. * @throws \think\Exception
  177. * @throws \think\exception\PDOException
  178. */
  179. public function del_reply()
  180. {
  181. Db::name('forum_reply')->where('id',input('id'))->update(['is_deleted'=>1]);
  182. $reply = Db::name('forum_reply')->where('id',input('id'))->field('id,forum_id')->find();
  183. UserForum::where('id',$reply['forum_id'])->setDec('r_num',1);
  184. $this->success('删除成功');
  185. }
  186. /**
  187. * 批量删除回答
  188. * @auth true
  189. * @menu true
  190. * @throws \think\Exception
  191. * @throws \think\exception\PDOException
  192. */
  193. public function remove_reply()
  194. {
  195. $ids = input('id');
  196. foreach (explode(',',$ids) as $id) {
  197. Db::name('forum_reply')->where('id',$id)->update(['is_deleted'=>1]);
  198. }
  199. $this->success('删除成功');
  200. }
  201. protected function _form_result(&$data)
  202. {
  203. UserForum::esAdd($data);
  204. \app\common\model\TopSearch::saveData($data,'forum');
  205. $this->success('操作成功', 'javascript:history.back()');
  206. }
  207. protected function _form_filter(&$data)
  208. {
  209. $this->level_arr = UserLevel::column('name','id');
  210. if($this->request->isPost()){
  211. list($post) = [$this->request->post()];
  212. $data['is_new'] = 0;
  213. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  214. //定时热搜
  215. if(!$post['hot_num']){
  216. $post['hot_num'] = 0;
  217. }
  218. if(isset($post['id'])){
  219. $info = \app\common\model\UserForum::where('id',$data['id'])->find();
  220. 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'])){
  221. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  222. $startdate = strtotime($data['regular_hot_start_time']);
  223. $enddate = strtotime($post['regular_hot_end_time']);
  224. $diff_seconds = ($enddate-$startdate)/60;
  225. $min_num = ceil($diff_seconds/10);
  226. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  227. $num = ceil($hot_num/$min_num);
  228. if($num < 0){
  229. $num = 0;
  230. }
  231. $data['regular_num'] = $num;
  232. }
  233. }else{
  234. if($post['regular_hot_end_time'] && $post['hot_target_num']){
  235. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  236. $startdate = strtotime($data['regular_hot_start_time']);
  237. $enddate = strtotime($post['regular_hot_end_time']);
  238. $diff_seconds = ($enddate-$startdate)/60;
  239. $min_num = ceil($diff_seconds/10);
  240. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  241. $num = ceil($hot_num/$min_num);
  242. if($num < 0){
  243. $num = 0;
  244. }
  245. $data['regular_num'] = $num;
  246. }
  247. }
  248. if(!$post['regular_hot_end_time']){
  249. unset($data['regular_hot_end_time']);
  250. }
  251. //定时热搜end
  252. }
  253. }
  254. /**
  255. * 回答问题
  256. * @auth true
  257. * @menu true
  258. * @throws \think\Exception
  259. * @throws \think\db\exception\DataNotFoundException
  260. * @throws \think\db\exception\ModelNotFoundException
  261. * @throws \think\exception\DbException
  262. * @throws \think\exception\PDOException
  263. */
  264. public function reply_forum()
  265. {
  266. if($this->request->isGet()) {
  267. $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray();
  268. if(input('id')){
  269. $raply_info = ForumReply::where('id',input('id'))->find()->toArray();
  270. $this->assign('raply_info',$raply_info);
  271. }
  272. $this->assign('forum_info',$forum_info);
  273. $this->_form($this->table,'reply_forum');
  274. }else if ($this->request->isPost()){
  275. $id = input('post.id');
  276. $content = input('post.re_content');
  277. $raply_id = input('post.raply_id');
  278. if(!$content) $this->error('请输入内容');
  279. $issue_user = UserForum::where('id',$id)->value('user_id');
  280. if($raply_id){
  281. $res = ForumReply::where('id',$raply_id)->update(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  282. }else{
  283. $res = ForumReply::create(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  284. }
  285. if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问');
  286. if($raply_id){
  287. UserForum:: sendCollectMsg($id,$raply_id);
  288. }else{
  289. UserForum:: sendCollectMsg($id,$res->id);
  290. }
  291. UserForum::where('id',$id)->setInc('r_num',1);
  292. $this->success('回答完成');
  293. }
  294. }
  295. /**
  296. * 置顶设置
  297. * @auth true
  298. * @menu true
  299. * @param array $data
  300. */
  301. public function stick()
  302. {
  303. $this->_save('forum_reply', ['is_top' => input('is_top')]);
  304. }
  305. /**
  306. * 禁用
  307. * @auth true
  308. * @menu true
  309. * @throws \think\Exception
  310. * @throws \think\db\exception\DataNotFoundException
  311. * @throws \think\db\exception\ModelNotFoundException
  312. * @throws \think\exception\DbException
  313. * @throws \think\exception\PDOException
  314. */
  315. public function forbidden()
  316. {
  317. UserForum::where('id',input('id'))->update(['status'=>0]);
  318. UserForum::esAdd(input('id'));
  319. \app\common\model\TopSearch::saveData(input('id'),'forum');
  320. $this->success('已禁用!');
  321. }
  322. /**
  323. * 启用
  324. * @auth true
  325. * @menu true
  326. * @throws \think\Exception
  327. * @throws \think\db\exception\DataNotFoundException
  328. * @throws \think\db\exception\ModelNotFoundException
  329. * @throws \think\exception\DbException
  330. * @throws \think\exception\PDOException
  331. */
  332. public function enable()
  333. {
  334. UserForum::where('id',input('id'))->update(['status'=>1]);
  335. UserForum::esAdd(input('id'));
  336. \app\common\model\TopSearch::saveData(input('id'),'forum');
  337. $this->success('已启用!');
  338. }
  339. /**
  340. * 问答导入
  341. * @auth true
  342. * @menu true
  343. * @throws \think\Exception
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. * @throws \think\exception\DbException
  347. * @throws \think\exception\PDOException
  348. */
  349. public function forum_import()
  350. {
  351. $file = request()->file('file');
  352. $file_size = $_FILES['file']['size'];
  353. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  354. //限制上传表格类型
  355. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  356. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  357. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  358. if (!file_exists($dir)) mkdir($dir, 0777, true);
  359. $info = $file->move($dir);
  360. $fileName = $info->getSaveName();
  361. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  362. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  363. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');*/
  364. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  365. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  366. $highestRow = $sheet->getHighestRow(); // 取得总行数
  367. $arr = array('A','B','C','D','E','F','G','H','I','J');
  368. // 一次读取一列
  369. $res_arr = [];
  370. for ($row = 2; $row <= $highestRow; $row++) {
  371. $row_arr = array();
  372. for ($column = 0 ;$column < count($arr) ; $column++) {
  373. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  374. $row_arr[] = $val;
  375. }
  376. $res_arr[] = $row_arr;
  377. }
  378. $success_num = 0;
  379. foreach ($res_arr as $import) {
  380. // if (!trim($import['0']) || empty($import['0'])) continue;
  381. $user_id = User::where('phone',$import['3'])->where('phone_pre',$import['2'])->value('id');
  382. $add_data = [
  383. 'title' => trim($import['0']),
  384. 'label' => trim($import['1']),
  385. 'user_id' => $user_id ? $user_id : 1,
  386. 'level' => intval($import['4']),
  387. 'comment_switch' => intval($import['5']),
  388. 'read_num' => $import['6'],
  389. 'content' => $import['7'],
  390. ];
  391. $success_num++;
  392. $forum_info = UserForum::create($add_data)->toArray();
  393. if($import['8']){
  394. $reply_arr = [
  395. 'forum_id' => $forum_info['id'],
  396. 'user_id' => 0,
  397. 'content' => $import['8'],
  398. ];
  399. if($import['9']){
  400. $user_info = User::getDefaultUser(['phone'=>$import[9]]);
  401. if($user_info){
  402. $reply_arr['user_id'] = $user_info['id'];
  403. }
  404. }
  405. $forum_reply = ForumReply::create($reply_arr)->toArray();
  406. }
  407. UserForum::esAdd($forum_info['id']);
  408. \app\common\model\TopSearch::saveData($forum_info['id'],'forum');
  409. }
  410. $this->success('成功导入记录条数:'.$success_num);
  411. }
  412. /**
  413. * 答案导入
  414. * @auth true
  415. * @menu true
  416. * @throws \think\Exception
  417. * @throws \think\db\exception\DataNotFoundException
  418. * @throws \think\db\exception\ModelNotFoundException
  419. * @throws \think\exception\DbException
  420. * @throws \think\exception\PDOException
  421. */
  422. public function apply_import()
  423. {
  424. $forum_id = $this->request->param('forum_id');
  425. $forum_info = UserForum::where('id',$forum_id)->find()->toArray();
  426. $file = request()->file('file');
  427. $file_size = $_FILES['file']['size'];
  428. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  429. //限制上传表格类型
  430. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  431. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  432. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  433. if (!file_exists($dir)) mkdir($dir, 0777, true);
  434. $info = $file->move($dir);
  435. $fileName = $info->getSaveName();
  436. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  437. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  438. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  439. $highestRow = $sheet->getHighestRow(); // 取得总行数
  440. $arr = array('A','B','C');
  441. // 一次读取一列
  442. $res_arr = [];
  443. for ($row = 2; $row <= $highestRow; $row++) {
  444. $row_arr = array();
  445. for ($column = 0 ;$column < count($arr) ; $column++) {
  446. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  447. $row_arr[] = $val;
  448. }
  449. $res_arr[] = $row_arr;
  450. }
  451. $success_num = 0;
  452. foreach ($res_arr as $import) {
  453. if (!trim($import['2']) || empty($import['2'])) continue;
  454. $user_info = User::getDefaultUser(['phone_pre'=>$import[0],'phone'=>$import[1]]);
  455. $res = ForumReply::create([
  456. 'user_id' => $user_info['id'],
  457. 'content' => $import['2'],
  458. 'issue_user' => $forum_info['user_id'],
  459. 'forum_id' => $forum_id,
  460. 'is_read' =>$user_info['id'] == $forum_info['user_id'] ? 1 : 0,
  461. ])->toArray();
  462. UserMessage:: sendUserMessage($forum_info['user_id'], 'forum', 5, 0, $user_info['id'], $forum_id, $user_info['name'] . '回复了您的提问');
  463. // 有新的回答时给关注问题的会员推送
  464. UserForum:: sendCollectMsg($forum_id,$res['id']);
  465. $success_num++;
  466. }
  467. $this->success('成功导入记录条数:'.$success_num);
  468. }
  469. }