Topic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Topic extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'topic';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. protected $autoWriteTimestamp = true;
  13. // 自动完成
  14. protected $auto = [];
  15. protected $insert = [];
  16. protected $update = [];
  17. public function getTopicStatusTextAttr($val,$data)
  18. {
  19. $arr = [0=>lang('disable'),1=>lang('enable')];
  20. return $arr[$data['topic_status']];
  21. }
  22. public function countData($where)
  23. {
  24. $total = $this->where($where)->count();
  25. return $total;
  26. }
  27. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$totalshow=1)
  28. {
  29. $page = $page > 0 ? (int)$page : 1;
  30. $limit = $limit ? (int)$limit : 20;
  31. $start = $start ? (int)$start : 0;
  32. if(!is_array($where)){
  33. $where = json_decode($where,true);
  34. }
  35. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  36. if($totalshow==1) {
  37. $total = $this->where($where)->count();
  38. }
  39. $tmp = Db::name('Topic')->where($where)->order($order)->limit($limit_str)->select();
  40. $list = [];
  41. foreach($tmp as $k=>$v){
  42. $list[$v['topic_id']] = $v;
  43. }
  44. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  45. }
  46. public function listCacheData($lp)
  47. {
  48. if (!is_array($lp)) {
  49. $lp = json_decode($lp, true);
  50. }
  51. $order = $lp['order'];
  52. $by = $lp['by'];
  53. $ids = $lp['ids'];
  54. $paging = $lp['paging'];
  55. $pageurl = $lp['pageurl'];
  56. $level = $lp['level'];
  57. $letter = $lp['letter'];
  58. $tag = $lp['tag'];
  59. $class = $lp['class'];
  60. $start = intval(abs($lp['start']));
  61. $num = intval(abs($lp['num']));
  62. $half = intval(abs($lp['half']));
  63. $timeadd = $lp['timeadd'];
  64. $timehits = $lp['timehits'];
  65. $time = $lp['time'];
  66. $hitsmonth = $lp['hitsmonth'];
  67. $hitsweek = $lp['hitsweek'];
  68. $hitsday = $lp['hitsday'];
  69. $hits = $lp['hits'];
  70. $not = $lp['not'];
  71. $cachetime = $lp['cachetime'];
  72. $page = 1;
  73. $where = [];
  74. $totalshow = 0;
  75. if(empty($num)){
  76. $num = 20;
  77. }
  78. if($start>1){
  79. $start--;
  80. }
  81. if(!in_array($paging, ['yes', 'no'])) {
  82. $paging = 'no';
  83. }
  84. $param = mac_param_url();
  85. if($paging=='yes') {
  86. $param = mac_search_len_check($param);
  87. $totalshow = 1;
  88. if (!empty($param['id'])) {
  89. $ids = intval($param['id']);
  90. }
  91. if(!empty($param['ids'])){
  92. $ids = $param['ids'];
  93. }
  94. if(!empty($param['level'])) {
  95. if($param['level']=='all'){
  96. $level = '1,2,3,4,5,6,7,8,9';
  97. }
  98. else{
  99. $level = $param['level'];
  100. }
  101. }
  102. if(!empty($param['letter'])) {
  103. $letter = $param['letter'];
  104. }
  105. if(!empty($param['wd'])) {
  106. $wd = $param['wd'];
  107. }
  108. if(!empty($param['tag'])) {
  109. $tag = $param['tag'];
  110. }
  111. if(!empty($param['class'])) {
  112. $class = $param['class'];
  113. }
  114. if(!empty($param['by'])){
  115. $by = $param['by'];
  116. }
  117. if(!empty($param['order'])){
  118. $order = $param['order'];
  119. }
  120. if(!empty($param['page'])){
  121. $page = intval($param['page']);
  122. }
  123. foreach($param as $k=>$v){
  124. if(empty($v)){
  125. unset($param[$k]);
  126. }
  127. }
  128. if(empty($pageurl)){
  129. $pageurl = 'topic/index';
  130. }
  131. $param['page'] = 'PAGELINK';
  132. $pageurl = mac_url($pageurl,$param);
  133. }
  134. $where['topic_status'] = ['eq',1];
  135. if(!empty($level)) {
  136. $where['topic_level'] = ['in',explode(',',$level)];
  137. }
  138. if(!empty($ids)) {
  139. if($ids!='all'){
  140. $where['topic_id'] = ['in',explode(',',$ids)];
  141. }
  142. }
  143. if(!empty($not)){
  144. $where['topic_id'] = ['not in',explode(',',$not)];
  145. }
  146. if(!empty($letter)){
  147. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  148. $letter='0,1,2,3,4,5,6,7,8,9';
  149. }
  150. $where['topic_letter'] = ['in',explode(',',$letter)];
  151. }
  152. if(!empty($timeadd)){
  153. $s = intval(strtotime($timeadd));
  154. $where['topic_time_add'] =['gt',$s];
  155. }
  156. if(!empty($timehits)){
  157. $s = intval(strtotime($timehits));
  158. $where['topic_time_hits'] =['gt',$s];
  159. }
  160. if(!empty($time)){
  161. $s = intval(strtotime($time));
  162. $where['topic_time'] =['gt',$s];
  163. }
  164. if(!empty($hitsmonth)){
  165. $tmp = explode(' ',$hitsmonth);
  166. if(count($tmp)==1){
  167. $where['topic_hits_month'] = ['gt', $tmp];
  168. }
  169. else{
  170. $where['topic_hits_month'] = [$tmp[0],$tmp[1]];
  171. }
  172. }
  173. if(!empty($hitsweek)){
  174. $tmp = explode(' ',$hitsweek);
  175. if(count($tmp)==1){
  176. $where['topic_hits_week'] = ['gt', $tmp];
  177. }
  178. else{
  179. $where['topic_hits_week'] = [$tmp[0],$tmp[1]];
  180. }
  181. }
  182. if(!empty($hitsday)){
  183. $tmp = explode(' ',$hitsday);
  184. if(count($tmp)==1){
  185. $where['topic_hits_day'] = ['gt', $tmp];
  186. }
  187. else{
  188. $where['topic_hits_day'] = [$tmp[0],$tmp[1]];
  189. }
  190. }
  191. if(!empty($hits)){
  192. $tmp = explode(' ',$hits);
  193. if(count($tmp)==1){
  194. $where['topic_hits'] = ['gt', $tmp];
  195. }
  196. else{
  197. $where['topic_hits'] = [$tmp[0],$tmp[1]];
  198. }
  199. }
  200. if(!empty($wd)) {
  201. $where['topic_name|topic_en|topic_sub'] = ['like', '%' . $wd . '%'];
  202. }
  203. if(!empty($tag)) {
  204. $where['topic_tag'] = ['like', mac_like_arr($tag),'OR'];
  205. }
  206. if(!empty($class)) {
  207. $where['topic_type'] = ['like', mac_like_arr($class),'OR'];
  208. }
  209. if($by=='rnd'){
  210. $data_count = $this->countData($where);
  211. $page_total = floor($data_count / $lp['num']) + 1;
  212. if($data_count < $lp['num']){
  213. $lp['num'] = $data_count;
  214. }
  215. $randi = @mt_rand(1, $page_total);
  216. $page = $randi;
  217. $by = 'hits_week';
  218. $order = 'desc';
  219. }
  220. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd'])) {
  221. $by = 'time';
  222. }
  223. if(!in_array($order, ['asc', 'desc'])) {
  224. $order = 'desc';
  225. }
  226. $order= 'topic_'.$by .' ' . $order;
  227. $where_cache = $where;
  228. if(!empty($randi)){
  229. unset($where_cache['topic_id']);
  230. $where_cache['order'] = 'rnd';
  231. }
  232. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('topic_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  233. $res = Cache::get($cach_name);
  234. if(empty($cachetime)){
  235. $cachetime = $GLOBALS['config']['app']['cache_time'];
  236. }
  237. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  238. $res = $this->listData($where,$order,$page,$num,$start,'*',$totalshow);
  239. if($GLOBALS['config']['app']['cache_core']==1) {
  240. Cache::set($cach_name, $res, $cachetime);
  241. }
  242. }
  243. $res['pageurl'] = $pageurl;
  244. $res['half'] = $half;
  245. return $res;
  246. }
  247. public function infoData($where,$field='*',$cache=0)
  248. {
  249. if(empty($where) || !is_array($where)){
  250. return ['code'=>1001,'msg'=>lang('param_err')];
  251. }
  252. $data_cache = false;
  253. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'topic_detail_'.$where['topic_id'][1].'_'.$where['topic_en'][1];
  254. if($where['topic_id'][0]=='eq' || $where['topic_en'][0]=='eq'){
  255. $data_cache = true;
  256. }
  257. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  258. $info = Cache::get($key);
  259. }
  260. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['topic_id']) ) {
  261. $info = $this->field($field)->where($where)->find();
  262. if (empty($info)) {
  263. return ['code' => 1002, 'msg' => lang('obtain_err')];
  264. }
  265. $info = $info->toArray();
  266. if (!empty($info['topic_extend'])) {
  267. $info['topic_extend'] = json_decode($info['topic_extend'], true);
  268. } else {
  269. $info['topic_extend'] = json_decode('{"type":"","area":"","lang":"","year":"","star":"","director":"","state":"","version":""}', true);
  270. }
  271. $info['vod_list'] = [];
  272. $info['art_list'] = [];
  273. if (!empty($info['topic_rel_vod'])) {
  274. $where = [];
  275. $where['vod_id'] = ['in', $info['topic_rel_vod']];
  276. $where['vod_status'] = ['eq', 1];
  277. $order = 'vod_time desc';
  278. $field = '*';
  279. $res = model('Vod')->listData($where, $order, 1, 999, 0, $field);
  280. if ($res['code'] == 1) {
  281. $info['vod_list'] = $res['list'];
  282. }
  283. }
  284. if (!empty($info['topic_rel_art'])) {
  285. $where = [];
  286. $where['art_id'] = ['in', $info['topic_rel_art']];
  287. $where['art_status'] = ['eq', 1];
  288. $order = 'art_time desc';
  289. $field = '*';
  290. $res = model('Art')->listData($where, $order, 1, 999, 0, $field);
  291. if ($res['code'] == 1) {
  292. $info['art_list'] = $res['list'];
  293. }
  294. }
  295. if (!empty($info['topic_tag'])) {
  296. $where=[];
  297. $where['vod_tag'] = ['like', mac_like_arr($info['topic_tag']),'OR'];
  298. $where['vod_status'] = ['eq', 1];
  299. $order = 'vod_time desc';
  300. $field = '*';
  301. $res = model('Vod')->listData($where, $order, 1, 999, 0, $field);
  302. if ($res['code'] == 1) {
  303. $info['vod_list'] = array_merge($info['vod_list'],$res['list']);
  304. }
  305. $where=[];
  306. $where['art_tag'] = ['like', mac_like_arr($info['topic_tag']),'OR'];
  307. $where['art_status'] = ['eq', 1];
  308. $order = 'art_time desc';
  309. $field = '*';
  310. $res = model('Art')->listData($where, $order, 1, 999, 0, $field);
  311. if ($res['code'] == 1) {
  312. $info['art_list'] = array_merge($info['art_list'],$res['list']);
  313. }
  314. }
  315. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  316. Cache::set($key, $info);
  317. }
  318. }
  319. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  320. }
  321. public function saveData($data)
  322. {
  323. $validate = \think\Loader::validate('Topic');
  324. if(!$validate->check($data)){
  325. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  326. }
  327. $key = 'topic_detail_'.$data['topic_id'];
  328. Cache::rm($key);
  329. $key = 'topic_detail_'.$data['topic_en'];
  330. Cache::rm($key);
  331. $key = 'topic_detail_'.$data['topic_id'].'_'.$data['topic_en'];
  332. Cache::rm($key);
  333. if(empty($data['topic_extend'])){
  334. $data['topic_extend'] = '';
  335. }
  336. if(empty($data['topic_en'])){
  337. $data['topic_en'] = Pinyin::get($data['topic_name']);
  338. }
  339. if(!empty($data['topic_content'])) {
  340. $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
  341. @preg_match_all($pattern_src, $data['topic_content'], $match_src1);
  342. if (!empty($match_src1)) {
  343. foreach ($match_src1[1] as $v1) {
  344. $v2 = str_replace($GLOBALS['config']['upload']['protocol'] . ':', 'mac:', $v1);
  345. $data['topic_content'] = str_replace($v1, $v2, $data['topic_content']);
  346. }
  347. }
  348. unset($match_src1);
  349. }
  350. if(empty($data['topic_blurb'])){
  351. $data['topic_blurb'] = mac_substring( strip_tags($data['topic_content']) ,100);
  352. }
  353. if($data['uptime']==1){
  354. $data['topic_time'] = time();
  355. }
  356. unset($data['uptime']);
  357. // xss过滤
  358. $filter_fields = [
  359. 'topic_name',
  360. 'topic_en',
  361. 'topic_sub',
  362. 'topic_color',
  363. 'topic_tpl',
  364. 'topic_type',
  365. 'topic_pic',
  366. 'topic_pic_thumb',
  367. 'topic_pic_slide',
  368. 'topic_key',
  369. 'topic_des',
  370. 'topic_title',
  371. 'topic_blurb',
  372. 'topic_remarks',
  373. 'topic_tag',
  374. 'topic_rel_vod',
  375. 'topic_rel_art',
  376. 'topic_content',
  377. 'topic_extend',
  378. ];
  379. foreach ($filter_fields as $filter_field) {
  380. if (!isset($data[$filter_field])) {
  381. continue;
  382. }
  383. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  384. }
  385. if(!empty($data['topic_id'])){
  386. $where=[];
  387. $where['topic_id'] = ['eq',$data['topic_id']];
  388. $res = $this->allowField(true)->where($where)->update($data);
  389. }
  390. else{
  391. $data['topic_time_add'] = time();
  392. $data['topic_time'] = time();
  393. $res = $this->allowField(true)->insert($data);
  394. }
  395. if(false === $res){
  396. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  397. }
  398. return ['code'=>1,'msg'=>lang('save_ok')];
  399. }
  400. public function delData($where)
  401. {
  402. $list = $this->listData($where,'',1,9999);
  403. if($list['code'] !==1){
  404. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  405. }
  406. $path = './';
  407. foreach($list['list'] as $k=>$v){
  408. $pic = $path.$v['topic_pic'];
  409. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  410. unlink($pic);
  411. }
  412. $pic = $path.$v['topic_pic_thumb'];
  413. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  414. unlink($pic);
  415. }
  416. $pic = $path.$v['topic_pic_slide'];
  417. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  418. unlink($pic);
  419. }
  420. if($GLOBALS['config']['view']['topic_detail'] ==2 ){
  421. $lnk = mac_url_topic_detail($v);
  422. $lnk = reset_html_filename($lnk);
  423. if(file_exists($lnk)){
  424. unlink($lnk);
  425. }
  426. }
  427. }
  428. $res = $this->where($where)->delete();
  429. if($res===false){
  430. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  431. }
  432. return ['code'=>1,'msg'=>lang('del_ok')];
  433. }
  434. public function fieldData($where,$col,$val)
  435. {
  436. if(!isset($col) || !isset($val)){
  437. return ['code'=>1001,'msg'=>lang('param_err')];
  438. }
  439. $data = [];
  440. $data[$col] = $val;
  441. $res = $this->allowField(true)->where($where)->update($data);
  442. if($res===false){
  443. return ['code'=>1002,'msg'=>lang('set_err').':'.$this->getError() ];
  444. }
  445. $list = $this->field('topic_id,topic_name,topic_en')->where($where)->select();
  446. foreach($list as $k=>$v){
  447. $key = 'topic_detail_'.$v['topic_id'];
  448. Cache::rm($key);
  449. $key = 'topic_detail_'.$v['topic_en'];
  450. Cache::rm($key);
  451. }
  452. return ['code'=>1,'msg'=>lang('set_ok')];
  453. }
  454. }