Comment.php 8.5 KB

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