Comment.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Comment extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'comment';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function getCommentStatusTextAttr($val,$data)
  15. {
  16. $arr = [0=>lang('disable'),1=>lang('enable')];
  17. return $arr[$data['comment_status']];
  18. }
  19. public function countData($where)
  20. {
  21. $total = $this->where($where)->count();
  22. return $total;
  23. }
  24. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  25. {
  26. $page = $page > 0 ? (int)$page : 1;
  27. $limit = $limit ? (int)$limit : 20;
  28. $start = $start ? (int)$start : 0;
  29. if(!is_array($where)){
  30. $where = json_decode($where,true);
  31. }
  32. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  33. if($totalshow==1) {
  34. $total = $this->where($where)->count();
  35. }
  36. $list = Db::name('Comment')->field($field)->where($where)->order($order)->limit($limit_str)->select();
  37. $user_ids=[];
  38. foreach($list as $k=>$v){
  39. $list[$k]['user_portrait'] = mac_get_user_portrait($v['user_id']);
  40. $list[$k]['comment_content'] = mac_restore_htmlfilter($list[$k]['comment_content']);
  41. $where2=[];
  42. $where2['comment_pid'] = $v['comment_id'];
  43. $where2['comment_status'] = ['eq',1];
  44. $sub = Db::name('Comment')->where($where2)->order($order)->select();
  45. $list[$k]['sub'] = $sub;
  46. foreach($sub as $k2=>$v2){
  47. $list[$k]['sub'][$k2]['user_portrait'] = mac_get_user_portrait($v2['user_id']);
  48. $list[$k]['sub'][$k2]['comment_content'] = mac_restore_htmlfilter($list[$k]['sub'][$k2]['comment_content']);
  49. }
  50. $list[$k]['data'] = [];
  51. if($v['comment_mid'] == 1){
  52. $where3=[];
  53. $where3['vod_id'] = ['eq',$v['comment_rid']];
  54. $vod = model('Vod')->infoData($where3);
  55. $list[$k]['data'] = $vod['info'];
  56. }
  57. elseif($v['comment_mid'] == 2){
  58. $where3=[];
  59. $where3['art_id'] = ['eq',$v['comment_rid']];
  60. $vod = model('Art')->infoData($where3);
  61. $list[$k]['data'] = $vod['info'];
  62. }
  63. elseif($v['comment_mid'] == 3){
  64. $where3=[];
  65. $where3['topic_id'] = ['eq',$v['comment_rid']];
  66. $vod = model('Topic')->infoData($where3);
  67. $list[$k]['data'] = $vod['info'];
  68. }
  69. elseif($v['comment_mid'] == 8){
  70. $where3=[];
  71. $where3['actor_id'] = ['eq',$v['comment_rid']];
  72. $vod = model('Actor')->infoData($where3);
  73. $list[$k]['data'] = $vod['info'];
  74. }
  75. elseif($v['comment_mid'] == 9){
  76. $where3=[];
  77. $where3['role_id'] = ['eq',$v['comment_rid']];
  78. $vod = model('Role')->infoData($where3);
  79. $list[$k]['data'] = $vod['info'];
  80. }
  81. elseif($v['comment_mid'] == 11){
  82. $where3=[];
  83. $where3['website_id'] = ['eq',$v['comment_rid']];
  84. $vod = model('Website')->infoData($where3);
  85. $list[$k]['data'] = $vod['info'];
  86. }
  87. }
  88. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  89. }
  90. public function listCacheData($lp)
  91. {
  92. if (!is_array($lp)) {
  93. $lp = json_decode($lp, true);
  94. }
  95. $order = $lp['order'];
  96. $by = $lp['by'];
  97. $paging = $lp['paging'];
  98. $start = intval(abs($lp['start']));
  99. $num = intval(abs($lp['num']));
  100. $rid = intval(abs($lp['rid']));
  101. $pid = intval(abs($lp['pid']));
  102. $mid = intval(abs($lp['mid']));
  103. $uid = intval(abs($lp['uid']));
  104. $half = intval(abs($lp['half']));
  105. $pageurl = $lp['pageurl'];
  106. $page = 1;
  107. $where = [];
  108. if(empty($num)){
  109. $num = 20;
  110. }
  111. if($start>1){
  112. $start--;
  113. }
  114. if (!in_array($mid, ['1','2','3','8','9'])) {
  115. //$mid = 1;
  116. }
  117. if(!in_array($paging, ['yes', 'no'])) {
  118. $paging = 'no';
  119. }
  120. if($paging=='yes') {
  121. $param = mac_param_url();
  122. if(!empty($param['mid'])){
  123. $mid = $param['mid'];
  124. }
  125. if(!empty($param['rid'])){
  126. $rid = $param['rid'];
  127. }
  128. if(!empty($param['pid'])){
  129. $pid = $param['pid'];
  130. }
  131. if(!empty($param['by'])){
  132. $by = $param['by'];
  133. }
  134. if(!empty($param['order'])){
  135. $order = $param['order'];
  136. }
  137. if(!empty($param['page'])){
  138. $page = intval($param['page']);
  139. }
  140. foreach($param as $k=>$v){
  141. if(empty($v)){
  142. unset($param[$k]);
  143. }
  144. }
  145. if(empty($pageurl)){
  146. $pageurl = 'comment/index';
  147. }
  148. $param['page'] = 'PAGELINK';
  149. $pageurl = mac_url($pageurl,$param);
  150. }
  151. $where['comment_status'] = ['eq',1];
  152. $where['comment_pid'] = ['eq',0];
  153. if(!empty($rid)){
  154. $where['comment_rid'] = ['eq',$rid];
  155. }
  156. if(!empty($pid)){
  157. $where['comment_pid'] = ['eq',$pid];
  158. }
  159. if(!empty($uid)){
  160. $where['user_id'] = ['eq',$uid];
  161. }
  162. if(!empty($mid)){
  163. $where['comment_mid'] = ['eq',$mid];
  164. }
  165. if(!in_array($by, ['id', 'time','up','down'])) {
  166. $by = 'time';
  167. }
  168. if(!in_array($order, ['asc', 'desc'])) {
  169. $order = 'desc';
  170. }
  171. $order= 'comment_'.$by .' ' . $order;
  172. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('comment_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
  173. $res = $this->listData($where,$order,$page,$num,$start);
  174. $res['pageurl'] = $pageurl;
  175. $res['half'] = $half;
  176. return $res;
  177. }
  178. public function infoData($where,$field='*')
  179. {
  180. if(empty($where) || !is_array($where)){
  181. return ['code'=>1001,'msg'=>lang('param_err')];
  182. }
  183. $info = $this->field($field)->where($where)->find();
  184. if(empty($info)){
  185. return ['code'=>1002,'msg'=>lang('obtain_err')];
  186. }
  187. $info = $info->toArray();
  188. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  189. }
  190. public function saveData($data)
  191. {
  192. $validate = \think\Loader::validate('Comment');
  193. if(!$validate->check($data)){
  194. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  195. }
  196. // xss过滤
  197. $filter_fields = [
  198. 'comment_name',
  199. 'comment_content',
  200. ];
  201. foreach ($filter_fields as $filter_field) {
  202. if (!isset($data[$filter_field])) {
  203. continue;
  204. }
  205. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  206. }
  207. if(!empty($data['comment_id'])){
  208. $where=[];
  209. $where['comment_id'] = ['eq',$data['comment_id']];
  210. $res = $this->allowField(true)->where($where)->update($data);
  211. }
  212. else{
  213. $data['comment_time'] = time();
  214. $res = $this->allowField(true)->insert($data);
  215. }
  216. if(false === $res){
  217. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  218. }
  219. return ['code'=>1,'msg'=>lang('save_ok')];
  220. }
  221. public function delData($where)
  222. {
  223. $res = $this->where($where)->delete();
  224. if($res===false){
  225. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  226. }
  227. return ['code'=>1,'msg'=>lang('del_ok')];
  228. }
  229. public function fieldData($where,$col,$val)
  230. {
  231. if(!isset($col) || !isset($val)){
  232. return ['code'=>1001,'msg'=>lang('param_err')];
  233. }
  234. $data = [];
  235. $data[$col] = $val;
  236. $res = $this->allowField(true)->where($where)->update($data);
  237. if($res===false){
  238. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  239. }
  240. return ['code'=>1,'msg'=>lang('set_ok')];
  241. }
  242. }