Topic.php 16 KB

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