Forum.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. $data['is_new'] = 0;
  212. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  213. }
  214. }
  215. /**
  216. * 回答问题
  217. * @auth true
  218. * @menu true
  219. * @throws \think\Exception
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. * @throws \think\exception\DbException
  223. * @throws \think\exception\PDOException
  224. */
  225. public function reply_forum()
  226. {
  227. if($this->request->isGet()) {
  228. $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray();
  229. $raply_info = ForumReply::where('id',input('id'))->find()->toArray();
  230. $this->assign('forum_info',$forum_info);
  231. $this->assign('raply_info',$raply_info);
  232. $this->_form($this->table,'reply_forum');
  233. }else if ($this->request->isPost()){
  234. $id = input('post.id');
  235. $content = input('post.re_content');
  236. $raply_id = input('post.raply_id');
  237. if(!$content) $this->error('请输入内容');
  238. $issue_user = UserForum::where('id',$id)->value('user_id');
  239. if($raply_id){
  240. $res = ForumReply::where('id',$raply_id)->update(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  241. }else{
  242. $res = ForumReply::create(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  243. }
  244. if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问');
  245. UserForum:: sendCollectMsg($id,$res->id);
  246. UserForum::where('id',$id)->setInc('r_num',1);
  247. $this->success($id);
  248. }
  249. }
  250. /**
  251. * 置顶设置
  252. * @auth true
  253. * @menu true
  254. * @param array $data
  255. */
  256. public function stick()
  257. {
  258. $this->_save('forum_reply', ['is_top' => input('is_top')]);
  259. }
  260. /**
  261. * 禁用
  262. * @auth true
  263. * @menu true
  264. * @throws \think\Exception
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. * @throws \think\exception\DbException
  268. * @throws \think\exception\PDOException
  269. */
  270. public function forbidden()
  271. {
  272. UserForum::where('id',input('id'))->update(['status'=>0]);
  273. UserForum::esAdd(input('id'));
  274. \app\common\model\TopSearch::saveData(input('id'),'forum');
  275. $this->success('已禁用!');
  276. }
  277. /**
  278. * 启用
  279. * @auth true
  280. * @menu true
  281. * @throws \think\Exception
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. * @throws \think\exception\DbException
  285. * @throws \think\exception\PDOException
  286. */
  287. public function enable()
  288. {
  289. UserForum::where('id',input('id'))->update(['status'=>1]);
  290. UserForum::esAdd(input('id'));
  291. \app\common\model\TopSearch::saveData(input('id'),'forum');
  292. $this->success('已启用!');
  293. }
  294. /**
  295. * 问答导入
  296. * @auth true
  297. * @menu true
  298. * @throws \think\Exception
  299. * @throws \think\db\exception\DataNotFoundException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. * @throws \think\exception\DbException
  302. * @throws \think\exception\PDOException
  303. */
  304. public function forum_import()
  305. {
  306. $file = request()->file('file');
  307. $file_size = $_FILES['file']['size'];
  308. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  309. //限制上传表格类型
  310. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  311. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  312. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  313. if (!file_exists($dir)) mkdir($dir, 0777, true);
  314. $info = $file->move($dir);
  315. $fileName = $info->getSaveName();
  316. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  317. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  318. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');*/
  319. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  320. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  321. $highestRow = $sheet->getHighestRow(); // 取得总行数
  322. $arr = array('A','B','C','D','E','F','G','H','I','J');
  323. // 一次读取一列
  324. $res_arr = [];
  325. for ($row = 2; $row <= $highestRow; $row++) {
  326. $row_arr = array();
  327. for ($column = 0 ;$column < count($arr) ; $column++) {
  328. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  329. $row_arr[] = $val;
  330. }
  331. $res_arr[] = $row_arr;
  332. }
  333. $success_num = 0;
  334. foreach ($res_arr as $import) {
  335. if (!trim($import['0']) || empty($import['0'])) continue;
  336. $user_id = User::where('phone',$import['3'])->where('phone_pre',$import['2'])->value('id');
  337. $add_data = [
  338. 'title' => trim($import['0']),
  339. 'label' => trim($import['1']),
  340. 'user_id' => $user_id ? $user_id : 1,
  341. 'level' => intval($import['4']),
  342. 'comment_switch' => intval($import['5']),
  343. 'read_num' => $import['6'],
  344. 'content' => $import['7'],
  345. ];
  346. $success_num++;
  347. $forum_info = UserForum::create($add_data)->toArray();
  348. if($import['8']){
  349. $reply_arr = [
  350. 'forum_id' => $forum_info['id'],
  351. 'user_id' => 0,
  352. 'content' => $import['8'],
  353. ];
  354. if($import['9']){
  355. $user_info = User::getDefaultUser(['phone'=>$import[9]]);
  356. if($user_info){
  357. $reply_arr['user_id'] = $user_info['id'];
  358. }
  359. }
  360. $forum_reply = ForumReply::create($reply_arr)->toArray();
  361. }
  362. UserForum::esAdd($forum_info['id']);
  363. \app\common\model\TopSearch::saveData($forum_info['id'],'forum');
  364. }
  365. $this->success('成功导入记录条数:'.$success_num);
  366. }
  367. /**
  368. * 答案导入
  369. * @auth true
  370. * @menu true
  371. * @throws \think\Exception
  372. * @throws \think\db\exception\DataNotFoundException
  373. * @throws \think\db\exception\ModelNotFoundException
  374. * @throws \think\exception\DbException
  375. * @throws \think\exception\PDOException
  376. */
  377. public function apply_import()
  378. {
  379. $forum_id = $this->request->param('forum_id');
  380. $forum_info = UserForum::where('id',$forum_id)->find()->toArray();
  381. $file = request()->file('file');
  382. $file_size = $_FILES['file']['size'];
  383. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  384. //限制上传表格类型
  385. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  386. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  387. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  388. if (!file_exists($dir)) mkdir($dir, 0777, true);
  389. $info = $file->move($dir);
  390. $fileName = $info->getSaveName();
  391. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  392. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  393. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  394. $highestRow = $sheet->getHighestRow(); // 取得总行数
  395. $arr = array('A','B','C');
  396. // 一次读取一列
  397. $res_arr = [];
  398. for ($row = 2; $row <= $highestRow; $row++) {
  399. $row_arr = array();
  400. for ($column = 0 ;$column < count($arr) ; $column++) {
  401. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  402. $row_arr[] = $val;
  403. }
  404. $res_arr[] = $row_arr;
  405. }
  406. $success_num = 0;
  407. foreach ($res_arr as $import) {
  408. if (!trim($import['2']) || empty($import['2'])) continue;
  409. $user_info = User::getDefaultUser(['phone_pre'=>$import[0],'phone'=>$import[1]]);
  410. $res = ForumReply::create([
  411. 'user_id' => $user_info['id'],
  412. 'content' => $import['2'],
  413. 'issue_user' => $forum_info['user_id'],
  414. 'forum_id' => $forum_id,
  415. 'is_read' =>$user_info['id'] == $forum_info['user_id'] ? 1 : 0,
  416. ])->toArray();
  417. UserMessage:: sendUserMessage($forum_info['user_id'], 'forum', 5, 0, $user_info['id'], $forum_id, $user_info['name'] . '回复了您的提问');
  418. // 有新的回答时给关注问题的会员推送
  419. UserForum:: sendCollectMsg($forum_id,$res['id']);
  420. $success_num++;
  421. }
  422. $this->success('成功导入记录条数:'.$success_num);
  423. }
  424. }