在ThinkPHP3.1.2整合UCenter详解(三)这一章,实现了同步注册的功能,有了帐号我们就可以来做同步登录了。
打开UcService.class.php文件,添加一个UC登录和一个登出的方法,代码到UCenter接口开发手册的用户接口那里找用户登录示例代码,把它复制过来,然后稍微更改一下,使其带有返回值,以便下一步的操作,代码如下:
01 |
public function uc_login( $username , $password ){ |
02 |
list( $uid , $username , $password , $email ) = uc_user_login( $username , $password ); |
07 |
'username' => $username , |
08 |
'password' => $password , |
11 |
} elseif ( $uid == -1) { |
13 |
} elseif ( $uid == -2) { |
15 |
} elseif ( $uid == -3) { |
继续在用户接口那里找到同步登录的代码示例,找到“uc_user_synlogin($uid);”其作用是执行同步登录,然后写成uc_synlogin方法如下:
1 |
public function uc_synlogin( $uid ){ |
3 |
return uc_user_synlogin( $uid ); |
到这里UcService.class.php文件的登录方法已经写好,接下来打开前台W3note项目的控制器MemberAction.class.php文件写一个同步登录的方法,看代码:
01 |
public function checkLogin() { |
03 |
if (! $_POST [ 'username' ]) $this ->error( '帐号错误!' ); |
04 |
if (! $_POST [ 'password' ]) $this ->error( '密码错误!' ); |
05 |
if ( empty ( $_POST [ 'verify' ])) $this ->error( '验证码必须!' ); |
06 |
import( "@.ORG.UcService" ); |
07 |
$ucService = new UcService; |
08 |
$uidarray = $ucService ->uc_login( $_POST [ 'username' ], $_POST [ 'password' ]); |
10 |
$loginurl = $ucService ->uc_synlogin( $uidarray ); |
12 |
if (! is_string ( $uidarray )){ |
17 |
$map [ 'username' ] = $_POST [ 'username' ]; |
18 |
$map [ "status" ] = array ( 'gt' ,0); |
19 |
if ( $_SESSION [ 'verify' ] != md5( $_POST [ 'verify' ])) { |
20 |
$this ->error( '验证码错误!' ); |
23 |
$memberinfo = $this ->Member->where( $map )->find(); |
26 |
if (false === $memberinfo ) { |
27 |
$this ->error( '帐号不存在或已禁用!' ); |
28 |
} elseif ( $memberinfo [ 'status' ]==0){ |
29 |
$this ->error( '帐号已禁用!' ); |
32 |
$password = pwdHash( $_POST [ 'password' ]); |
33 |
if ( $memberinfo [ 'password' ] != $password ) { |
34 |
$this ->error( '密码错误!' ); |
36 |
session(C( 'USER_AUTH_KEY' ), $memberinfo [ 'id' ]); |
37 |
session( 'email' , $memberinfo [ 'email' ] ); |
38 |
session( 'loginUserName' , $memberinfo [ 'loginUserName' ]); |
39 |
session( 'lastLoginTime' , $memberinfo [ 'lastLoginTime' ]); |
40 |
session( 'loginnum' , $memberinfo [ 'loginnum' ]); |
41 |
session( 'lastloginip' , $memberinfo [ 'lastloginip' ]); |
45 |
$data [ 'id' ] = $memberinfo [ 'id' ]; |
46 |
$data [ 'lastlogintime' ] = time(); |
47 |
$data [ 'loginnum' ] = array ( 'exp' , 'loginnum+1' ); |
48 |
$data [ 'lastloginip' ] = get_client_ip(); |
50 |
$this ->Member->save( $data ); |
51 |
$this ->success( '登录成功!' ,U( 'Member/index' )); |
我们来看一下checkLogin()方法的执行过程。
在项目W3note注册的一个帐号,然后在项目W3note提交登录表单后,首先执行UCenter的登录,前面我们写了两个UCenter的登录方法,在调用之前需要使用“import("@.ORG.UcService");”把UcService.class.php文件加载进来,实例化后得到$ucService,然后就可以使用用$ucService访问UCenterr的登录方法uc_login,返回一个$uidarray值,$uidarray包函什么数据?使用“dump($uidarray);”打印出来,以便下一步的操作,打印结果如下:
array(4) {
["uid"] => string(1) "1"
["username"] => string(5) "qqabc"
["password"] => string(6) "123456"
["email"] => string(9) "[email protected]"
}
下一步就是以此$uidarray作为参数传给同步登录方法uc_synlogin($uidarray),最后echo 一下uc_synlogin($uidarray)的返回值$loginurl,就可以实现帐号"qqabc"在UCenter登录了。帐号"qqabc"在UCenter登录成功后程序将继续往下执行项目W3note的登录,这里就不多说了。最后的结果是,帐号"qqabc"实现了在UCenter和项目W3note的同步登录!