phalapi做登录检测_GitHub - hs9206/phalapi-usercheck: PhalApi 2.x扩展类库,第三方用户登陆检测...

namespace App\Api;

use PhalApi\Api;

use PhalApi\Usercheck\Domain\Generator;

use PhalApi\Usercheck\Domain\UserSession;

use PhalApi\Usercheck\Domain\Login\Weixin as LoginWeixin;

use PhalApi\Usercheck\Domain\Login\Qq as LoginQQ;

use PhalApi\Exception\InternalServerError;

use PhalApi\Usercheck\Domain\Info as DomainInfo;

/**

* User扩展 - 登录服务

* @author Symo.Chan

*/

class Login extends Api {

public function getRules() {

return array(

'weixin' => array(

'openId' => array('name' => 'wx_openid', 'require' => true, 'min' => 1, 'max' => 28),

'token' => array('name' => 'wx_token', 'require' => true, 'min' => 1, 'max' => 150),

'expiresIn' => array('name' => 'wx_expires_in', 'require' => true, 'min' => 1, 'max' => 10),

'nickname' => array('name' => 'name', 'default' => '',),

'avatar' => array('name' => 'avatar', 'default' => '',),

),

'qq' => array(

'openId' => array('name' => 'qq_openid', 'require' => true, 'min' => 1, 'max' => 28),

'token' => array('name' => 'qq_token', 'require' => true, 'min' => 1, 'max' => 150),

'expiresIn' => array('name' => 'qq_expires_in', 'require' => true, 'min' => 1, 'max' => 10),

'nickname' => array('name' => 'name', 'default' => '',),

'avatar' => array('name' => 'avatar', 'default' => '',),

),

'getUserInfo' => array(

'otherUserId' => array('name' => 'other_user_id', 'type' => 'int', 'min' => 1, 'require' => true),

),

);

}

/**

* 微信登录

*

* - 首次绑定时,会自动创建新用户

* - 当逻辑有冲突,或者数据库写入失败时,以异常返回

*/

public function weixin()

{

$rs = array('code' => 0, 'info' => array(), 'msg' => '');

$domain = new LoginWeixin();

$isFirstBind = $domain->isFirstBind($this->openId);

$userId = 0;

if ($isFirstBind) {

$userId = Generator::createUserForWeixin($this->openId, $this->nickname, $this->avatar);

if ($userId <= 0) {

//异常1:用户创建失败

\PhalApi\DI()->logger->error('failed to create weixin user', array('openId' => $this->openId));

throw new InternalServerError(T('failed to create weixin user'));

}

$id = $domain->bindUser($userId, $this->openId, $this->token, $this->expiresIn);

if ($id <= 0) {

//异常2:绑定微信失败

\PhalApi\DI()->logger->error('failed to bind user with weixin',

array('userid' => $userId, 'openId' => $this->openId));

throw new InternalServerError(T('failed to bind user with weixin'));

}

} else {

$userId = $domain->getUserIdByWxOpenId($this->openId);

}

if ($userId <= 0) {

//异常3:微信用户不存在

\PhalApi\DI()->logger->error('weixin user not found',

array('userid' => $userId, 'openId' => $this->openId));

throw new InternalServerError(T('weixin user not found'));

}

$token = UserSession::generate($userId);

$rs['info']['user_id'] = $userId;

$rs['info']['token'] = $token;

$rs['info']['is_new'] = $isFirstBind ? 1 : 0;

return $rs;

}

/**

* QQ登录

*

* - 首次绑定时,会自动创建新用户

* - 当逻辑有冲突,或者数据库写入失败时,以异常返回

*/

public function qq()

{

$rs = array('code' => 0, 'info' => array(), 'msg' => '');

$domain = new LoginQQ();

$isFirstBind = $domain->isFirstBind($this->openId);

$userId = 0;

if ($isFirstBind) {

$userId = Generator::createUserForQq($this->openId, $this->nickname, $this->avatar);

if ($userId <= 0) {

//异常1:用户创建失败

\PhalApi\DI()->logger->error('failed to create qq user', array('openId' => $this->openId));

throw new InternalServerError(T('failed to create qq user'));

}

$id = $domain->bindUser($userId, $this->openId, $this->token, $this->expiresIn);

if ($id <= 0) {

//异常2:绑定QQ失败

\PhalApi\DI()->logger->error('failed to bind user with qq',

array('userid' => $userId, 'openId' => $this->openId));

throw new InternalServerError(T('failed to bind user with qq'));

}

} else {

$userId = $domain->getUserIdByQqOpenId($this->openId);

}

if ($userId <= 0) {

//异常3:QQ用户不存在

\PhalApi\DI()->logger->error('qq user not found',

array('userid' => $userId, 'openId' => $this->openId));

throw new InternalServerError(T('qq user not found'));

}

$token = UserSession::generate($userId);

$rs['info']['user_id'] = $userId;

$rs['info']['token'] = $token;

$rs['info']['is_new'] = $isFirstBind ? 1 : 0;

return $rs;

}

/**

* 用户信息

*/

public function getUserInfo() {

$rs = array('code' => 0, 'info' => array(), 'msg' => '');

// \PhalApi\DI()->usercheck->check(true);

$domain = new DomainInfo();

$info = $domain->getUserInfo($this->otherUserId);

if (empty($info)) {

$rs['code'] = 1;

$rs['msg'] = 'can not get user info';

\PhalApi\DI()->logger->debug('can not get user info', $this->otherUserId);

return $rs;

}

$rs['info'] = $info;

return $rs;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值