阿里云一键登录(对接移动端) PHP
一键登录的流程
一键登录的好处
极速稳定 无需发送短信验证码,避免出现通道拥堵,短信延迟等不稳定因素。 节约短信成本 一键登录比短信验证成本降低了20%以上,而且不需要专门的开发运营。 优化用户体验 用户只需点击一下,不需要输入密码和验证码,2秒完成注册/登录。 降低注册 降低注册/登录门槛、从而减少流失率、提高转化率和留存率。 绑定手机号码运营商端校验手机号码,能确保手机号码的真实有效性,过滤无效注册/异常登录。 安全无风险 SUBMAIL 一键登录服务应用多种安全机制,服务器直连运营商接口,杜绝被劫持、盗用、篡改、刷量等风险。
第一步阿里云申请:
服务端不用拿这的任何东西(需要AccessKey管理拿值)
直接上代码:
public function oneClickLogin(Request $request)
{
$aliToken= $request->param('aliToken');
if(empty($aliToken)){
return GetJson::getJson(nullValue, 'token不能空', []);
}
$aliRes = $this->aliLoginGetMobile($aliToken);
if($aliRes['status'] == 'success'){
if(isset($aliRes['data']['Code']) && $aliRes['data']['Code'] == 'OK') {
$telphone = $aliRes['data']['GetMobileResultDTO']['Mobile'];
$user = User::where('mobile', $telphone)->find();
if (!$user) {
$login_type=1;
return $this->extracted($login_type,$telphone);
}
return $this->setToken($user);
}
return GetJson::getJson( Asuccess,$aliRes['data']['Code'].':'.$aliRes['data']['Message'], []);
}
return GetJson::getJson( Asuccess,$aliRes['code'].':'.$aliRes['msg'], []);
}
private function aliLoginGetMobile($token = ''){
$accessKeyId = config('alycode.aliOauth.accessKeyId');
$accessKeySecret = config('alycode.aliOauth.accessKeySecret');
AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
->regionId('cn-hangzhou')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dypnsapi')
->scheme('https')
->version('2017-05-25')
->action('GetMobile')
->method('POST')
->host('dypnsapi.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'AccessToken' => $token
],
])
->request();
return array('status' => 'success', 'data' => $result->toArray());
} catch (ClientException $e) {
return array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage());
} catch (ServerException $e) {
return array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage());
}
}
public function setToken($newUser)
{
$redis = Rediscon::get_instance();
$userInfo = $redis->hgetall('userInfo_' . $newUser['id']);
if (!$userInfo) {
foreach ($newUser as $k => $v) {
$redis->hset('userInfo_' . $newUser['id'], $k, $v);
}
}
$getToken=(new TokenServer())->generateToken($newUser['id']);
$Refreshtoken=(new TokenServer())->refreshToken($newUser['id']);
if ($getToken && $Refreshtoken) {
$dataValue = $this->getDataValue($newUser, $getToken, $Refreshtoken);
Log::write(json_encode($dataValue), 'info');
return GetJson::getJson(Asuccess, '登录成功', [$dataValue]);
}
return GetJson::getJson($this->Afile, '登录失败', []);
}
public function extracted($login_type,$phone)
{
$id = User::addUserInfo($login_type, $phone);
$newUser = User::where('id', $id)->find();
$newUser = json_decode(json_encode($newUser), true);
$redis = Rediscon::get_instance();
$userInfo = $redis->hgetall('userInfo_' . $id);
if (!$userInfo) {
foreach ($newUser as $k => $v) {
$redis->hset('userInfo_' . $newUser['id'], $k, $v);
}
}
$getToken = (new TokenServer())->generateToken($id);
$Refreshtoken=(new TokenServer())->refreshToken($id);
if ($getToken && $Refreshtoken) {
$dataValue = $this->getDataValue($newUser, $getToken, $Refreshtoken);
Log::write('日志', 'info', $dataValue);
return GetJson::getJson(Asuccess, '登录成功', [$dataValue]);
}
return GetJson::getJson($this->Afile, '登录失败', []);
}
|