源代码 

load->model('api_model');
  }
  
	public function index()
	{
		$this->load->view('api/index');
	}
	
	/*
  功能:专家注册
  方法:POST /api/expertReg
  参数:username,email,password
  返回类型:json
  返回内容:{"status":["0","1"],"usernameError":["0","1"],"emailError":["0","1"],"passwordError":["0","1"],"usernameUnavailable":["0","1"]}
  */
	public function expertReg()
	{
		header('Access-Control-Allow-Origin: *'); 
	  $invalid=0;
	  
	  $this->load->helper('form');
	  $this->load->library('form_validation');
	  
	  $this->form_validation->set_rules('username', '专家名称', 'required|trim|xss_clean|max_length[40]');
	  $this->form_validation->set_rules('email', '邮箱', 'required|trim|xss_clean');
	  $this->form_validation->set_rules('password', '密码', 'required');
	  
		if (strlen(trim($this->input->post('username')))==0){$data['usernameError']='1';$invalid=1;}else{$data['usernameError']='0';}
	  if ( (strlen(trim($this->input->post('email')))==0) || (strpos(trim($this->input->post('email')),"@")==false) ){$data['emailError']='1';$invalid=1;}else{$data['emailError']='0';}
	  if (strlen(trim($this->input->post('password')))==0){$data['passwordError']='1';$invalid=1;}else{$data['passwordError']='0';}
		if ($this->form_validation->run() === FALSE or $invalid===1)
	  {
	    $data['status']='1'; //error:invalid
	  }
	  else
	  {
	    $data['status']='0'; //success
			$r=$this->api_model->expertReg(trim($this->input->post('username')),trim($this->input->post('email')),trim($this->input->post('password')));
			if ($r==false)
			{
				$data['usernameUnavailable']='1';
				$data['status']='1';
				$invalid=1;
			}
			else
			{
				$data['usernameUnavailable']='0';
			}
	  }
	  $this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:用户注册
  方法:POST /api/userReg
  参数:username,email,password
  返回类型:json
  返回内容:{"status":["0","1"],"usernameError":["0","1"],"emailError":["0","1"],"passwordError":["0","1"],"usernameUnavailable":["0","1"]}
  */
	public function userReg()
	{
		header('Access-Control-Allow-Origin: *'); 
	  $invalid=0;
	  
	  $this->load->helper('form');
	  $this->load->library('form_validation');
	  
	  $this->form_validation->set_rules('username', '用户名称', 'required|trim|xss_clean|max_length[40]');
	  $this->form_validation->set_rules('email', '邮箱', 'required|trim|xss_clean');
	  $this->form_validation->set_rules('password', '密码', 'required');
	  
		if (strlen(trim($this->input->post('username')))==0){$data['usernameError']='1';$invalid=1;}else{$data['usernameError']='0';}
	  if ( (strlen(trim($this->input->post('email')))==0) || (strpos(trim($this->input->post('email')),"@")==false) ){$data['emailError']='1';$invalid=1;}else{$data['emailError']='0';}
	  if (strlen(trim($this->input->post('password')))==0){$data['passwordError']='1';$invalid=1;}else{$data['passwordError']='0';}
		if ($this->form_validation->run() === FALSE or $invalid===1)
	  {
	    $data['status']='1'; //error:invalid
	  }
	  else
	  {
	    $data['status']='0'; //success
			$r=$this->api_model->userReg(trim($this->input->post('username')),trim($this->input->post('email')),trim($this->input->post('password')));
			if ($r===false)
			{
				$data['usernameUnavailable']='1';
				$data['status']='1';
				$invalid=1;
			}
			else if ($r===true)
			{
				$data['usernameUnavailable']='0';
			}
	  }
	  $this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
	功能:专家登录
	方法:POST /api/expertLogin
	参数:username,password
	返回类型:json
	返回内容:{"status":["0","1"]}
	*/
	public function expertLogin()
	{
		header('Access-Control-Allow-Origin: *'); 
		$username=$this->input->post('username');
		$password=$this->input->post('password');
		if($this->api_model->checkExpertPassword($username,$password)==true)
		{
			$data['status']="0";
			$this->session->set_userdata(array(
				'expertname' => $username
				));
		}
		else
		{
			$data['status']="1";
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
	功能:用户登录
	方法:POST /api/userLogin
	参数:username,password
	返回类型:json
	返回内容:{"status":["0","1"]}
	*/
	public function userLogin()
	{
		header('Access-Control-Allow-Origin: *'); 
		$username=$this->input->post('username');
		$password=$this->input->post('password');
		if($this->api_model->checkUserPassword($username,$password)==true)
		{
			$data['status']="0";
			$this->session->set_userdata(array(
				'username' => $username
				));
		}
		else
		{
			$data['status']="1";
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
	功能:检查用户是否已登录
	方法:GET /api/checkUserLoggedIn
	参数:
	返回类型:json
	返回内容:{"state":["0","1"]}
	*/
	public function checkUserLoggedIn()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
			$data['state']=1;
		}
		else if ($this->api_model->checkUserLoggedIn()===false)
		{
			$data['state']=0;
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
	功能:检查专家是否已登录
	方法:GET /api/checkExpertLoggedIn
	参数:
	返回类型:json
	返回内容:{"state":["0","1"]}
	*/
	public function checkExpertLoggedIn()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkExpertLoggedIn()===true)
		{
			$data['state']=1;
		}
		else if ($this->api_model->checkExpertLoggedIn()===false)
		{
			$data['state']=0;
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:用户提问题
  方法:POST /api/userAddQuestion
  参数:content
  返回类型:json
  返回内容:{"status":["0","1"],"contentError":["0","1"],"loginError":["0","1"],"questionId":#id}
  */
	public function userAddQuestion()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
		  $invalid=0;
		  
		  $this->load->helper('form');
		  $this->load->library('form_validation');
		  
		  $this->form_validation->set_rules('content', '问题内容', 'required|trim|xss_clean');
		  
			if (strlen(trim($this->input->post('content')))==0){$data['contentError']='1';$invalid=1;}else{$data['contentError']='0';}
			if ($this->form_validation->run() === FALSE or $invalid===1)
		  {
		    $data['status']='1'; //error:invalid
		  }
		  else
		  {
		    $data['status']='0'; //success
				$this->api_model->userAddQuestion(trim($this->input->post('content')));
				$this->db->order_by('id','desc');
				$data['id']=$this->db->get('questions')->result()[0]->id;
		  }
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:专家创建课程
  方法:POST /api/expertAddCourse
  参数:coursename
  返回类型:json
  返回内容:{"status":["0","1"],"coursenameError":["0","1"],"coursenameUnavailable":["0","1"],"loginError":["0","1"]}
  */
	public function expertAddCourse()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkExpertLoggedIn()===true)
		{
			$invalid=0;
		  
		  $this->load->helper('form');
		  $this->load->library('form_validation');
		  
		  $this->form_validation->set_rules('coursename', '课程名称', 'required|trim|xss_clean|max_length[128]');
		  
			if (strlen(trim($this->input->post('coursename')))==0){$data['coursenameError']='1';$invalid=1;}else{$data['coursenameError']='0';}
			if ($this->form_validation->run() === FALSE or $invalid===1)
		  {
		    $data['status']='1'; //error:invalid
		  }
		  else
		  {
		    $data['status']='0'; //success
				$r=$this->api_model->expertAddCourse(trim($this->input->post('coursename')),$this->session->userdata('expertname'));
				if ($r===false)
				{
					$data['coursenameUnavailable']='1';
					$data['status']='1';
					$invalid=1;
				}
				else if ($r===true)
				{
					$data['coursenameUnavailable']='0';
				}
		  }
		  $this->load->view('api/status',array("result" => json_encode($data)));
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:用户将问题推送到课程
  方法:POST /api/userPushQuestion
  参数:coursename,questionid
  返回类型:json
  返回内容:{"status":["0","1"],"coursenameError":["0","1"],"questionidError":["0","1"],"loginError":["0","1"]}
  */
  public function userPushQuestion()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
		  $invalid=0;
		  
		  $this->load->helper('form');
		  $this->load->library('form_validation');
		  
		  $this->form_validation->set_rules('coursename', '课程名称', 'required');
		  $this->form_validation->set_rules('questionid', '问题id', 'required');
			if (strlen(trim($this->input->post('coursename')))==0){$data['contentError']='1';$invalid=1;}else{$data['contentError']='0';}
			if (strlen(trim($this->input->post('questionid')))==0){$data['questionidError']='1';$invalid=1;}else{$data['questionidError']='0';}
			if ($this->form_validation->run() === FALSE or $invalid===1)
		  {
		    $data['status']='1'; //error:invalid
		  }
		  else
		  {
		    $data['status']='0'; //success
				$this->api_model->userPushQuestion($this->input->post('coursename'),$this->input->post('questionid'));
		  }
		  $this->load->view('api/status',array("result" => json_encode($data)));
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:专家回答问题
  方法:POST /api/expertAddAnswer
  参数:coursename,questionid,content
  返回类型:json
  返回内容:{"status":["0","1"],"coursenameError":["0","1"],"questionidError":["0","1"],"contentError":["0","1"],"permissionError":["0","1"],"loginError":["0","1"]}
  */
  public function expertAddAnswer()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkExpertLoggedIn()===true)
		{
			if ($this->api_model->checkExpertCourse(trim($this->input->post('coursename')),$this->session->userdata('expertname'))==true)
			{
			  $invalid=0;
			  
			  $this->load->helper('form');
			  $this->load->library('form_validation');
			  
			  $this->form_validation->set_rules('coursename', '课程名称', 'required');
			  $this->form_validation->set_rules('questionid', '问题id', 'required');
			  $this->form_validation->set_rules('content', '回答内容', 'required|trim|xss_clean');
			  
				if (strlen(trim($this->input->post('coursename')))==0){$data['coursenameError']='1';$invalid=1;}else{$data['coursenameError']='0';}
				if (strlen(trim($this->input->post('questionid')))==0){$data['questionidError']='1';$invalid=1;}else{$data['questionidError']='0';}
				if (strlen(trim($this->input->post('content')))==0){$data['contentError']='1';$invalid=1;}else{$data['contentError']='0';}
				
				if ($this->form_validation->run() === FALSE or $invalid===1)
			  {
			    $data['status']='1'; //error:invalid
			  }
			  else
			  {
			    $data['status']='0'; //success
					$this->api_model->expertAddAnswer($this->input->post('coursename'),$this->input->post('questionid'),trim($this->input->post('content')));
			  }
			  $this->load->view('api/status',array("result" => json_encode($data)));
			}
			else
			{
				$data['permissionError']='1';
				$data['status']='1';
			}
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
  /*
  功能:用户回答问题
  方法:POST /api/userAddAnswer
  参数:coursename,questionid,username,content
  返回类型:json
  返回内容:{"status":["0","1"],"coursenameError":["0","1"],"questionidError":["0","1"],"usernameError":["0","1"],"contentError":["0","1"],"loginError":["0","1"]}
  */
  
  public function userAddAnswer()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
		  $invalid=0;
		  
		  $this->load->helper('form');
		  $this->load->library('form_validation');
		  
		  $this->form_validation->set_rules('coursename', '课程名称', 'required');
		  $this->form_validation->set_rules('questionid', '问题id', 'required');
		  $this->form_validation->set_rules('content', '回答内容', 'required|trim|xss_clean');
		  
			if (strlen(trim($this->input->post('coursename')))==0){$data['coursenameError']='1';$invalid=1;}else{$data['coursenameError']='0';}
			if (strlen(trim($this->input->post('questionid')))==0){$data['questionidError']='1';$invalid=1;}else{$data['questionidError']='0';}
			if (strlen(trim($this->input->post('content')))==0){$data['contentError']='1';$invalid=1;}else{$data['contentError']='0';}
			
			if ($this->form_validation->run() === FALSE or $invalid===1)
		  {
		    $data['status']='1'; //error:invalid
		  }
		  else
		  {
		    $data['status']='0'; //success
				$this->api_model->userAddAnswer($this->input->post('coursename'),$this->input->post('questionid'),$this->session->userdata('username'),trim($this->input->post('content')));
		  }
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
  /*
  功能:专家获取课程列表
  方法:GET /api/expertGetCourses
  参数:
  返回类型:json
  返回内容:{"courseid":"coursename"}
  */
  
  public function expertGetCourses()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkExpertLoggedIn()===true)
		{
			$this->api_model->expertGetCourses($this->session->userdata('expertname'));
		}
	}
	
  /*
  功能:显示所有课程
  方法:GET /api/getCourses
  参数:
  返回类型:json
  返回内容:{"courseid":"coursename"}
  */
  
  public function getCourses()
	{
		$this->api_model->getCourses();
	}
  
  /*
  功能:用户切换"同问"状态
  方法:POST /api/userToggleVoteQuestion
  参数:coursename,questionid
  返回类型:json
  返回内容:{"status":["0","1"],"coursenameError":["0","1"],"questionidError":["0","1"],"loginError":["0","1"]}
  */
  
  public function userToggleVoteQuestion()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
			$invalid=0;
			$this->form_validation->set_rules('coursename', '课程名称', 'required');
		  $this->form_validation->set_rules('questionid', '问题id', 'required');
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		if (strlen(trim($this->input->post('coursename')))==0){$data['coursenameError']='1';$invalid=1;}else{$data['coursenameError']='0';}
		if (strlen(trim($this->input->post('questionid')))==0){$data['questionidError']='1';$invalid=1;}else{$data['questionidError']='0';}
		
		if ($this->form_validation->run() === FALSE or $invalid===1)
	  {
	    $data['status']='1'; //error:invalid
	  }
	  else
	  {
	    $data['status']='0'; //success
			$this->api_model->userToggleVoteQuestion($this->input->post('coursename'),$this->input->post('questionid'),$this->session->userdata('username'));
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
	
	/*
  功能:用户检查"同问"状态
  方法:POST /api/userCheckVoteQuestion
  参数:coursename,questionid
  返回类型:json
  返回内容:{"status":["0","1"],"state":["0","1"]}
  */
  
  public function userCheckVoteQuestion()
	{
		header('Access-Control-Allow-Origin: *'); 
		if ($this->api_model->checkUserLoggedIn()===true)
		{
			$invalid=0;
			$this->form_validation->set_rules('coursename', '课程名称', 'required');
		  $this->form_validation->set_rules('questionid', '问题id', 'required');
		}
		else
		{
			$data['loginError']='1';
			$data['status']='1';
		}
		if (strlen(trim($this->input->post('coursename')))==0){$data['coursenameError']='1';$invalid=1;}else{$data['coursenameError']='0';}
		if (strlen(trim($this->input->post('questionid')))==0){$data['questionidError']='1';$invalid=1;}else{$data['questionidError']='0';}
		
		if ($this->form_validation->run() === FALSE or $invalid===1)
	  {
	    $data['status']='1'; //error:invalid
	  }
	  else
	  {
	    $data['status']='0'; //success
			$data['state']=$this->api_model->userCheckVoteQuestion($this->input->post('coursename'),$this->input->post('questionid'),$this->session->userdata('username'));
		}
		$this->load->view('api/status',array("result" => json_encode($data)));
	}
  
  /*
  功能:显示课程待回答问题 (按投票数从多到少排列)
  方法:POST /api/getCourseUnansweredQuestions
  参数:coursename,number
  返回类型:json
  返回内容:{i:"content"}
  */
  
  public function getCourseUnansweredQuestions()
	{
		header('Access-Control-Allow-Origin: *'); 
		$this->api_model->getCourseUnansweredQuestions($this->input->post('coursename'),$this->input->post('number'));
	}
	
  /*
  功能:显示课程已回答的问题及答案 (按投票数从多到少排列)
  方法:POST /api/getCourseAnsweredQuestions
  参数:coursename,number
  返回类型:json
  返回内容:{i:{"question":"content",j:"answerj"}}
  */
  
  public function getCourseAnsweredQuestions()
	{
		header('Access-Control-Allow-Origin: *'); 
		$this->api_model->getCourseAnsweredQuestions();
	}
  
}