123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 评论
  4. // +----------------------------------------------------------------------
  5. // | Author: yykun
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use think\Db;
  9. use think\Model;
  10. use app\admin\model\Common;
  11. use com\verify\HonrayVerify;
  12. use think\Cache;
  13. class Comment extends Model
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如微信模块用weixin作为数据表前缀
  18. */
  19. protected $name = 'admin_comment';
  20. protected $createTime = 'create_time';
  21. protected $autoWriteTimestamp = true;
  22. protected $insert = [
  23. 'status' => 1,
  24. ];
  25. /**
  26. * 根据ID查看评论
  27. * @author Michael_xu
  28. * @param
  29. * @return
  30. */
  31. public function read($param)
  32. {
  33. $userModel = new \app\admin\model\User();
  34. $map['comment.type'] = $param['type'] ? : ''; //默认评论类型
  35. $map['comment.type_id'] = $param['type_id'];
  36. $map['comment.isreply'] = 0;
  37. $list = Db::name('AdminComment')
  38. ->alias('comment')
  39. ->join('admin_user u','u.id=comment.user_id')
  40. ->field('comment.*,u.username,u.realname,u.thumb_img')
  41. ->where($map)
  42. ->select();
  43. foreach ($list as $key => $value) {
  44. $list[$key]['userInfo']['id'] = $value['user_id'];
  45. $list[$key]['userInfo']['username'] = $value['username'];
  46. $list[$key]['userInfo']['realname'] = $value['realname'];
  47. $list[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
  48. $list[$key]['userInfo']['thumb_img'] = $value['thumb_img'] ? getFullPath($value['thumb_img']) : '';
  49. $list[$key]['replyuserInfo'] = $userModel->getUserById($value['reply_user_id']);
  50. $replyList = [];
  51. $replyList = Db::name('AdminComment')->where(['reply_fid' => $value['comment_id']])->select();
  52. foreach ($replyList as $k=>$v) {
  53. $replyList[$k]['userInfo'] = $userModel->getUserById($v['user_id']);
  54. $replyList[$k]['replyuserInfo'] = $userModel->getUserById($v['reply_user_id']);
  55. }
  56. $list[$key]['childCommentList'] = $replyList ? : array();
  57. }
  58. return $list;
  59. }
  60. /**
  61. * 获取回复
  62. * @author Michael_xu
  63. * @param
  64. * @return
  65. */
  66. function commentList($parent_id = 0,&$result = array())
  67. {
  68. $list = $this->where(['status' => 1,'reply_id' => $parent_id])->order("create_time desc")->select();
  69. if ($list) {
  70. foreach ($list as $cm) {
  71. $thisArr =& $result[];
  72. $cm["children"] = $this->commentList($cm["comment_id"],$thisArr);
  73. $thisArr = $cm;
  74. }
  75. }
  76. return $result ? : [];
  77. }
  78. /**
  79. * 新建评论
  80. * @author Michael_xu
  81. * @param
  82. * @return
  83. */
  84. public function createData($param)
  85. {
  86. $commentInfo = [];
  87. if (!empty($param['comment_id'])) $commentInfo = Db::name('admin_comment')->where('comment_id', $param['comment_id'])->find();
  88. $param['reply_content'] = !empty($commentInfo['content']) ? $commentInfo['content'] : ''; //内容拼接保存
  89. $param['isreply'] = $param['comment_id'] ? 1 : 0; //是否是回复评论
  90. $param['reply_id'] = $param['comment_id'] ? $param['comment_id'] : 0; //回复消息id
  91. $param['reply_fid'] = !empty($param['main_id']) ? $param['main_id'] : 0; //回复最上级ID
  92. $param['reply_user_id'] = !empty($commentInfo['user_id']) ? $commentInfo['user_id'] : 0; //回复别人ID
  93. $param['status'] = 1;
  94. unset($param['main_id']);
  95. unset($param['comment_id']);
  96. //print_r($param);exit;
  97. // $param['reply_content'] = $param['reply_content'] ? : ''; //内容拼接保存
  98. // $param['isreply'] = $param['reply_comment_id'] ? 1 : 0; //是否是回复评论
  99. // $param['reply_id'] = $param['reply_comment_id'] ? $param['reply_comment_id'] : 0; //回复消息id
  100. // $param['reply_fid'] = $param['reply_fid'] ? : ''; //回复最上级ID
  101. // $param['reply_user_id'] = $param['reply_user_id'] ? : ''; //回复别人ID
  102. // $param['status'] = 1;
  103. if ($this->data($param)->allowField(true)->save()) {
  104. $userModel = new \app\admin\model\User();
  105. $userInfo = $userModel->getUserById($param['user_id']);
  106. //发送站内信
  107. switch ($param['type']) {
  108. case 'task' :
  109. $taskInfo = db('task')->where(['task_id' => $param['type_id']])->field('name,create_user_id,main_user_id,owner_user_id')->find();
  110. $user_ids[] = $taskInfo['create_user_id'];
  111. if ($taskInfo['main_user_id']) {
  112. $user_ids = array_merge($user_ids,array($taskInfo['main_user_id']));
  113. }
  114. if (stringToArray($taskInfo['owner_user_id'])) {
  115. $user_ids = array_merge($user_ids,stringToArray($taskInfo['owner_user_id']));
  116. }
  117. $user_ids = array_filter(array_unique($user_ids));
  118. $sendContent = $userInfo['realname'].',评论了任务《'.$taskInfo['name'].'》:'.$param['content'];
  119. break;
  120. }
  121. if ($user_ids && $sendContent) {
  122. $resMessage = sendMessage($user_ids, $sendContent, $param['type_id'], 1);
  123. }
  124. return Db::name('AdminComment')->where('comment_id',$this->comment_id)->find();
  125. } else {
  126. $this->error = '回复失败';
  127. return false;
  128. }
  129. }
  130. /**
  131. * 删除评论
  132. * @author Michael_xu
  133. * @param
  134. * @return
  135. */
  136. public function delDataById($param)
  137. {
  138. if ($param['comment_id']) {
  139. $flag = $this->where(['comment_id' => $param['comment_id']])->delete();
  140. } else {
  141. $flag = $this->where(['type' => $param['type'],'type_id' => $param['type_id']])->delete();
  142. }
  143. if (!$flag){
  144. $this->error = '不存在或已删除';
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * 获取评论数
  151. * @author Michael_xu
  152. * @param
  153. * @return
  154. */
  155. public function getCount($type,$type_id)
  156. {
  157. $count = 0;
  158. if ($type && $type_id) {
  159. $count = $this->where(['type' => $type,'type_id' => $type_id])->count();
  160. }
  161. return $count;
  162. }
  163. }