Art.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Art extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'art';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. // 自动完成
  13. protected $auto = [];
  14. protected $insert = [];
  15. protected $update = [];
  16. public function getArtStatusTextAttr($val,$data)
  17. {
  18. $arr = [0=>lang('disable'),1=>lang('enable')];
  19. return $arr[$data['art_status']];
  20. }
  21. public function getArtContentTextAttr($val,$data)
  22. {
  23. $arr = explode('$$$',$data['art_content']);
  24. return $arr;
  25. }
  26. public function countData($where)
  27. {
  28. $total = $this->where($where)->count();
  29. return $total;
  30. }
  31. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  32. {
  33. if(!is_array($where)){
  34. $where = json_decode($where,true);
  35. }
  36. $where2='';
  37. if(!empty($where['_string'])){
  38. $where2 = $where['_string'];
  39. unset($where['_string']);
  40. }
  41. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  42. if($totalshow==1) {
  43. $total = $this->where($where)->count();
  44. }
  45. $list = Db::name('Art')->field($field)->where($where)->where($where2)->order($order)->limit($limit_str)->select();
  46. //dump($where);die;
  47. //echo $this->getLastSql();die;
  48. //分类
  49. $type_list = model('Type')->getCache('type_list');
  50. //用户组
  51. $group_list = model('Group')->getCache('group_list');
  52. foreach($list as $k=>$v){
  53. if($addition==1){
  54. if(!empty($v['type_id'])) {
  55. $list[$k]['type'] = $type_list[$v['type_id']];
  56. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  57. }
  58. if(!empty($v['group_id'])) {
  59. $list[$k]['group'] = $group_list[$v['group_id']];
  60. }
  61. }
  62. }
  63. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  64. }
  65. public function listRepeatData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1)
  66. {
  67. if(!is_array($where)){
  68. $where = json_decode($where,true);
  69. }
  70. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  71. $total = $this
  72. ->join('tmpart t','t.name1 = art_name')
  73. ->where($where)
  74. ->count();
  75. $list = Db::name('Art')
  76. ->join('tmpart t','t.name1 = art_name')
  77. ->field($field)
  78. ->where($where)
  79. ->order($order)
  80. ->limit($limit_str)
  81. ->select();
  82. //dump($where);die;
  83. //echo $this->getLastSql();die;
  84. //分类
  85. $type_list = model('Type')->getCache('type_list');
  86. //用户组
  87. $group_list = model('Group')->getCache('group_list');
  88. foreach($list as $k=>$v){
  89. if($addition==1){
  90. if(!empty($v['type_id'])) {
  91. $list[$k]['type'] = $type_list[$v['type_id']];
  92. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  93. }
  94. if(!empty($v['group_id'])) {
  95. $list[$k]['group'] = $group_list[$v['group_id']];
  96. }
  97. }
  98. }
  99. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  100. }
  101. public function listCacheData($lp)
  102. {
  103. if (!is_array($lp)) {
  104. $lp = json_decode($lp, true);
  105. }
  106. $order = $lp['order'];
  107. $by = $lp['by'];
  108. $type = $lp['type'];
  109. $ids = $lp['ids'];
  110. $rel = $lp['rel'];
  111. $paging = $lp['paging'];
  112. $pageurl = $lp['pageurl'];
  113. $level = $lp['level'];
  114. $wd = $lp['wd'];
  115. $tag = $lp['tag'];
  116. $class = $lp['class'];
  117. $letter = $lp['letter'];
  118. $start = intval(abs($lp['start']));
  119. $num = intval(abs($lp['num']));
  120. $half = intval(abs($lp['half']));
  121. $timeadd = $lp['timeadd'];
  122. $timehits = $lp['timehits'];
  123. $time = $lp['time'];
  124. $hitsmonth = $lp['hitsmonth'];
  125. $hitsweek = $lp['hitsweek'];
  126. $hitsday = $lp['hitsday'];
  127. $hits = $lp['hits'];
  128. $not = $lp['not'];
  129. $cachetime = $lp['cachetime'];
  130. $typenot = $lp['typenot'];
  131. $name = $lp['name'];
  132. $page = 1;
  133. $where = [];
  134. $totalshow=0;
  135. if(empty($num)){
  136. $num = 20;
  137. }
  138. if($start>1){
  139. $start--;
  140. }
  141. if(!in_array($paging, ['yes', 'no'])) {
  142. $paging = 'no';
  143. }
  144. $param = mac_param_url();
  145. if($paging=='yes') {
  146. $param = mac_search_len_check($param);
  147. $totalshow = 1;
  148. if(!empty($param['id'])) {
  149. //$type = intval($param['id']);
  150. }
  151. if(!empty($param['ids'])){
  152. $ids = $param['ids'];
  153. }
  154. if(!empty($param['tid'])) {
  155. $tid = intval($param['tid']);
  156. }
  157. if(!empty($param['level'])) {
  158. $level = $param['level'];
  159. }
  160. if(!empty($param['letter'])) {
  161. $letter = $param['letter'];
  162. }
  163. if(!empty($param['wd'])) {
  164. $wd = $param['wd'];
  165. }
  166. if(!empty($param['name'])) {
  167. $name = $param['name'];
  168. }
  169. if(!empty($param['tag'])) {
  170. $tag = $param['tag'];
  171. }
  172. if(!empty($param['class'])) {
  173. $class = $param['class'];
  174. }
  175. if(!empty($param['by'])){
  176. $by = $param['by'];
  177. }
  178. if(!empty($param['order'])){
  179. $order = $param['order'];
  180. }
  181. if(!empty($param['page'])){
  182. $page = intval($param['page']);
  183. }
  184. foreach($param as $k=>$v){
  185. if(empty($v)){
  186. unset($param[$k]);
  187. }
  188. }
  189. if(empty($pageurl)){
  190. $pageurl = 'art/type';
  191. }
  192. $param['page'] = 'PAGELINK';
  193. if($pageurl=='art/type' || $pageurl=='art/show'){
  194. $type = intval( $GLOBALS['type_id'] );
  195. $type_list = model('Type')->getCache('type_list');
  196. $type_info = $type_list[$type];
  197. $flag='type';
  198. if($pageurl == 'art/show'){
  199. $flag='show';
  200. }
  201. $pageurl = mac_url_type($type_info,$param,$flag);
  202. }
  203. else{
  204. $pageurl = mac_url($pageurl,$param);
  205. }
  206. }
  207. $where['art_status'] = ['eq',1];
  208. if(!empty($level)) {
  209. if($level=='all'){
  210. $level = '1,2,3,4,5,6,7,8,9';
  211. }
  212. $where['art_level'] = ['in',explode(',',$level)];
  213. }
  214. if(!empty($ids)) {
  215. if($ids!='all'){
  216. $where['art_id'] = ['in',explode(',',$ids)];
  217. }
  218. }
  219. if(!empty($not)){
  220. $where['art_id'] = ['not in',explode(',',$not)];
  221. }
  222. if(!empty($rel)){
  223. $tmp = explode(',',$rel);
  224. if(is_numeric($rel) || mac_array_check_num($tmp)==true ){
  225. $where['art_id'] = ['in',$tmp];
  226. }
  227. else{
  228. $where['art_rel_art'] = ['like', mac_like_arr($rel),'OR'];
  229. }
  230. }
  231. if(!empty($letter)){
  232. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  233. $letter='0,1,2,3,4,5,6,7,8,9';
  234. }
  235. $where['art_letter'] = ['in',explode(',',$letter)];
  236. }
  237. if(!empty($timeadd)){
  238. $s = intval(strtotime($timeadd));
  239. $where['art_time_add'] =['gt',$s];
  240. }
  241. if(!empty($timehits)){
  242. $s = intval(strtotime($timehits));
  243. $where['art_time_hits'] =['gt',$s];
  244. }
  245. if(!empty($time)){
  246. $s = intval(strtotime($time));
  247. $where['art_time'] =['gt',$s];
  248. }
  249. if(!empty($type)) {
  250. if($type=='current'){
  251. $type = intval( $GLOBALS['type_id'] );
  252. }
  253. if($type!='all') {
  254. $tmp_arr = explode(',', $type);
  255. $type_list = model('Type')->getCache('type_list');
  256. $type = [];
  257. foreach ($type_list as $k2 => $v2) {
  258. if (in_array($v2['type_id'] . '', $tmp_arr) || in_array($v2['type_pid'] . '', $tmp_arr)) {
  259. $type[] = $v2['type_id'];
  260. }
  261. }
  262. $type = array_unique($type);
  263. $where['type_id'] = ['in', implode(',', $type)];
  264. }
  265. }
  266. if(!empty($typenot)){
  267. $where['type_id'] = ['not in',$typenot];
  268. }
  269. if(!empty($tid)) {
  270. $where['type_id|type_id_1'] = ['eq',$tid];
  271. }
  272. if(!empty($hitsmonth)){
  273. $tmp = explode(' ',$hitsmonth);
  274. if(count($tmp)==1){
  275. $where['art_hits_month'] = ['gt', $tmp];
  276. }
  277. else{
  278. $where['art_hits_month'] = [$tmp[0],$tmp[1]];
  279. }
  280. }
  281. if(!empty($hitsweek)){
  282. $tmp = explode(' ',$hitsweek);
  283. if(count($tmp)==1){
  284. $where['art_hits_week'] = ['gt', $tmp];
  285. }
  286. else{
  287. $where['art_hits_week'] = [$tmp[0],$tmp[1]];
  288. }
  289. }
  290. if(!empty($hitsday)){
  291. $tmp = explode(' ',$hitsday);
  292. if(count($tmp)==1){
  293. $where['art_hits_day'] = ['gt', $tmp];
  294. }
  295. else{
  296. $where['art_hits_day'] = [$tmp[0],$tmp[1]];
  297. }
  298. }
  299. if(!empty($hits)){
  300. $tmp = explode(' ',$hits);
  301. if(count($tmp)==1){
  302. $where['art_hits'] = ['gt', $tmp];
  303. }
  304. else{
  305. $where['art_hits'] = [$tmp[0],$tmp[1]];
  306. }
  307. }
  308. if(!empty($wd)) {
  309. $role = 'art_name';
  310. if(!empty($GLOBALS['config']['app']['search_art_rule'])){
  311. $role .= '|'.$GLOBALS['config']['app']['search_art_rule'];
  312. }
  313. $where[$role] = ['like', '%' . $wd . '%'];
  314. }
  315. if(!empty($name)) {
  316. $where['art_name'] = ['like', mac_like_arr($name),'OR'];
  317. }
  318. if(!empty($tag)) {
  319. $where['art_tag'] = ['like', mac_like_arr($tag),'OR'];
  320. }
  321. if(!empty($class)) {
  322. $where['art_class'] = ['like',mac_like_arr($class),'OR'];
  323. }
  324. if(defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['popedom_filter'] ==1){
  325. $type_ids = mac_get_popedom_filter($GLOBALS['user']['group']['group_type']);
  326. if(!empty($type_ids)){
  327. if(!empty($where['type_id'])){
  328. $where['type_id'] = [ $where['type_id'],['not in', explode(',',$type_ids)] ];
  329. }
  330. else{
  331. $where['type_id'] = ['not in', explode(',',$type_ids)];
  332. }
  333. }
  334. }
  335. if($by=='rnd'){
  336. $data_count = $this->countData($where);
  337. $page_total = floor($data_count / $lp['num']) + 1;
  338. if($data_count < $lp['num']){
  339. $lp['num'] = $data_count;
  340. }
  341. $randi = @mt_rand(1, $page_total);
  342. $page = $randi;
  343. $by = 'hits_week';
  344. $order = 'desc';
  345. }
  346. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd'])) {
  347. $by = 'time';
  348. }
  349. if(!in_array($order, ['asc', 'desc'])) {
  350. $order = 'desc';
  351. }
  352. $order= 'art_'.$by .' ' . $order;
  353. $where_cache = $where;
  354. if(!empty($randi)){
  355. unset($where_cache['art_id']);
  356. $where_cache['order'] = 'rnd';
  357. }
  358. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('art_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  359. $res = Cache::get($cach_name);
  360. if(empty($cachetime)){
  361. $cachetime = $GLOBALS['config']['app']['cache_time'];
  362. }
  363. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  364. $res = $this->listData($where,$order,$page,$num,$start,'*',1,$totalshow);
  365. if($GLOBALS['config']['app']['cache_core']==1) {
  366. Cache::set($cach_name, $res, $cachetime);
  367. }
  368. }
  369. $res['pageurl'] = $pageurl;
  370. $res['half'] = $half;
  371. return $res;
  372. }
  373. public function infoData($where,$field='*',$cache=0)
  374. {
  375. if(empty($where) || !is_array($where)){
  376. return ['code'=>1001,'msg'=>lang('param_err')];
  377. }
  378. $data_cache = false;
  379. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'art_detail_'.$where['art_id'][1].'_'.$where['art_en'][1];
  380. if($where['art_id'][0]=='eq' || $where['art_en'][0]=='eq'){
  381. $data_cache = true;
  382. }
  383. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  384. $info = Cache::get($key);
  385. }
  386. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['art_id'])) {
  387. $info = $this->field($field)->where($where)->find();
  388. if (empty($info)) {
  389. return ['code' => 1002, 'msg' => lang('obtain_err')];
  390. }
  391. $info = $info->toArray();
  392. //内容
  393. if (!empty($info['art_content'])) {
  394. $info['art_page_list'] = mac_art_list($info['art_title'], $info['art_note'], $info['art_content']);
  395. $info['art_page_total'] = count($info['art_page_list']);
  396. }
  397. if(!empty($info['art_pic_screenshot'])){
  398. $info['art_pic_screenshot_list'] = mac_screenshot_list($info['art_pic_screenshot']);
  399. }
  400. //分类
  401. if (!empty($info['type_id'])) {
  402. $type_list = model('Type')->getCache('type_list');
  403. $info['type'] = $type_list[$info['type_id']];
  404. $info['type_1'] = $type_list[$info['type']['type_pid']];
  405. }
  406. //用户组
  407. if (!empty($info['group_id'])) {
  408. $group_list = model('Group')->getCache('group_list');
  409. $info['group'] = $group_list[$info['group_id']];
  410. }
  411. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  412. Cache::set($key, $info);
  413. }
  414. }
  415. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  416. }
  417. public function saveData($data)
  418. {
  419. $validate = \think\Loader::validate('Art');
  420. if(!$validate->check($data)){
  421. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  422. }
  423. $key = 'art_detail_'.$data['art_id'];
  424. Cache::rm($key);
  425. $key = 'art_detail_'.$data['art_en'];
  426. Cache::rm($key);
  427. $key = 'art_detail_'.$data['art_id'].'_'.$data['art_en'];
  428. Cache::rm($key);
  429. $type_list = model('Type')->getCache('type_list');
  430. $type_info = $type_list[$data['type_id']];
  431. $data['type_id_1'] = $type_info['type_pid'];
  432. if(empty($data['art_en'])){
  433. $data['art_en'] = Pinyin::get($data['art_name']);
  434. }
  435. if(empty($data['art_letter'])){
  436. $data['art_letter'] = strtoupper(substr($data['art_en'],0,1));
  437. }
  438. if(!empty($data['art_pic_screenshot'])){
  439. $data['art_pic_screenshot'] = str_replace( array(chr(10),chr(13)), array('','#'),$data['art_pic_screenshot']);
  440. }
  441. if(!empty($data['art_content'])) {
  442. $data['art_content'] = join('$$$', $data['art_content']);
  443. $data['art_title'] = join('$$$', $data['art_title']);
  444. $data['art_note'] = join('$$$', $data['art_note']);
  445. $pattern_src = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/';
  446. @preg_match_all($pattern_src, $data['art_content'], $match_src1);
  447. if (!empty($match_src1)) {
  448. foreach ($match_src1[1] as $v1) {
  449. $v2 = str_replace($GLOBALS['config']['upload']['protocol'] . ':', 'mac:', $v1);
  450. $data['art_content'] = str_replace($v1, $v2, $data['art_content']);
  451. }
  452. if (empty($data['art_pic'])) {
  453. $data['art_pic'] = (string)$match_src1[1][0];
  454. }
  455. }
  456. unset($match_src1);
  457. }
  458. if(empty($data['art_blurb'])){
  459. $data['art_blurb'] = mac_substring( str_replace('$$$','', strip_tags($data['art_content'])),100);
  460. }
  461. if($data['uptime']==1){
  462. $data['art_time'] = time();
  463. }
  464. if($data['uptag']==1){
  465. $data['art_tag'] = mac_get_tag($data['art_name'], $data['art_content']);
  466. }
  467. unset($data['uptime']);
  468. unset($data['uptag']);
  469. // xss过滤
  470. $filter_fields = [
  471. 'art_name',
  472. 'art_sub',
  473. 'art_en',
  474. 'art_color',
  475. 'art_from',
  476. 'art_author',
  477. 'art_tag',
  478. 'art_class',
  479. 'art_pic',
  480. 'art_pic_thumb',
  481. 'art_pic_slide',
  482. 'art_blurb',
  483. 'art_remarks',
  484. 'art_jumpurl',
  485. 'art_tpl',
  486. 'art_rel_art',
  487. 'art_rel_vod',
  488. 'art_pwd',
  489. 'art_pwd_url',
  490. ];
  491. foreach ($filter_fields as $filter_field) {
  492. if (!isset($data[$filter_field])) {
  493. continue;
  494. }
  495. $data[$filter_field] = mac_filter_xss($data[$filter_field]);
  496. }
  497. if(!empty($data['art_id'])){
  498. $where=[];
  499. $where['art_id'] = ['eq',$data['art_id']];
  500. $res = $this->allowField(true)->where($where)->update($data);
  501. }
  502. else{
  503. $data['art_time_add'] = time();
  504. $data['art_time'] = time();
  505. $res = $this->allowField(true)->insert($data);
  506. }
  507. if(false === $res){
  508. return ['code'=>1002,'msg'=>lang('save_err').':'.$this->getError() ];
  509. }
  510. return ['code'=>1,'msg'=>lang('save_ok')];
  511. }
  512. public function delData($where)
  513. {
  514. $list = $this->listData($where,'',1,9999);
  515. if($list['code'] !==1){
  516. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  517. }
  518. $path = './';
  519. foreach($list['list'] as $k=>$v){
  520. $pic = $path.$v['art_pic'];
  521. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  522. unlink($pic);
  523. }
  524. $pic = $path.$v['art_pic_thumb'];
  525. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  526. unlink($pic);
  527. }
  528. $pic = $path.$v['art_pic_slide'];
  529. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  530. unlink($pic);
  531. }
  532. if($GLOBALS['config']['view']['art_detail'] ==2 ){
  533. $lnk = mac_url_art_detail($v);
  534. $lnk = reset_html_filename($lnk);
  535. if(file_exists($lnk)){
  536. unlink($lnk);
  537. }
  538. }
  539. }
  540. $res = $this->where($where)->delete();
  541. if($res===false){
  542. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  543. }
  544. return ['code'=>1,'msg'=>lang('del_ok')];
  545. }
  546. public function fieldData($where,$update)
  547. {
  548. if(!is_array($update)){
  549. return ['code'=>1001,'msg'=>lang('param_err')];
  550. }
  551. $res = $this->allowField(true)->where($where)->update($update);
  552. if($res===false){
  553. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  554. }
  555. $list = $this->field('art_id,art_name,art_en')->where($where)->select();
  556. foreach($list as $k=>$v){
  557. $key = 'art_detail_'.$v['art_id'];
  558. Cache::rm($key);
  559. $key = 'art_detail_'.$v['art_en'];
  560. Cache::rm($key);
  561. }
  562. return ['code'=>1,'msg'=>lang('set_ok')];
  563. }
  564. public function updateToday($flag='art')
  565. {
  566. $today = strtotime(date('Y-m-d'));
  567. $where = [];
  568. $where['art_time'] = ['gt',$today];
  569. if($flag=='type'){
  570. $ids = $this->where($where)->column('type_id');
  571. }
  572. else{
  573. $ids = $this->where($where)->column('art_id');
  574. }
  575. if(empty($ids)){
  576. $ids = [];
  577. }else{
  578. $ids = array_unique($ids);
  579. }
  580. return ['code'=>1,'msg'=>lang('obtain_ok'),'data'=> join(',',$ids) ];
  581. }
  582. }