示例:hadsky文章 (HadskyDemoSkycaiji)

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

应用代码

<?php
/**
 * 示例:hadsky发帖
 * 您可参考代码自行开发hadsky的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class HadskyDemoSkycaijiCms extends BaseCms{
	public function cms_db_hadsky($cmsPath){
		$dbFile=realpath($cmsPath.'/puyuetian/mysql/config.php');
		//转换成thinkphp数据库配置
		include $dbFile;
		$config=$_G['MYSQL'];
		if(preg_match('/set names (\w+)/i', $config['CHARSET'],$charset)){
			$config['CHARSET']=$charset[1];
		}else{
			$config['CHARSET']='utf8';
		}
		
		$cmsDb=array(
			'db_type'  => 'mysql',
			'db_user'  => $config['USERNAME'],
			'db_pwd'   => $config['PASSWORD'],
			'db_host'  => $config['LOCATION'],
			'db_port'  => 3306,
			'db_name'  => $config['DATABASE'],
			'db_charset'  => $config['CHARSET'],
			'db_prefix'  => $config['PREFIX']
		);
		
		return $cmsDb;
	}
	//参数
	public $_params=array(
		'author' => array (
			'name' => '作者账号',
			'tag' => 'text',
			'require'=>1,
		),
		'forum' => array (
			'name' => '版块ID',
			'tag' => 'select',
			'option' => 'function:param_option_forum',
			'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('__USER__')->where(array('username'=>$params['author']))->find();
		if(!empty($uid)){
			$uid=$uid['id'];
		}else{
			return array('id'=>0,'error'=>'用户不存在');
		}
		
		$newRead=array(
			'sortid'=>$params['forum'],
			'uid'=>$uid,
			'title'=>$params['title'],
			'content'=>$params['content'],
			'looknum'=>0,
			'zannum'=>0,
			'posttime'=>time(),
			'readlevel'=>0,
			'replyuid'=>$uid,
			'replycontent'=>null,
			'replytime'=>0,
			'replyip'=>null,
			'postip'=>'',
			'top'=>0,
			'high'=>0,
			'replyafterlook'=>0,
			'data'=>null,
			'del'=>0,
			'activetime'=>time(),
			'replyid'=>0,
			'fs'=>1,
			'label'=>''
		);
		
		$id=$this->db()->table('__READ__')->add($newRead);//添加主题并返回id
		if($id>0){
			return array('id'=>$id,'target'=>'文章:'.$id);
		}else{
			return array('id'=>0,'error'=>'添加文章失败');
		}
	}
	
	/*
	 * 参数选项:版块
	 * 必须返回键值对形式的数组
	 */
	public function param_option_forum(){
		$forumsDb=$this->db()->table('__READSORT__')->select();
		$forumList=array();
		foreach ($forumsDb as $forum){
			$forumList[$forum['id']]=auto_convert2utf8($forum['title']);
		}
		return $forumList;
	}
}
?>