注意事项:
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());
}
}