首先,小程序的登录流程图是这样的
项目登录流程思路:
第一步:获取openid(在之前的文章写了,这里不再复述)
第二步 : 与数据库中的openid对比,如果存在openid表示已经授权登录过了。直接返回用户信息,同时需要将session_key存到redis,后续手机号码授权时需要用到。如果不存在,表示未授权登录。跳转到授权登录
//获取用户是否已经授权
public function getUserId()
{
$code = $this->input->post('code');
if (empty($code)){
return $this->fail('','code不能为空');
}
$apiData = $this->get_api_data($code);
if (!isset($apiData['errcode'])){
$openID= $apiData['openid'];
$userData = $this->AppUserModel->get(['openid'=>$openID]);
if (empty($userData)) {
return $this->fail('','未授权');
}else{
$sessionKey = $apiData['session_key'];
$mc = &load_cache('redis');
$mc->save('session_key', $sessionKey, 3600);
$returnData['uid']=$userData['id'];
return $this->success($returnData,'已授权');
}
}else {
return $this->fail('','获取openid失败');
}
}
第三步 :微信授权登录,获取openid和
session_key。然后通过解密获取到用户信息
//未授权用户,请求微信接口,进行授权,获取用户信息
public function saveUser()
{
$appid = "自己的APPID" ;
$code = $this->input->post('code');
if (empty($code)){
return $this->fail('','code不能为空');
}
$encryptedData = $this->input->post('encryptedData');
if (empty($encryptedData)){
return $this->fail('','encryptedData不能为空');
}
$iv = $this->input->post('iv');
if (empty($iv)){
return $this->fail('','iv不能为空');
}
$apiData = $this->get_api_data($code);
if(!isset($apiData['errcode'])){
$sessionKey = $apiData['session_key'];
$userifo = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $userifo->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
$data = json_decode($data,true);
$userData = [
'nickname' =>$data['nickName'],
'headimg' =>$data['avatarUrl'],
'unionid' =>$data['unionId'],
'openid' =>$data['openId'],
'c_time' =>time(),
];
$result = $this->AppUserModel->get(['openid'=>$data['openId']]);
if (!$result){
$userDataId = $this->AppUserModel->add($userData);
if ($userDataId){
$returnData['uid']=$userDataId;
return $this->success($returnData,'已授权');
}else{
return $this->fail('','授权失败');
}
}else{
$this->AppUserModel->update($userData,['openid'=>$data['openId']]);
$returnData['uid']=$result['id'];
return $this->success($returnData,'已授权');
}
}
}else{
return $this->fail($apiData,'获取用户信息失败');
}
}
第四步 : 这边是手机号码授权,获取用户手机号码
//获取用户手机号码授权
public function getUserMobile()
{
$encryptedData = $this->input->post('encryptedData');
if (empty($encryptedData)){
return $this->fail('','encryptedData不能为空');
}
$iv = $this->input->post('iv');
if (empty($iv)){
return $this->fail('','iv不能为空');
}
$uid = $this->input->post('uid');
if (empty($uid)){
return $this->fail('','uid不能为空');
}
$appid = "自己的APPID" ;
//读取redis
$mc = &load_cache('redis');
$sessionKey= $mc->get('session_key');
include_once APPPATH."/third_party/WeChat/wxBizDataCrypt.php";
$userifo = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $userifo->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
$data = json_decode($data,true);
$userData = [
'mobile' =>$data['phoneNumber'],
];
$where['id'] = $uid;
$result = $this->AppUserModel->update($userData,$where);
if ($result){
return $this->success('','手机号码已授权');
}else{
return $this->fail('','手机号码授权失败');
}
}else{
return $this->fail('','获取用户手机号码失败');
}
}