Forum.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\ForumReply;
  4. use app\common\model\UserForum;
  5. use app\common\model\UserLevel;
  6. use app\common\model\UserMessage;
  7. use library\Controller;
  8. use think\Db;
  9. /**
  10. * 问答
  11. * Class Forum
  12. * @package app\operate\controller
  13. */
  14. class Forum extends Controller
  15. {
  16. protected $table = 'UserForum';
  17. /**
  18. * 列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $this->title = '列表';
  30. $where = [];
  31. $where[] = ['f.is_deleted','=',0];
  32. if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%'];
  33. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  34. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  35. $query = $this->_query($this->table)->alias('f')
  36. ->field('f.*,u.name,u.phone,u.headimg')
  37. ->leftJoin('store_member u','u.id = f.user_id')
  38. ->where($where)
  39. ->order('sort desc,f.id desc')->page();
  40. }
  41. protected function _index_page_filter(&$data){
  42. $app_name = sysconf('app_name');
  43. $app_logo = sysconf('app_logo');
  44. foreach ($data as &$v)
  45. {
  46. if(!$v['user_id']) $v['name'] = $app_name;
  47. if(!$v['user_id']) $v['headimg'] = $app_logo;
  48. }
  49. }
  50. /**
  51. * 添加
  52. * @auth true
  53. * @menu true
  54. * @throws \think\Exception
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \think\exception\DbException
  58. * @throws \think\exception\PDOException
  59. */
  60. public function add()
  61. {
  62. $this->title = '添加';
  63. $this->_form($this->table, 'form');
  64. }
  65. /**
  66. * 编辑
  67. * @auth true
  68. * @menu true
  69. * @throws \think\Exception
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. * @throws \think\exception\PDOException
  74. */
  75. public function edit()
  76. {
  77. $this->title = '编辑';
  78. $this->_form($this->table, 'form');
  79. }
  80. /**
  81. * 删除
  82. * @auth true
  83. * @throws \think\Exception
  84. * @throws \think\exception\PDOException
  85. */
  86. public function del()
  87. {
  88. $this->_save($this->table, ['is_deleted' => '1']);
  89. }
  90. /**
  91. * 回复
  92. * @auth true
  93. * @menu true
  94. * @throws \think\Exception
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. * @throws \think\exception\PDOException
  99. */
  100. public function reply()
  101. {
  102. $this->title = '回答列表';
  103. $where = [];
  104. $where[] = ['r.is_deleted','=',0];
  105. $where[] = ['r.forum_id','=',input('id')];
  106. if($title = input('content')) $where[] = ['r.content','like','%'.$title.'%'];
  107. if($name = input('name')) $where[] = ['u.name','like','%'.$name.'%'];
  108. if($phone = input('phone')) $where[] = ['u.phone','=',$phone];
  109. $list = $this->_query('forum_reply')
  110. ->alias('r')
  111. ->field('r.*,u.name,u.phone,u.headimg')
  112. ->leftJoin('store_member u','u.id = r.user_id')
  113. ->where($where)
  114. ->order('r.is_top desc,r.id desc')->page();
  115. $this->assign('list',$list);
  116. $this->fetch('');
  117. }
  118. protected function _reply_page_filter(&$data){
  119. $app_name = sysconf('app_name');
  120. $app_logo = sysconf('app_logo');
  121. foreach ($data as &$v)
  122. {
  123. if(!$v['user_id']) $v['name'] = $app_name;
  124. if(!$v['user_id']) $v['headimg'] = $app_logo;
  125. }
  126. }
  127. public function del_reply()
  128. {
  129. Db::name('forum_reply')->where('id',input('id'))->update(['is_deleted'=>1]);
  130. $this->success('删除成功');
  131. }
  132. protected function _form_result(&$data)
  133. {
  134. $this->success('操作成功', 'javascript:history.back()');
  135. }
  136. protected function _form_filter(&$data)
  137. {
  138. $this->level_arr = UserLevel::column('name','id');
  139. }
  140. /**
  141. * 回答问题
  142. * @auth true
  143. * @menu true
  144. * @throws \think\Exception
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. * @throws \think\exception\PDOException
  149. */
  150. public function reply_forum()
  151. {
  152. if($this->request->isGet()) {
  153. $forum_info = UserForum::where('id',input('forum_id'))->find()->toArray();
  154. $this->assign('forum_info',$forum_info);
  155. $this->_form($this->table,'reply_forum');
  156. }else if ($this->request->isPost()){
  157. $id = input('post.id');
  158. $content = input('post.content');
  159. if(!$content) $this->error('请输入内容');
  160. $issue_user = UserForum::where('id',$id)->value('user_id');
  161. $res = ForumReply::create(['user_id'=>0,'content'=>$content,'issue_user'=>$issue_user,'forum_id'=>$id]);
  162. if($issue_user)UserMessage:: sendUserMessage($issue_user,'forum',5,0,0,$id,'平台回复了您的提问');
  163. $this->success('回答完成');
  164. }
  165. }
  166. /**
  167. * 置顶设置
  168. * @auth true
  169. * @menu true
  170. * @param array $data
  171. */
  172. public function stick()
  173. {
  174. $this->_save('forum_reply', ['is_top' => input('is_top')]);
  175. }
  176. }