Website.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Cache;
  5. use app\common\util\Pinyin;
  6. class Website extends Base {
  7. // 设置数据表(不含前缀)
  8. protected $name = 'website';
  9. // 定义时间戳字段名
  10. protected $createTime = '';
  11. protected $updateTime = '';
  12. // 自动完成
  13. protected $auto = [];
  14. protected $insert = [];
  15. protected $update = [];
  16. public function getWebsiteStatusTextAttr($val,$data)
  17. {
  18. $arr = [0=>'禁用',1=>'启用'];
  19. return $arr[$data['website_status']];
  20. }
  21. public function countData($where)
  22. {
  23. $total = $this->where($where)->count();
  24. return $total;
  25. }
  26. public function listData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1,$totalshow=1)
  27. {
  28. if(!is_array($where)){
  29. $where = json_decode($where,true);
  30. }
  31. $where2='';
  32. if(!empty($where['_string'])){
  33. $where2 = $where['_string'];
  34. unset($where['_string']);
  35. }
  36. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  37. if($totalshow==1) {
  38. $total = $this->where($where)->count();
  39. }
  40. $list = Db::name('Website')->field($field)->where($where)->where($where2)->orderRaw($order)->limit($limit_str)->select();
  41. //分类
  42. $type_list = model('Type')->getCache('type_list');
  43. //用户组
  44. $group_list = model('Group')->getCache('group_list');
  45. foreach($list as $k=>$v){
  46. if($addition==1){
  47. if(!empty($v['type_id'])) {
  48. $list[$k]['type'] = $type_list[$v['type_id']];
  49. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  50. }
  51. if(!empty($v['group_id'])) {
  52. $list[$k]['group'] = $group_list[$v['group_id']];
  53. }
  54. }
  55. }
  56. return ['code'=>1,'msg'=>'数据列表','page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  57. }
  58. public function listRepeatData($where,$order,$page=1,$limit=20,$start=0,$field='*',$addition=1)
  59. {
  60. if(!is_array($where)){
  61. $where = json_decode($where,true);
  62. }
  63. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  64. $total = $this
  65. ->join('tmpwebsite t','t.name1 = website_name')
  66. ->where($where)
  67. ->count();
  68. $list = $this
  69. ->join('tmpwebsite t','t.name1 = website_name')
  70. ->field($field)
  71. ->where($where)
  72. ->order($order)
  73. ->limit($limit_str)
  74. ->select();
  75. //dump($where);die;
  76. //echo $this->getLastSql();die;
  77. //分类
  78. $type_list = model('Type')->getCache('type_list');
  79. //用户组
  80. $group_list = model('Group')->getCache('group_list');
  81. foreach($list as $k=>$v){
  82. if($addition==1){
  83. if(!empty($v['type_id'])) {
  84. $list[$k]['type'] = $type_list[$v['type_id']];
  85. $list[$k]['type_1'] = $type_list[$list[$k]['type']['type_pid']];
  86. }
  87. if(!empty($v['group_id'])) {
  88. $list[$k]['group'] = $group_list[$v['group_id']];
  89. }
  90. }
  91. }
  92. return ['code'=>1,'msg'=>'数据列表','page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  93. }
  94. public function listCacheData($lp)
  95. {
  96. if (!is_array($lp)) {
  97. $lp = json_decode($lp, true);
  98. }
  99. $order = $lp['order'];
  100. $by = $lp['by'];
  101. $type = $lp['type'];
  102. $ids = $lp['ids'];
  103. $paging = $lp['paging'];
  104. $pageurl = $lp['pageurl'];
  105. $level = $lp['level'];
  106. $wd = $lp['wd'];
  107. $name = $lp['name'];
  108. $area = $lp['area'];
  109. $lang = $lp['lang'];
  110. $letter = $lp['letter'];
  111. $start = intval(abs($lp['start']));
  112. $num = intval(abs($lp['num']));
  113. $half = intval(abs($lp['half']));
  114. $timeadd = $lp['timeadd'];
  115. $timehits = $lp['timehits'];
  116. $time = $lp['time'];
  117. $hitsmonth = $lp['hitsmonth'];
  118. $hitsweek = $lp['hitsweek'];
  119. $hitsday = $lp['hitsday'];
  120. $hits = $lp['hits'];
  121. $not = $lp['not'];
  122. $cachetime = $lp['cachetime'];
  123. $typenot = $lp['typenot'];
  124. $refermonth = $lp['refermonth'];
  125. $referweek = $lp['refermonth'];
  126. $referday = $lp['refermonth'];
  127. $refer = $lp['refer'];
  128. $page = 1;
  129. $where = [];
  130. $totalshow=0;
  131. if(empty($num)){
  132. $num = 20;
  133. }
  134. if($start>1){
  135. $start--;
  136. }
  137. if(!in_array($paging, ['yes', 'no'])) {
  138. $paging = 'no';
  139. }
  140. $param = mac_param_url();
  141. if($paging=='yes') {
  142. $totalshow = 1;
  143. if(!empty($param['id'])) {
  144. //$type = intval($param['id']);
  145. }
  146. if(!empty($param['ids'])){
  147. $ids = $param['ids'];
  148. }
  149. if(!empty($param['level'])) {
  150. if($param['level']=='all'){
  151. $level = '1,2,3,4,5,6,7,8,9';
  152. }
  153. else{
  154. $level = $param['level'];
  155. }
  156. }
  157. if(!empty($param['letter'])) {
  158. $letter = $param['letter'];
  159. }
  160. if(!empty($param['area'])) {
  161. $area = $param['area'];
  162. }
  163. if(!empty($param['lang'])) {
  164. $lang = $param['lang'];
  165. }
  166. if(!empty($param['wd'])) {
  167. $wd = $param['wd'];
  168. }
  169. if(!empty($param['by'])){
  170. $by = $param['by'];
  171. }
  172. if(!empty($param['order'])){
  173. $order = $param['order'];
  174. }
  175. if(!empty($param['page'])){
  176. $page = intval($param['page']);
  177. }
  178. foreach($param as $k=>$v){
  179. if(empty($v)){
  180. unset($param[$k]);
  181. }
  182. }
  183. if(empty($pageurl)){
  184. $pageurl = 'website/type';
  185. }
  186. $param['page'] = 'PAGELINK';
  187. if($pageurl=='website/type' || $pageurl=='website/show'){
  188. $type = intval( $GLOBALS['type_id'] );
  189. $type_list = model('Type')->getCache('type_list');
  190. $type_info = $type_list[$type];
  191. $flag='type';
  192. if($pageurl == 'website/show'){
  193. $flag='show';
  194. }
  195. $pageurl = mac_url_type($type_info,$param,$flag);
  196. }
  197. else{
  198. $pageurl = mac_url($pageurl,$param);
  199. }
  200. }
  201. $where['website_status'] = ['eq',1];
  202. if(!empty($level)) {
  203. if($level=='all'){
  204. $level = '1,2,3,4,5,6,7,8,9';
  205. }
  206. $where['website_level'] = ['in',explode(',',$level)];
  207. }
  208. if(!empty($ids)) {
  209. if($ids!='all'){
  210. $where['website_id'] = ['in',explode(',',$ids)];
  211. }
  212. }
  213. if(!empty($not)){
  214. $where['website_id'] = ['not in',explode(',',$not)];
  215. }
  216. if(!empty($letter)){
  217. if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
  218. $letter='0,1,2,3,4,5,6,7,8,9';
  219. }
  220. $where['website_letter'] = ['in',explode(',',$letter)];
  221. }
  222. if(!empty($timeadd)){
  223. $s = intval(strtotime($timeadd));
  224. $where['website_time_add'] =['gt',$s];
  225. }
  226. if(!empty($timehits)){
  227. $s = intval(strtotime($timehits));
  228. $where['website_time_hits'] =['gt',$s];
  229. }
  230. if(!empty($time)){
  231. $s = intval(strtotime($time));
  232. $where['website_time'] =['gt',$s];
  233. }
  234. if(!empty($type)) {
  235. if($type=='current'){
  236. $type = intval( $GLOBALS['type_id'] );
  237. }
  238. if($type!='all') {
  239. $tmp_arr = explode(',', $type);
  240. $type_list = model('Type')->getCache('type_list');
  241. $type = [];
  242. foreach ($type_list as $k2 => $v2) {
  243. if (in_array($v2['type_id'] . '', $tmp_arr) || in_array($v2['type_pid'] . '', $tmp_arr)) {
  244. $type[] = $v2['type_id'];
  245. }
  246. }
  247. $type = array_unique($type);
  248. $where['type_id'] = ['in', implode(',', $type)];
  249. }
  250. }
  251. if(!empty($typenot)){
  252. $where['type_id'] = ['not in',$typenot];
  253. }
  254. if(!empty($tid)) {
  255. $where['type_id|type_id_1'] = ['eq',$tid];
  256. }
  257. if(!empty($hitsmonth)){
  258. $tmp = explode(' ',$hitsmonth);
  259. if(count($tmp)==1){
  260. $where['website_hits_month'] = ['gt', $tmp];
  261. }
  262. else{
  263. $where['website_hits_month'] = [$tmp[0],$tmp[1]];
  264. }
  265. }
  266. if(!empty($hitsweek)){
  267. $tmp = explode(' ',$hitsweek);
  268. if(count($tmp)==1){
  269. $where['website_hits_week'] = ['gt', $tmp];
  270. }
  271. else{
  272. $where['website_hits_week'] = [$tmp[0],$tmp[1]];
  273. }
  274. }
  275. if(!empty($hitsday)){
  276. $tmp = explode(' ',$hitsday);
  277. if(count($tmp)==1){
  278. $where['website_hits_day'] = ['gt', $tmp];
  279. }
  280. else{
  281. $where['website_hits_day'] = [$tmp[0],$tmp[1]];
  282. }
  283. }
  284. if(!empty($hits)){
  285. $tmp = explode(' ',$hits);
  286. if(count($tmp)==1){
  287. $where['website_hits'] = ['gt', $tmp];
  288. }
  289. else{
  290. $where['website_hits'] = [$tmp[0],$tmp[1]];
  291. }
  292. }
  293. if(!empty($refermonth)){
  294. $tmp = explode(' ',$refermonth);
  295. if(count($tmp)==1){
  296. $where['website_refer_month'] = ['gt', $tmp];
  297. }
  298. else{
  299. $where['website_refer_month'] = [$tmp[0],$tmp[1]];
  300. }
  301. }
  302. if(!empty($referweek)){
  303. $tmp = explode(' ',$referweek);
  304. if(count($tmp)==1){
  305. $where['website_refer_week'] = ['gt', $tmp];
  306. }
  307. else{
  308. $where['website_refer_week'] = [$tmp[0],$tmp[1]];
  309. }
  310. }
  311. if(!empty($referday)){
  312. $tmp = explode(' ',$referday);
  313. if(count($tmp)==1){
  314. $where['website_refer_day'] = ['gt', $tmp];
  315. }
  316. else{
  317. $where['website_refer_day'] = [$tmp[0],$tmp[1]];
  318. }
  319. }
  320. if(!empty($refer)){
  321. $tmp = explode(' ',$refer);
  322. if(count($tmp)==1){
  323. $where['website_refer'] = ['gt', $tmp];
  324. }
  325. else{
  326. $where['website_refer'] = [$tmp[0],$tmp[1]];
  327. }
  328. }
  329. if(!empty($area)){
  330. $where['website_area'] = ['in',explode(',',$area) ];
  331. }
  332. if(!empty($lang)){
  333. $where['website_lang'] = ['in',explode(',',$lang) ];
  334. }
  335. if(!empty($name)){
  336. $where['website_name'] = ['in',explode(',',$name) ];
  337. }
  338. if(!empty($wd)) {
  339. $where['website_name|website_en'] = ['like', '%' . $wd . '%'];
  340. }
  341. if($by=='rnd'){
  342. $data_count = $this->countData($where);
  343. $page_total = floor($data_count / $lp['num']) + 1;
  344. if($data_count < $lp['num']){
  345. $lp['num'] = $data_count;
  346. }
  347. $randi = @mt_rand(1, $page_total);
  348. $page = $randi;
  349. $by = 'hits_week';
  350. $order = 'desc';
  351. }
  352. if(!in_array($by, ['id', 'time','time_add','score','hits','hits_day','hits_week','hits_month','up','down','level','rnd','in'])) {
  353. $by = 'time';
  354. }
  355. if(!in_array($order, ['asc', 'desc'])) {
  356. $order = 'desc';
  357. }
  358. $where_cache = $where;
  359. if(!empty($randi)){
  360. unset($where_cache['website_id']);
  361. $where_cache['order'] = 'rnd';
  362. }
  363. if($by=='in' && !empty($name) ){
  364. $order = ' find_in_set(website_name, \''.$name.'\' ) ';
  365. }
  366. else{
  367. if($by=='in' && empty($name) ){
  368. $by = 'time';
  369. }
  370. $order= 'website_'.$by .' ' . $order;
  371. }
  372. $cach_name = $GLOBALS['config']['app']['cache_flag']. '_' .md5('website_listcache_'.http_build_query($where_cache).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl);
  373. $res = Cache::get($cach_name);
  374. if(empty($cachetime)){
  375. $cachetime = $GLOBALS['config']['app']['cache_time'];
  376. }
  377. if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
  378. $res = $this->listData($where,$order,$page,$num,$start,'*',1,$totalshow);
  379. if($GLOBALS['config']['app']['cache_core']==1){
  380. Cache::set($cach_name, $res, $cachetime);
  381. }
  382. }
  383. $res['pageurl'] = $pageurl;
  384. $res['half'] = $half;
  385. return $res;
  386. }
  387. public function infoData($where,$field='*',$cache=0)
  388. {
  389. if(empty($where) || !is_array($where)){
  390. return ['code'=>1001,'msg'=>'参数错误'];
  391. }
  392. $data_cache = false;
  393. $key = $GLOBALS['config']['app']['cache_flag']. '_'.'website_detail_'.$where['website_id'][1].'_'.$where['website_en'][1];
  394. if($where['website_id'][0]=='eq' || $where['website_en'][0]=='eq'){
  395. $data_cache = true;
  396. }
  397. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache) {
  398. $info = Cache::get($key);
  399. }
  400. if($GLOBALS['config']['app']['cache_core']==0 || $cache==0 || empty($info['website_id'])) {
  401. $info = $this->field($field)->where($where)->find();
  402. if (empty($info)) {
  403. return ['code' => 1002, 'msg' => '获取数据失败'];
  404. }
  405. $info = $info->toArray();
  406. //分类
  407. if (!empty($info['type_id'])) {
  408. $type_list = model('Type')->getCache('type_list');
  409. $info['type'] = $type_list[$info['type_id']];
  410. $info['type_1'] = $type_list[$info['type']['type_pid']];
  411. }
  412. if($GLOBALS['config']['app']['cache_core']==1 && $data_cache && $cache==1) {
  413. Cache::set($key, $info);
  414. }
  415. }
  416. return ['code'=>1,'msg'=>'获取成功','info'=>$info];
  417. }
  418. public function saveData($data)
  419. {
  420. $validate = \think\Loader::validate('Website');
  421. if(!$validate->check($data)){
  422. return ['code'=>1001,'msg'=>'参数错误:'.$validate->getError() ];
  423. }
  424. $key = 'website_detail_'.$data['website_id'];
  425. Cache::rm($key);
  426. $key = 'website_detail_'.$data['website_en'];
  427. Cache::rm($key);
  428. $key = 'website_detail_'.$data['website_id'].'_'.$data['website_en'];
  429. Cache::rm($key);
  430. $type_list = model('Type')->getCache('type_list');
  431. $type_info = $type_list[$data['type_id']];
  432. $data['type_id_1'] = $type_info['type_pid'];
  433. if(empty($data['website_en'])){
  434. $data['website_en'] = Pinyin::get($data['website_name']);
  435. }
  436. if(empty($data['website_letter'])){
  437. $data['website_letter'] = strtoupper(substr($data['website_en'],0,1));
  438. }
  439. if($data['uptime']==1){
  440. $data['website_time'] = time();
  441. }
  442. if($data['uptag']==1){
  443. $data['website_tag'] = mac_get_tag($data['website_name'], $data['website_content']);
  444. }
  445. unset($data['uptime']);
  446. unset($data['uptag']);
  447. if(!empty($data['website_id'])){
  448. $where=[];
  449. $where['website_id'] = ['eq',$data['website_id']];
  450. $res = $this->allowField(true)->where($where)->update($data);
  451. }
  452. else{
  453. $data['website_time_add'] = time();
  454. $data['website_time'] = time();
  455. $res = $this->allowField(true)->insert($data);
  456. }
  457. if(false === $res){
  458. return ['code'=>1002,'msg'=>'保存失败:'.$this->getError() ];
  459. }
  460. return ['code'=>1,'msg'=>'保存成功'];
  461. }
  462. public function delData($where)
  463. {
  464. $list = $this->listData($where,'',1,9999);
  465. if($list['code'] !==1){
  466. return ['code'=>1001,'msg'=>'删除失败:'.$this->getError() ];
  467. }
  468. $path = './';
  469. foreach($list['list'] as $k=>$v){
  470. $pic = $path.$v['website_pic'];
  471. if(file_exists($pic) && (substr($pic,0,8) == "./upload") || count( explode("./",$pic) ) ==1){
  472. unlink($pic);
  473. }
  474. if($GLOBALS['config']['view']['website_detail'] ==2 ){
  475. $lnk = mac_url_website_detail($v);
  476. $lnk = reset_html_filename($lnk);
  477. if(file_exists($lnk)){
  478. unlink($lnk);
  479. }
  480. }
  481. }
  482. $res = $this->where($where)->delete();
  483. if($res===false){
  484. return ['code'=>1001,'msg'=>'删除失败:'.$this->getError() ];
  485. }
  486. return ['code'=>1,'msg'=>'删除成功'];
  487. }
  488. public function fieldData($where,$update)
  489. {
  490. if(!is_array($update)){
  491. return ['code'=>1001,'msg'=>'参数错误'];
  492. }
  493. $res = $this->allowField(true)->where($where)->update($update);
  494. if($res===false){
  495. return ['code'=>1001,'msg'=>'设置失败:'.$this->getError() ];
  496. }
  497. $list = $this->field('website_id,website_name,website_en')->where($where)->select();
  498. foreach($list as $k=>$v){
  499. $key = 'website_detail_'.$v['website_id'];
  500. Cache::rm($key);
  501. $key = 'website_detail_'.$v['website_en'];
  502. Cache::rm($key);
  503. }
  504. return ['code'=>1,'msg'=>'设置成功'];
  505. }
  506. public function updateToday($flag='art')
  507. {
  508. $today = strtotime(date('Y-m-d'));
  509. $where = [];
  510. $where['website_time'] = ['gt',$today];
  511. if($flag=='type'){
  512. $ids = $this->where($where)->column('type_id');
  513. }
  514. else{
  515. $ids = $this->where($where)->column('website_id');
  516. }
  517. if(empty($ids)){
  518. $ids = [];
  519. }else{
  520. $ids = array_unique($ids);
  521. }
  522. return ['code'=>1,'msg'=>'获取成功','data'=> join(',',$ids) ];
  523. }
  524. public function visit($param)
  525. {
  526. $ip = sprintf('%u', ip2long(request()->ip()));
  527. if ($ip > 2147483647) {
  528. $ip = 0;
  529. }
  530. $max_cc = $GLOBALS['config']['website']['refer_visit_num'];
  531. if(empty($max_cc)){
  532. $max_cc=1;
  533. }
  534. $todayunix = strtotime("today");
  535. $where = [];
  536. $where['user_id'] = 0;
  537. $where['visit_ip'] = $ip;
  538. $where['visit_time'] = ['gt', $todayunix];
  539. $cc = model('visit')->where($where)->count();
  540. if ($cc>= $max_cc){
  541. return ['code' => 102, 'msg' => '每日仅能'.$max_cc.'次来路记录'];
  542. }
  543. $data = [];
  544. $data['user_id'] = 0;
  545. $data['visit_ip'] = $ip;
  546. $data['visit_time'] = time();
  547. $data['visit_ly'] = htmlspecialchars($param['url']);
  548. $res = model('visit')->saveData($data);
  549. if ($res['code'] > 1) {
  550. return ['code' => 103, 'msg' => '来路记录失败,请重试'];
  551. }
  552. return ['code'=>1,'msg'=>'来路记录成功'];
  553. }
  554. }