通过之前的学习,本文主要介绍一下实现用户的登录、注册等功能的接口代码,让大家通过小实战来感悟phalapi框架的神奇之处。(以下代码均可右滑)
/**
* 用户注册
* @desc 若用户名存在,throw -1
*/
public function userRegister() {
$Domain_User = new Domain_User();
//检查username是否存在
$Domain_User->userCheck($this->username);
return $Domain_User->userAdd($this);
}
/**
* @param $data
* 用户注册
* @return int
* @throws BadRequestException
*/
public function userAdd($data){
$Model_User = new Model_User();
$uid = $Model_User->userAdd($data);
if (!$uid){
throw new BadRequestException('error userAdd',-1);
}
return (int)$uid['id'];
}
/**
* @param $data
* 用户注册
* @return $result
*/
public function userAdd($data){
return $result = $this->getORM()->insert(array(
'username' => $data->username,
'password' => $data->password,
'phone' => $data->phone,
'create_date' => time()
));
}
/**
* 用户登录
* @desc 返回用户id
*/
public function userLogin() {
$Domain_User = new Domain_User();
$Domain_User->userCheck($this->username);
return $Domain_User->userLogin($this);
}
/**
* @param $data
* 用户登录
* @return $id
* @throws BadRequestException
*/
public function userLogin($data){
$Model_User = new Model_User();
$id = $Model_User->userLogin($data);
if (!$id){
throw new BadRequestException('error username or password wrong',-1);
}
return $id;
}
/**
* @param $data
* 用户登录
*/
public function userLogin($data){
return $this->getORM()->select('id')->where(array('username'=>$data->username))->where(array('password'=>$data->password))->fetch();
}
/**
*获取用户信息
* @desc 返回用户id信息
* @return mixed
*/
public function getInfo() {
$Domain_User = new Domain_User();
return $Domain_User->getInfo($this->id);
}
/**
* @param $id
* 根据id获取用户信息
* @return $user
* @throws BadRequestException
*/
public function getInfo($id){
$Model_User = new Model_User();
$user = $Model_User->getInfo($id);
if (!$user){
throw new BadRequestException('error id wrong',-1);
}
return $user;
}
/**
* @param $id
* @根据id获取用户信息
*/
public function getInfo($id){
//$id=(int)$id;
return $this->getORM()->where(array('id'=>$id))->fetch();
}
public function getUserList(){
return $this->getORM()->select('*');
}
在这里,小编对代码进行了部分注释,希望对大家的学习有所帮助,也希望有问题加我微信(wzc88czw)交流学习心得。