WhatSNS问题采集 

类型
发布插件 » cms程序
标识
WhatsnsDnrwhatsnsDiannaorun
下载
36次
评论
1条
更新
2020-09-18
开发者
叶小枫

WhatSNS问答程序采集发布插件,对接最新的Whatsns程序。作者QQ:760361093

<?php
namespace plugin\release\cms;
class WhatsnsDnrwhatsnsDiannaorun extends BaseCms{
    
    public function cms_db_whatsns($cmsPath){
		$dbFile=realpath($cmsPath.'/application/config/database.php');
		//转换成thinkphp数据库配置
		define(BASEPATH, '/');
		include $dbFile;
	
		$db=$db?$db:array();
		$cmsDb=array(
			'db_type'  => 'mysql',
			'db_user'  => $db['default']['username'],
			'db_pwd'   => $db['default']['password'],
			'db_host'  => $db['default']['hostname'],
			'db_port'  => 3306,
			'db_name'  => $db['default']['database'],
			'db_charset'  => $db['default']['char_set'],
			'db_prefix'  => $db['default']['dbprefix']
		);
	
		return $cmsDb;
	}
	
	/*参数*/
	public $_params = array (
		'cateid' => array (
			'name' => '栏目',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_get_cateidlist',
		),
		'title' => array (
			'name' => '标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'uid' => array (
			'name' => '发布用户UID(不选表示随机)',
			'tag' => 'select',
			'option' => 'function:param_option_get_useridlist',
		),
		'answer' => array (
			'name' => '答案',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'auid' => array (
			'name' => '答题用户UID(不选表示随机)',
			'tag' => 'select',
			'option' => 'function:param_option_get_useridlist',
		),
		
		'dateline' => array (
			'name' => '回答延迟(s)',
			'require' => 1,
			'tag' => 'number',
		)
	);
	
	public function param_option_get_cateidlist(){
		/*必须返回键值对数组*/
		$forumDb=$this->db()->table('__CATEGORY__')->select();
		//读取论坛版块
		$forumList=array();
		foreach ($forumDb as $forum){
			$forumList[$forum['id']]=auto_convert2utf8($forum['name']);//自动转码
		}
		return $forumList;
	}
	
	public function param_option_get_useridlist(){
		/*必须返回键值对数组*/
			$forumDb=$this->db()->table('__USER__')->select();
		//读取论坛版块
		$forumList=array();
		foreach ($forumDb as $forum){
			$forumList[$forum['uid']]=auto_convert2utf8($forum['username']);//自动转码
		}
		return $forumList;
	}
	

	
	
	/*导入数据*/
	public function runImport($params){
		/*
		 * -----这里开始写代码-----
		 * 数据库操作:$this->db(),可参考thinkphp5的数据库操作
		 * 参数值列表:$params,$params[变量名] 调用参数的值
		 */
		
		$cateData=$this->db()->table('__CATEGORY__')->where('id',$params['cateid'])->find();
		if(empty($cateData)){
			return array('id'=>0,'error'=>$params['cateid'].'栏目不存在');//返回错误信息
		}
		
		$userData = [];
		$auserData = [];
		
		if(is_numeric($params['uid'])){
		    $userData=$this->db()->table('__USER__')->where('uid',$params['uid'])->find();
    		if(empty($userData)){
    			return array('id'=>0,'error'=>$params['uid'].'发布用户不存在');//返回错误信息
    		}
		}else{
		    $userDatas=$this->db()->table('__USER__')->where('groupid',7)->select();
		    $randuser = mt_rand(1,count($userDatas)-1);
		    $userData['uid'] = $userDatas[$randuser]['uid'];
		    $userData['username'] = $userDatas[$randuser]['username'];
		}
		
		if(is_numeric($params['auid'])){
		    $auserData=$this->db()->table('__USER__')->where('uid',$params['auid'])->find();
    		if(empty($auserData)){
    			return array('id'=>0,'error'=>$params['uid'].'答题用户不存在');//返回错误信息
    		}
		}else{
		    $userDatas=$this->db()->table('__USER__')->where('groupid',7)->select();
		    $randuser = mt_rand(1,count($userDatas)-1);
		    $auserData['uid'] = $userDatas[$randuser]['uid'];
		    $auserData['username'] = $userDatas[$randuser]['username'];
		}
		
		
		$error='';//错误信息
		$newThread=array(
			'cid'=>$params['cateid'],
			'cid1'=>$params['cateid'],
			'author'=>$userData['username'],
			'authorid'=>$userData['uid'],
			'title'=>$params['title'],
			'description'=>$params['content'],
			'time'=>time() - (int)$params['dateline'],
			'endtime'=>time()+51840000 - (int)$params['dateline'],
			'attentions'=>1,
			'views'=>1,
			'supply'=>'',
			'ip'=>'211.'.mt_rand(1,255).'.61.'.mt_rand(1,255),
            'relatebytag'=>$params['dateline'].' '.$tagsid
		);
		$qId=$this->db()->table('__QUESTION__')->insert($newThread,false,true);//返回的id
		$target=$this->siteurl.'q-'.$qId.'.html';
		if($qId>0){
		    $nData=array(
    			'qid'=>$qId,
    			'followerid'=>$userData['uid'],
    			'follower'=>$userData['username'],
    			'time'=>time() - (int)$params['dateline']
		    );
		    $this->db()->table('__QUESTION_ATTENTION__')->insert($nData,false,true);//返回的id
		}else{
		    return array('id'=>0,'error'=>'添加问题失败');//返回错误信息
		}
		$nData=array(
			'qid'=>$qId,
			'title'=>$params['title'],
			'author'=>$auserData['username'],
			'authorid'=>$auserData['uid'],
			'time'=>time(),
			'content'=>$params['answer'],
			'status'=>2,
			'ip'=>'211.'.mt_rand(1,255).'.61.'.mt_rand(1,255),

	    );
	    $res = $this->db()->table('__ANSWER__')->insert($nData,false,true);//返回的id
		if($res<1){
		    return array('id'=>0,'error'=>'添加答案失败');//返回错误信息
		}
		$this->db()->table('__QUESTION__')->where('id',$qId)->update(array(
					'answers'=>1,
				));


		/*
		 * 必须以数组形式返回:
		 * id(必填)表示入库返回的自增id或状态
		 * target(可选)记录入库的数据位置(发布的网址等)
		 * desc(可选)记录入库的数据位置附加信息
		 * error(可选)记录入库失败的错误信息
		 * 入库的信息可在“已采集数据”中查看
		 */
		return array('id'=>$qId,'target'=>$target,'desc'=>'','error'=>'');
	}
}
?>