极光一键登录PHP方法

注意事项:

1、 loginToken是sdk调用login()方法返回的logintoken,不是是时候方法返回的token
2、解密手机号码用的是【RSA加密私钥】,多次尝试不行的话,立刻去重置极光的公钥。重置公钥不需要审核,一下就好了

提示:重置方法
a、生成密钥对在这里插入图片描述
b、公钥去掉换行复制过来就可以了
c、 私钥不用去换行


php代码:

/**
 * @title : 极光一键登录API
 * @time  : 2021年12月3日/下午4:06:58
 */
function jpushlogin($loginToken='')
{
    // 请求头部
    $config = get_addon_config('jpush');
    $http_header = ['Content-type:application/json','Authorization: Basic '. base64_encode("{$config['AppKey']}:{$config['MasterSecret']}")];
    $data = curl_post('https://api.verification.jpush.cn/v1/web/loginTokenVerify', ['loginToken'=>$loginToken], $http_header);
    if(!$data)$this->error('无返回解析数据');
    if(($data['code']??null) != 8000)$this->error($data['content'],['code'=>$data['code'],'submitdata'=>input()]);
    // 解密手机号码
    $string_prefix = '-----BEGIN RSA PRIVATE KEY-----';
    $string_suffix = '-----END RSA PRIVATE KEY-----';
    $encrypted = $data['phone'];
    $key = $string_prefix . "\n" . $config['PrivateKey'] . "\n" . $string_suffix;
    openssl_private_decrypt(base64_decode($encrypted), $mobile,$key);
    if(!$mobile || strlen($mobile)!=11)$this->error('一键登录失败,请尝试使用’账号密码登录‘','-1');
    $user = \app\common\model\User::getByMobile($mobile);
    if ($user) {
        if ($user->status != 'normal') {
            $this->error(__('Account is locked'));
        }
        //如果已经有账号则直接登录
        $ret = $this->auth->direct($user->id);
        $type = 'login';
    } else {
        $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, ['avatar'=>config('site.newuseravatar')]);
        $type = 'register';
    }
    if ($ret) {
        $data = ['userinfo' => $this->auth->getUserinfo(),'extend'=>['type'=>$type]];
        $this->success(__('Logged in successful'), $data);
    } else {
        $this->error($this->auth->getError());
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值