Comment.php 8.3 KB

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