Comment.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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'] == 12){
  82. $where3=[];
  83. $where3['manga_id'] = ['eq',$v['comment_rid']];
  84. $vod = model('Manga')->infoData($where3);
  85. $list[$k]['data'] = $vod['info'];
  86. }
  87. elseif($v['comment_mid'] == 11){
  88. $where3=[];
  89. $where3['website_id'] = ['eq',$v['comment_rid']];
  90. $vod = model('Website')->infoData($where3);
  91. $list[$k]['data'] = $vod['info'];
  92. }
  93. }
  94. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  95. }
  96. public function listCacheData($lp)
  97. {
  98. if (!is_array($lp)) {
  99. $lp = json_decode($lp, true);
  100. }
  101. $order = $lp['order'];
  102. $by = $lp['by'];
  103. $paging = $lp['paging'];
  104. $start = abs(intval($lp['start']));
  105. $num = abs(intval($lp['num']));
  106. $rid = abs(intval($lp['rid']));
  107. $pid = abs(intval($lp['pid']));
  108. $mid = abs(intval($lp['mid']));
  109. $uid = abs(intval($lp['uid']));
  110. $half = abs(intval($lp['half']));
  111. $pageurl = $lp['pageurl'];
  112. $page = 1;
  113. $where = [];
  114. if(empty($num)){
  115. $num = 20;
  116. }
  117. if($start>1){
  118. $start--;
  119. }
  120. if (!in_array($mid, ['1','2','3','8','9'])) {
  121. //$mid = 1;
  122. }
  123. if(!in_array($paging, ['yes', 'no'])) {
  124. $paging = 'no';
  125. }
  126. if($paging=='yes') {
  127. $param = mac_param_url();
  128. if(!empty($param['mid'])){
  129. $mid = $param['mid'];
  130. }
  131. if(!empty($param['rid'])){
  132. $rid = $param['rid'];
  133. }
  134. if(!empty($param['pid'])){
  135. $pid = $param['pid'];
  136. }
  137. if(!empty($param['by'])){
  138. $by = $param['by'];
  139. }
  140. if(!empty($param['order'])){
  141. $order = $param['order'];
  142. }
  143. if(!empty($param['page'])){
  144. $page = intval($param['page']);
  145. }
  146. foreach($param as $k=>$v){
  147. if(empty($v)){
  148. unset($param[$k]);
  149. }
  150. }
  151. if(empty($pageurl)){
  152. $pageurl = 'comment/index';
  153. }
  154. $param['page'] = 'PAGELINK';
  155. $pageurl = mac_url($pageurl,$param);
  156. }
  157. $where['comment_status'] = ['eq',1];
  158. $where['comment_pid'] = ['eq',0];
  159. if(!empty($rid)){
  160. $where['comment_rid'] = ['eq',$rid];
  161. }
  162. if(!empty($pid)){
  163. $where['comment_pid'] = ['eq',$pid];
  164. }
  165. if(!empty($uid)){
  166. $where['user_id'] = ['eq',$uid];
  167. }
  168. if(!empty($mid)){
  169. $where['comment_mid'] = ['eq',$mid];
  170. }
  171. if(!in_array($by, ['id', 'time','up','down'])) {
  172. $by = 'time';
  173. }
  174. if(!in_array($order, ['asc', 'desc'])) {
  175. $order = 'desc';
  176. }
  177. $order= 'comment_'.$by .' ' . $order;
  178. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('comment_listcache_'.join('&',$where).'_'.$order.'_'.$page.'_'.$num.'_'.$start);
  179. $res = $this->listData($where,$order,$page,$num,$start);
  180. $res['pageurl'] = $pageurl;
  181. $res['half'] = $half;
  182. return $res;
  183. }
  184. public function infoData($where,$field='*')
  185. {
  186. if(empty($where) || !is_array($where)){
  187. return ['code'=>1001,'msg'=>lang('param_err')];
  188. }
  189. $info = $this->field($field)->where($where)->find();
  190. if(empty($info)){
  191. return ['code'=>1002,'msg'=>lang('obtain_err')];
  192. }
  193. $info = $info->toArray();
  194. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  195. }
  196. public function saveData($data)
  197. {
  198. $validate = \think\Loader::validate('Comment');
  199. if(!$validate->check($data)){
  200. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  201. }
  202. // xss过滤
  203. $filter_fields = [
  204. 'comment_name',
  205. 'comment_content',
  206. ];
  207. foreach ($filter_fields as $filter_field) {
  208. if (!isset($data[$filter_field])) {
  209. continue;
  210. }
  211. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  212. }
  213. if(!empty($data['comment_id'])){
  214. $where=[];
  215. $where['comment_id'] = ['eq',$data['comment_id']];
  216. $res = $this->allowField(true)->where($where)->update($data);
  217. }
  218. else{
  219. $data['comment_time'] = time();
  220. $res = $this->allowField(true)->insert($data);
  221. }
  222. if(false === $res){
  223. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  224. }
  225. return ['code'=>1,'msg'=>lang('save_ok')];
  226. }
  227. public function delData($where)
  228. {
  229. $res = $this->where($where)->delete();
  230. if($res===false){
  231. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  232. }
  233. return ['code'=>1,'msg'=>lang('del_ok')];
  234. }
  235. public function fieldData($where,$col,$val)
  236. {
  237. if(!isset($col) || !isset($val)){
  238. return ['code'=>1001,'msg'=>lang('param_err')];
  239. }
  240. $data = [];
  241. $data[$col] = $val;
  242. $res = $this->allowField(true)->where($where)->update($data);
  243. if($res===false){
  244. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  245. }
  246. return ['code'=>1,'msg'=>lang('set_ok')];
  247. }
  248. }