Topic.php 15 KB

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