Forum.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. $service_phone = Db::name('system_config')->where('name','service_phone')->value('value');
  214. $user_id = User::where('phone',$service_phone)->value('id');
  215. if($this->request->action() == 'add'){
  216. if($user_id){
  217. $data['user_id'] = $user_id;
  218. }
  219. }
  220. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  221. //定时热搜
  222. if(!$post['hot_num']){
  223. $post['hot_num'] = 0;
  224. }
  225. if(isset($post['id'])){
  226. $info = \app\common\model\UserForum::where('id',$data['id'])->find();
  227. 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'])){
  228. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  229. $startdate = strtotime($data['regular_hot_start_time']);
  230. $enddate = strtotime($post['regular_hot_end_time']);
  231. $diff_seconds = ($enddate-$startdate)/60;
  232. $min_num = ceil($diff_seconds/10);
  233. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  234. $num = ceil($hot_num/$min_num);
  235. if($num < 0){
  236. $num = 0;
  237. }
  238. $data['regular_num'] = $num;
  239. }
  240. }else{
  241. if($post['regular_hot_end_time'] && $post['hot_target_num']){
  242. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  243. $startdate = strtotime($data['regular_hot_start_time']);
  244. $enddate = strtotime($post['regular_hot_end_time']);
  245. $diff_seconds = ($enddate-$startdate)/60;
  246. $min_num = ceil($diff_seconds/10);
  247. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  248. $num = ceil($hot_num/$min_num);
  249. if($num < 0){
  250. $num = 0;
  251. }
  252. $data['regular_num'] = $num;
  253. }
  254. }
  255. if(!$post['regular_hot_end_time']){
  256. unset($data['regular_hot_end_time']);
  257. }
  258. //定时热搜end
  259. }
  260. }
  261. /**
  262. * 回答问题
  263. * @auth true
  264. * @menu true
  265. * @throws \think\Exception
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. * @throws \think\exception\DbException
  269. * @throws \think\exception\PDOException
  270. */
  271. public function reply_forum()
  272. {
  273. if($this->request->isGet()) {
  274. $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray();
  275. if(input('id')){
  276. $raply_info = ForumReply::where('id',input('id'))->find()->toArray();
  277. $this->assign('raply_info',$raply_info);
  278. }
  279. $this->assign('forum_info',$forum_info);
  280. $this->_form($this->table,'reply_forum');
  281. }else if ($this->request->isPost()){
  282. $id = input('post.id');
  283. $content = input('post.re_content');
  284. $raply_id = input('post.raply_id');
  285. $service_phone = Db::name('system_config')->where('name','service_phone')->value('value');
  286. $user_id = User::where('phone',$service_phone)->value('id');
  287. if(!$content) $this->error('请输入内容');
  288. $issue_user = UserForum::where('id',$id)->value('user_id');
  289. $arr = [
  290. 'content'=>$content,
  291. 'issue_user'=>$issue_user,
  292. 'forum_id'=>$id
  293. ];
  294. if($user_id){
  295. $arr['user_id'] = $user_id;
  296. }
  297. if($raply_id){
  298. $res = ForumReply::where('id',$raply_id)->update($arr);
  299. }else{
  300. $res = ForumReply::create($arr);
  301. }
  302. if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问');
  303. if($raply_id){
  304. UserForum:: sendCollectMsg($id,$raply_id);
  305. }else{
  306. UserForum:: sendCollectMsg($id,$res->id);
  307. }
  308. UserForum::where('id',$id)->setInc('r_num',1);
  309. $this->success('回答完成');
  310. }
  311. }
  312. /**
  313. * 置顶设置
  314. * @auth true
  315. * @menu true
  316. * @param array $data
  317. */
  318. public function stick()
  319. {
  320. $this->_save('forum_reply', ['is_top' => input('is_top')]);
  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 forbidden()
  333. {
  334. UserForum::where('id',input('id'))->update(['status'=>0]);
  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 enable()
  350. {
  351. UserForum::where('id',input('id'))->update(['status'=>1]);
  352. UserForum::esAdd(input('id'));
  353. \app\common\model\TopSearch::saveData(input('id'),'forum');
  354. $this->success('已启用!');
  355. }
  356. /**
  357. * 问答导入
  358. * @auth true
  359. * @menu true
  360. * @throws \think\Exception
  361. * @throws \think\db\exception\DataNotFoundException
  362. * @throws \think\db\exception\ModelNotFoundException
  363. * @throws \think\exception\DbException
  364. * @throws \think\exception\PDOException
  365. */
  366. public function forum_import()
  367. {
  368. $file = request()->file('file');
  369. $file_size = $_FILES['file']['size'];
  370. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  371. //限制上传表格类型
  372. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  373. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  374. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  375. if (!file_exists($dir)) mkdir($dir, 0777, true);
  376. $info = $file->move($dir);
  377. $fileName = $info->getSaveName();
  378. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  379. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  380. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');*/
  381. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  382. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  383. $highestRow = $sheet->getHighestRow(); // 取得总行数
  384. $arr = array('A','B','C','D','E','F','G','H','I','J');
  385. // 一次读取一列
  386. $res_arr = [];
  387. for ($row = 2; $row <= $highestRow; $row++) {
  388. $row_arr = array();
  389. for ($column = 0 ;$column < count($arr) ; $column++) {
  390. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  391. $row_arr[] = $val;
  392. }
  393. $res_arr[] = $row_arr;
  394. }
  395. $success_num = 0;
  396. foreach ($res_arr as $import) {
  397. // if (!trim($import['0']) || empty($import['0'])) continue;
  398. $user_id = User::where('phone',$import['3'])->where('phone_pre',$import['2'])->value('id');
  399. $add_data = [
  400. 'title' => trim($import['0']),
  401. 'label' => trim($import['1']),
  402. 'user_id' => $user_id ? $user_id : 1,
  403. 'level' => intval($import['4']),
  404. 'comment_switch' => intval($import['5']),
  405. 'read_num' => $import['6'],
  406. 'content' => $import['7'],
  407. ];
  408. $success_num++;
  409. $forum_info = UserForum::create($add_data)->toArray();
  410. if($import['8']){
  411. $reply_arr = [
  412. 'forum_id' => $forum_info['id'],
  413. 'user_id' => 0,
  414. 'content' => $import['8'],
  415. ];
  416. if($import['9']){
  417. $user_info = User::getDefaultUser(['phone'=>$import[9]]);
  418. if($user_info){
  419. $reply_arr['user_id'] = $user_info['id'];
  420. }
  421. }
  422. $forum_reply = ForumReply::create($reply_arr)->toArray();
  423. }
  424. UserForum::esAdd($forum_info['id']);
  425. \app\common\model\TopSearch::saveData($forum_info['id'],'forum');
  426. }
  427. $this->success('成功导入记录条数:'.$success_num);
  428. }
  429. /**
  430. * 答案导入
  431. * @auth true
  432. * @menu true
  433. * @throws \think\Exception
  434. * @throws \think\db\exception\DataNotFoundException
  435. * @throws \think\db\exception\ModelNotFoundException
  436. * @throws \think\exception\DbException
  437. * @throws \think\exception\PDOException
  438. */
  439. public function apply_import()
  440. {
  441. $forum_id = $this->request->param('forum_id');
  442. $forum_info = UserForum::where('id',$forum_id)->find()->toArray();
  443. $file = request()->file('file');
  444. $file_size = $_FILES['file']['size'];
  445. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  446. //限制上传表格类型
  447. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  448. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  449. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  450. if (!file_exists($dir)) mkdir($dir, 0777, true);
  451. $info = $file->move($dir);
  452. $fileName = $info->getSaveName();
  453. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  454. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  455. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  456. $highestRow = $sheet->getHighestRow(); // 取得总行数
  457. $arr = array('A','B','C');
  458. // 一次读取一列
  459. $res_arr = [];
  460. for ($row = 2; $row <= $highestRow; $row++) {
  461. $row_arr = array();
  462. for ($column = 0 ;$column < count($arr) ; $column++) {
  463. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  464. $row_arr[] = $val;
  465. }
  466. $res_arr[] = $row_arr;
  467. }
  468. $success_num = 0;
  469. foreach ($res_arr as $import) {
  470. if (!trim($import['2']) || empty($import['2'])) continue;
  471. $user_info = User::getDefaultUser(['phone_pre'=>$import[0],'phone'=>$import[1]]);
  472. $res = ForumReply::create([
  473. 'user_id' => $user_info['id'],
  474. 'content' => $import['2'],
  475. 'issue_user' => $forum_info['user_id'],
  476. 'forum_id' => $forum_id,
  477. 'is_read' =>$user_info['id'] == $forum_info['user_id'] ? 1 : 0,
  478. ])->toArray();
  479. UserMessage:: sendUserMessage($forum_info['user_id'], 'forum', 5, 0, $user_info['id'], $forum_id, $user_info['name'] . '回复了您的提问');
  480. // 有新的回答时给关注问题的会员推送
  481. UserForum:: sendCollectMsg($forum_id,$res['id']);
  482. $success_num++;
  483. }
  484. $this->success('成功导入记录条数:'.$success_num);
  485. }
  486. }