Forum.php 16 KB

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