Forum.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%'];
  35. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  36. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  37. $query = $this->_query($this->table)->alias('f')
  38. ->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),0) reply_num")
  39. ->leftJoin('store_member u','u.id = f.user_id')
  40. ->where($where)
  41. ->order('sort desc,f.id desc')->page();
  42. }
  43. protected function _index_page_filter(&$data){
  44. $app_name = sysconf('app_name');
  45. $app_logo = sysconf('app_logo');
  46. foreach ($data as &$v)
  47. {
  48. if(!$v['user_id']) $v['name'] = $app_name;
  49. if(!$v['user_id']) $v['headimg'] = $app_logo;
  50. $v['comment_num'] = ForumReplyComment::where([ 'forum_id' => $v['id'],'reply_id'=>0])->where('is_deleted','<',2)->count();
  51. }
  52. }
  53. /**
  54. * 添加
  55. * @auth true
  56. * @menu true
  57. * @throws \think\Exception
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @throws \think\exception\DbException
  61. * @throws \think\exception\PDOException
  62. */
  63. public function add()
  64. {
  65. $this->title = '添加';
  66. $this->_form($this->table, 'form');
  67. }
  68. /**
  69. * 编辑
  70. * @auth true
  71. * @menu true
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. * @throws \think\exception\PDOException
  77. */
  78. public function edit()
  79. {
  80. $this->title = '编辑';
  81. $this->_form($this->table, 'form');
  82. }
  83. /**
  84. * 删除
  85. * @auth true
  86. * @throws \think\Exception
  87. * @throws \think\exception\PDOException
  88. */
  89. public function del()
  90. {
  91. UserForum::where('id',input('id'))->update(['is_deleted' => '1']);
  92. UserForum::esAdd(input('id'));
  93. \app\common\model\TopSearch::saveData(input('id'),'forum');
  94. $this->success('已删除!');
  95. }
  96. /**
  97. * 删除
  98. * @auth true
  99. * @throws \think\Exception
  100. * @throws \think\exception\PDOException
  101. */
  102. public function remove()
  103. {
  104. $ids = input('id');
  105. foreach (explode(',',$ids) as $id) {
  106. UserForum::where('id',$id)->update(['is_deleted' => '1']);
  107. UserForum::esAdd( $id);
  108. \app\common\model\TopSearch::saveData($id,'forum');
  109. }
  110. $this->success('已删除!');
  111. }
  112. /**
  113. * 回复
  114. * @auth true
  115. * @menu true
  116. * @throws \think\Exception
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. * @throws \think\exception\PDOException
  121. */
  122. public function reply()
  123. {
  124. $this->title = '回答列表';
  125. $where = [];
  126. $where[] = ['r.is_deleted','=',0];
  127. $where[] = ['r.forum_id','=',input('id')];
  128. if($title = input('content')) $where[] = ['r.content','like','%'.$title.'%'];
  129. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  130. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  131. if($reply_id = input('reply_id')) $where[] = ['r.id','=',$reply_id];
  132. $list = $this->_query('forum_reply')
  133. ->alias('r')
  134. ->field('r.*,u.name,u.phone,u.headimg')
  135. ->leftJoin('store_member u','u.id = r.user_id')
  136. ->where($where)
  137. ->order('r.is_top desc,r.id desc')->page();
  138. $this->assign('list',$list);
  139. $this->fetch('');
  140. }
  141. protected function _reply_page_filter(&$data){
  142. $app_name = sysconf('app_name');
  143. $app_logo = sysconf('app_logo');
  144. foreach ($data as &$v)
  145. {
  146. if(!$v['user_id']) $v['name'] = $app_name;
  147. if(!$v['user_id']) $v['headimg'] = $app_logo;
  148. }
  149. }
  150. /**
  151. * 删除回答
  152. * @auth true
  153. * @menu true
  154. * @throws \think\Exception
  155. * @throws \think\exception\PDOException
  156. */
  157. public function del_reply()
  158. {
  159. Db::name('forum_reply')->where('id',input('id'))->update(['is_deleted'=>1]);
  160. $this->success('删除成功');
  161. }
  162. /**
  163. * 批量删除回答
  164. * @auth true
  165. * @menu true
  166. * @throws \think\Exception
  167. * @throws \think\exception\PDOException
  168. */
  169. public function remove_reply()
  170. {
  171. $ids = input('id');
  172. foreach (explode(',',$ids) as $id) {
  173. Db::name('forum_reply')->where('id',$id)->update(['is_deleted'=>1]);
  174. }
  175. $this->success('删除成功');
  176. }
  177. protected function _form_result(&$data)
  178. {
  179. UserForum::esAdd($data);
  180. \app\common\model\TopSearch::saveData($data,'forum');
  181. $this->success('操作成功', 'javascript:history.back()');
  182. }
  183. protected function _form_filter(&$data)
  184. {
  185. $this->level_arr = UserLevel::column('name','id');
  186. if($this->request->isPost()){
  187. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  188. }
  189. }
  190. /**
  191. * 回答问题
  192. * @auth true
  193. * @menu true
  194. * @throws \think\Exception
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\ModelNotFoundException
  197. * @throws \think\exception\DbException
  198. * @throws \think\exception\PDOException
  199. */
  200. public function reply_forum()
  201. {
  202. if($this->request->isGet()) {
  203. $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray();
  204. $this->assign('forum_info',$forum_info);
  205. $this->_form($this->table,'reply_forum');
  206. }else if ($this->request->isPost()){
  207. $id = input('post.id');
  208. $content = input('post.re_content');
  209. if(!$content) $this->error('请输入内容');
  210. $issue_user = UserForum::where('id',$id)->value('user_id');
  211. $res = ForumReply::create(['content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  212. if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问');
  213. UserForum:: sendCollectMsg($id,$res->id);
  214. $this->success('回答完成');
  215. }
  216. }
  217. /**
  218. * 置顶设置
  219. * @auth true
  220. * @menu true
  221. * @param array $data
  222. */
  223. public function stick()
  224. {
  225. $this->_save('forum_reply', ['is_top' => input('is_top')]);
  226. }
  227. /**
  228. * 禁用
  229. * @auth true
  230. * @menu true
  231. * @throws \think\Exception
  232. * @throws \think\db\exception\DataNotFoundException
  233. * @throws \think\db\exception\ModelNotFoundException
  234. * @throws \think\exception\DbException
  235. * @throws \think\exception\PDOException
  236. */
  237. public function forbidden()
  238. {
  239. UserForum::where('id',input('id'))->update(['status'=>0]);
  240. UserForum::esAdd(input('id'));
  241. \app\common\model\TopSearch::saveData(input('id'),'forum');
  242. $this->success('已禁用!');
  243. }
  244. /**
  245. * 启用
  246. * @auth true
  247. * @menu true
  248. * @throws \think\Exception
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. * @throws \think\exception\DbException
  252. * @throws \think\exception\PDOException
  253. */
  254. public function enable()
  255. {
  256. UserForum::where('id',input('id'))->update(['status'=>1]);
  257. UserForum::esAdd(input('id'));
  258. \app\common\model\TopSearch::saveData(input('id'),'forum');
  259. $this->success('已启用!');
  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 forum_import()
  272. {
  273. $file = request()->file('file');
  274. $file_size = $_FILES['file']['size'];
  275. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  276. //限制上传表格类型
  277. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  278. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  279. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  280. if (!file_exists($dir)) mkdir($dir, 0777, true);
  281. $info = $file->move($dir);
  282. $fileName = $info->getSaveName();
  283. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  284. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  285. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');*/
  286. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  287. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  288. $highestRow = $sheet->getHighestRow(); // 取得总行数
  289. $arr = array('A','B','C','D','E','F','G','H');
  290. // 一次读取一列
  291. $res_arr = [];
  292. for ($row = 2; $row <= $highestRow; $row++) {
  293. $row_arr = array();
  294. for ($column = 0 ;$column < count($arr) ; $column++) {
  295. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  296. $row_arr[] = $val;
  297. }
  298. $res_arr[] = $row_arr;
  299. }
  300. $success_num = 0;
  301. foreach ($res_arr as $import) {
  302. if (!trim($import['0']) || empty($import['0'])) continue;
  303. $user_id = User::where('phone',$import['3'])->where('phone_pre',$import['2'])->value('id');
  304. $add_data = [
  305. 'title' => trim($import['0']),
  306. 'label' => trim($import['1']),
  307. 'user_id' => $user_id ? $user_id : 1,
  308. 'level' => intval($import['4']),
  309. 'comment_switch' => intval($import['5']),
  310. 'read_num' => $import['6'],
  311. 'content' => $import['7'],
  312. ];
  313. $success_num++;
  314. $forum_info = UserForum::create($add_data)->toArray();
  315. UserForum::esAdd($forum_info['id']);
  316. \app\common\model\TopSearch::saveData($forum_info['id'],'forum');
  317. }
  318. $this->success('成功导入记录条数:'.$success_num);
  319. }
  320. /**
  321. * 答案导入
  322. * @auth true
  323. * @menu true
  324. * @throws \think\Exception
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. * @throws \think\exception\DbException
  328. * @throws \think\exception\PDOException
  329. */
  330. public function apply_import()
  331. {
  332. $forum_id = $this->request->param('forum_id');
  333. $forum_info = UserForum::where('id',$forum_id)->find()->toArray();
  334. $file = request()->file('file');
  335. $file_size = $_FILES['file']['size'];
  336. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  337. //限制上传表格类型
  338. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  339. if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls/xlsx格式!');
  340. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/forum_upload';
  341. if (!file_exists($dir)) mkdir($dir, 0777, true);
  342. $info = $file->move($dir);
  343. $fileName = $info->getSaveName();
  344. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/forum_upload/{$fileName}";
  345. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  346. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  347. $highestRow = $sheet->getHighestRow(); // 取得总行数
  348. $arr = array('A','B','C');
  349. // 一次读取一列
  350. $res_arr = [];
  351. for ($row = 2; $row <= $highestRow; $row++) {
  352. $row_arr = array();
  353. for ($column = 0 ;$column < count($arr) ; $column++) {
  354. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  355. $row_arr[] = $val;
  356. }
  357. $res_arr[] = $row_arr;
  358. }
  359. $success_num = 0;
  360. foreach ($res_arr as $import) {
  361. if (!trim($import['2']) || empty($import['2'])) continue;
  362. $user_info = User::getDefaultUser(['phone_pre'=>$import[0],'phone'=>$import[1]]);
  363. $res = ForumReply::create([
  364. 'user_id' => $user_info['id'],
  365. 'content' => $import['2'],
  366. 'issue_user' => $forum_info['user_id'],
  367. 'forum_id' => $forum_id,
  368. 'is_read' =>$user_info['id'] == $forum_info['user_id'] ? 1 : 0,
  369. ])->toArray();
  370. UserMessage:: sendUserMessage($forum_info['user_id'], 'forum', 5, 0, $user_info['id'], $forum_id, $user_info['name'] . '回复了您的提问');
  371. // 有新的回答时给关注问题的会员推送
  372. UserForum:: sendCollectMsg($forum_id,$res['id']);
  373. $success_num++;
  374. }
  375. $this->success('成功导入记录条数:'.$success_num);
  376. }
  377. }