示例:mipcms文章 (MipcmsDemoSkycaiji)

  更新日期:2018-08-02 21:14
实现mipcms文章入库功能,您可参考该代码开发更多mipcms应用

应用代码

<?php
/**
 * 示例:mipcms
 * 您可参考代码自行开发mipcms的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class MipcmsDemoSkycaijiCms extends BaseCms{
	public $siteurl;//cms网址
	public function init_extend(){
		$siteurl=$this->db()->table('__SETTINGS__')->where("`key`='articleDomain'")->find();
		$this->siteurl=$siteurl['val'];
	}
	public function cms_db_mipcms($cmsPath){
		$dbFile=realpath($cmsPath.'/app/database.php');
		//转换成thinkphp数据库配置
		$config=include $dbFile;
		
		$cmsDb=array(
			'db_type'  => $config['type'],
			'db_user'  => $config['username'],
			'db_pwd'   => $config['password'],
			'db_host'  => $config['hostname'],
			'db_port'  => $config['hostport'],
			'db_name'  => $config['database'],
			'db_charset'  => $config['charset'],
			'db_prefix'  => $config['prefix']
		);
		
		return $cmsDb;
	}
	//参数
	public $_params=array(
		'author' => array (
			'name' => '作者账号',
			'tag' => 'text',
			'require'=>1,
		),
		'category' => array (
			'name' => '分类',
			'tag' => 'select',
			'option' => 'function:param_option_category',
			'require'=>1,
		),
		'title' => array (
			'name' => '文章标题',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
		'content' => array (
			'name' => '文章内容',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
	);

	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$uid=$this->db()->table('__USERS__')->where(array('username'=>$params['author']))->find();
		if(!empty($uid)){
			$uid=$uid['uid'];
		}else{
			return array('id'=>0,'error'=>'用户不存在');
		}
		
		$uuid=$this->uuid();
		
		$newArticle=array(
	        'uuid'=>$uuid,
	        'cid'=>$params['category'],
	        'uid'=>$uid,
	        'title'=>$params['title'],
	        'views'=>0,
	        'create_time'=>0,
	        'edit_time'=>0,
	        'is_recommend'=>0,
	        'comments'=>0,
	        'version'=>0,
	        'publish_time'=>time(),
	        'collect'=>0,
	        'mip_post_num'=>0,
	        'content_id'=>$uuid,
	        'url_name'=>'',
	        'img_url'=>null,
	        'keywords'=>'',
	        'link_tags'=>'',
	        'description'=>'',
	        'site_id'=>null,
	        'mip_push_num'=>null,
	        'amp_push_num'=>null,
	        'xzh_push_num'=>null,
	        'link_push_num'=>null,
	        'yc_push_num'=>null,
	        'baidu_spider_num'=>null,
	        'google_spider_num'=>null,
	        'so_spider_num'=>null,
	        'sm_spider_num'=>null,
	        'sogou_spider_num'=>null,
	        'baidu_spider_time'=>null,
	        'google_spider_time'=>null,
	        'so_spider_time'=>null,
	        'sm_spider_time'=>null,
	        'sogou_spider_time'=>null,
		);
		
		$id=$this->db()->table('__ARTICLES__')->add($newArticle);//添加并返回id
		if($id>0){
			//添加内容
			$this->db()->table('__ARTICLES_CONTENT__')->add(array(
				'id'=>$uuid,
				'content'=>$params['content']
			));
			
			return array('id'=>$id,'target'=>$this->siteurl."index.php?s=/article/{$id}.html");
		}else{
			return array('id'=>0,'error'=>'添加文章失败');
		}
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catDb=$this->db()->table('__ARTICLES_CATEGORY__')->select();
		$catList=array();
		foreach ($catDb as $cat){
			$catList[$cat['id']]=auto_convert2utf8($cat['name']);
		}
		return $catList;
	}
	public function uuid(){
        $charid = strtolower(md5(uniqid(rand(), true)));
        return substr($charid,0,24);
    }
}
?>