<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace news\home\controller;
use \temmoku\controller;
use \temmoku\db;
use \temmoku\lib\pages;
use \news\init;
use \temmoku\lib\custom_model;
use \news\public_class\article;
class articles extends Controller
{

    public function articles()
    {
    	!isset($_GET['aid']) && $this->err('缺少必要参数');
    	!isset($_GET['page']) && $_GET['page']=1;
    	$floor=floor($_GET['aid']/1000);
    	$cache_dir=RUNTIME_PATH."/cache/".MODULE.'/'.DEVICE."/content/{$floor}/{$_GET[aid]}_{$_GET[page]}.txt";
    	if(C(MODULE.'|setting.content_cache_time') && is_file($cache_dir) && time()-@filemtime($cache_dir) < C(MODULE.'|setting.content_cache_time')*3600){
    		echo get_cache($cache_dir);
    		exit;
    	}
    	
    	$data=db::select('*')->from(M_jab.'article AS A')->innerJoin(M_jab.'content AS B','A.aid = B.aid')->where('A.aid='.$_GET['aid'])->orderByDESC(['A.aid'])->setPaging('1')->page($_GET['page'])->row();
		if(!$data){
    		$this->err('不存在的数据');
   		}
   		db::query("UPDATE `".M_jab."article` SET `clicks` = `clicks`+1 WHERE aid=$data[aid]");
   		//判断是不是模型
   		if($data['mid']){
   			$Custom_Model=db::select('*')->from(M_jab.'content_'.$data['mid'])->where("aid= '$data[aid]' ")->row();
   			$setting=C(MODULE.'|WN_MODELDB.'.$data['mid'])['field'];
   			$_data=(new Custom_Model)->show($setting,$Custom_Model);
   			$data=array_merge((array)$data,(array)$_data);
   		}
   		
   		$CATEGORY_DB=C(MODULE.'|CATEGORY_SETTING.'.$data['cid']);
   		$CATEGORY_SETTING=unserialize($CATEGORY_DB['setting']);

   		if($CATEGORY_SETTING['tpl']['article']){
   			$tpl=$CATEGORY_SETTING['tpl']['article'];
   		}else{
   			if($data['mid']){
   				$tpl="articles_".$data['mid'];
   			}else{
   				$tpl="articles";
   			}
   		}
   		//判断是否有浏览权限
   		if($CATEGORY_SETTING['look'] && C('mydb.groupid') != '3'){
   			if(!in_array(C('mydb.groupid'),$CATEGORY_SETTING['look'])){
   				$this->err('没有权限');
   			}
   		}
   		
   		//判断是否出售
   		if($data['sell_jifen'] && C('mydb.groupid') != '3'){
   			if($_POST['step']=='payment'){
   				if(!C('MYDB')){
   					$this->err('请登陆');
   				}
   				if($data['sell_jifen'] > C('mydb.jifen')){
   					$this->err('你的积分不足以支付此篇文章');
   				}
   				//减少购买者积分
   				(new \temmoku\lib\integral)->add(C('mydb.uid'),-$data['sell_jifen'],"购买文章所付积分$data[sell_jifen]文章地址：".U(['aid'=>$data['aid'],'cid'=>$data['cid']],MODULE));
   				//增加出售者积分
   				(new \temmoku\lib\integral)->add($data['uid'],$data['sell_jifen'],"出售文章所赚积分$data[sell_jifen]文章地址：".U(['aid'=>$data['aid'],'cid'=>$data['cid']],MODULE));
   				
   				$arr=[
   					'aid'=>$data['aid'],
					'uid'=>C('mydb.uid'),
					'username'=>C('mydb.username'),
					'integral'=>$integral,
					'posttime'=>NOWTIME
				];
				db::insert(M_jab.'buyarticle')->cols($arr)->query();
   				$this->success('支付成功');
   			}
   			$_data=false;
   			if(C('mydb')){
   				$_data=db::select('*')->from(M_jab.'buyarticle')->where("aid = '$data[aid]' AND uid=".C('mydb.uid'))->row();
   			}
   			
   			if(!$_data){
   				$data['content']=(new article)->sell_jifen($data['sell_jifen']);
   			}
   		}
   		
   		//判断是否加密
   		if($data['password'] && C('mydb.groupid') != '3'){
   			if($_POST['step']=='pass'){
   				if(md5($_POST['password']) != $data['password']){
   					$data['content']=(new article)->pass('密码不正确，请重新输入');
   				}else{
   					(new \temmoku\lib\cookie)->set('article_'.$data['aid'],$data['password'],'7');
   				}
   			}else{
   				$article=(new \temmoku\lib\cookie)->get('article_'.$data['aid']);
   				if(!$article){
   					$data['content']=(new article)->pass();
   				}
   			}
   		}
   		
    	$Get_Page=(new Pages)->Pages(M_jab.'content','aid='.$_GET['aid'],'','1',$_GET['page']);
    	$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return U(['aid'=>$_GET['aid'],'cid'=>$_GET['cid'],'page'=>$matches[1]],MODULE);}, $Get_Page );
    	//title标签
    	
    	$CATEGORY_NAME=empty($CATEGORY_DB['seo_name']) ? $CATEGORY_DB['name'] :$CATEGORY_DB['seo_name'];
    	$title=empty($data['seotitle']) ? $data['title'] :$data['seotitle'];
    	$TITLE=$title.'_'.$CATEGORY_NAME.'_'.C('WEBNAME');
    	//关键字标签
    	$_keyword=empty($data['keyword']) ? C('WEB_SEO_KEYWORDS') :$data['keyword'];
    	if($_keyword){
    		$keyword=implode(',',$_keyword=explode(' ' ,$_keyword));
    		$data['keyword']='';
    		foreach($_keyword AS $__keyword){
    			$data['keyword'] .='<a href="'.HTTP.init::$domain.'/search/index/keyword/'.$__keyword.C('HTML').'">'.$__keyword.'</a>';
    		}
    	} 
    	//描述标签
    	$DESCRIPTION=$data['description'];
    	$data['thumb']=get_img_url($data['thumb']);
    	$data['content']=htmlspecialchars_decode(str_replace  ( 'WWW_TEMMOKU_CN'  , WEBURL.'/'.C('upfiles_Catalog') ,  $data['content']));
    	//最新新闻
    	$data['posttime']=date("Y-m-d H:i:s",$data['posttime']);
		$Crumbs = preg_replace_callback('/cid=([0-9]+)/',function ($matches) {return U(['cid'=>$matches[1]],MODULE);}, C(MODULE.'|WN_CRUMBS.'.$data['cid']) );
    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$keyword);
    	$this->assign('DESCRIPTION',$DESCRIPTION);
    	$this->assign('data',$data);
    	$this->assign('Get_Page',$Get_Page);
    	$this->assign('Crumbs',$Crumbs);
		//不是pc设备才去获取
		if(DEVICE !='pc'){
			if(DEVICE=='wechat'){//如果是微信打开
				$wechat_share=(new \plugin\wechat\public_class\wechat)->getticket(U([aid=>$data[aid],cid=>$data[cid]]));
				$this->assign('wechat_share',$wechat_share);
			}
$wechat_share=(new \plugin\wechat\public_class\wechat)->getticket(U([aid=>$data[aid],cid=>$data[cid]]));
				$this->assign('wechat_share',$wechat_share);
				
				
			$menu_more_list=db::select('*')->from(M_jab.'category')->where("upcid=$CATEGORY_DB[upcid]")->query();
			$this->assign('menu_more_list',$menu_more_list);
			$this->assign('webname',$CATEGORY_DB['name']);
		}
		
		$this->display(tpl($tpl));
		
        if(C(MODULE.'|setting.content_cache_time') && !$data['password'] && !$data['sell_jifen']){
    		$content=ob_get_contents();
    		$content .="<script type=\"text/javascript\" src=\"".M_WEBURL."/articles/content_update?aid=$data[aid]\" ></script>";
			write_file($cache_dir,$content);
		}
    }
    
    public function content_update(){
    	$data=db::select('*')->from(M_jab.'article')->where("aid= '$_GET[aid]'")->row();
    	if($data){
    		db::query("UPDATE `".M_jab."article` SET `clicks` = `clicks`+1 WHERE aid=$data[aid]");
    		$clicks=$data['clicks']+1;
    		echo "$('#clicks').html($clicks);$('#comment_num').html($data[comment_num]);allnum='$data[comment_num]'";
    	}
    }  
}
?>
