|
具体搭建传送门:小程序开发记录
代码片段
PHP复制代码
Class UserLogin extends CI_Controller
{
function __construct ()
{
parent ::__construct ();
$this->load->library(array('WXLoginHelper'));
$this->load->model('General');
$this->load->driver('cache');
}
function login ()
{
/* code, rawData, signature, encryptData iv */
$postData = $this->input->post(array('code','rawData','signature','encryptedData','iv'));
$code = $postData['code'];
$rawData = $postData['rawData'];
$signature = $postData['signature'];
$encryptedData = $postData['encryptedData'];
$iv = $postData['iv'];
$test = new WXLoginHelper ;
$data = $test->checkLogin($code,$rawData,$signature,$encryptedData,$iv);
$session = $this->cache->memcached->save($data['session3rd'], $data['openId'] . $data['sessionKey']);
//检查user表是否存在改用户,如果存在,则则返回session,如果不存在,写入表再发回session
$find = $this->General->query('user',array('openid'=>$data['openId']));
$find = $find ==null ? 0:$find[0];
if($find == 0)
{
$user_id = $this->General->insert('user',array('openId'=>$data['openId'],'nickName'=>$data['nickName'],
'gender'=>$data['gender'],'city'=>$data['city'],'language'=>$data['language'],'province'=>$data['province'],'country'=>$data['country'],'avatarUrl'=>$data['avatarUrl']));
} else{
$user_id = $find['id'];
}
if(isset($find['avatarUrl'] && $find['avatarUrl'] !== $data['avatarUrl'])
{
$this->General->update('user',array('id'=>$user_id),array('avatarUrl'=>$data['avatarUrl'])); //如果头像更改,则更新头像
}
echo json_encode(array('status'=>0,'session3rd'=>$data['session3rd'],'user_id'=>$user_id));
}
function checkLogin ()
{ //如果客户端存在 session3rd,则post 到此接口,获取对应数据
$session3rd = $this->input->post('session3rd');
$data = $this->cache->memcached->get($session3rd);
echo json_encode(array('status'=>0,'session3rd'=>$data));
}
}
复制代码
|
评分
-
查看全部评分
|