今天用uniapp尝试做了下抖音小程序,分享一下登录的做法
这里是uniapp获取code的代码
uni.checkSession({
success(res) {
http.post('/api/user/index', {}).then(res => {
uni.setStorageSync('userInfo', res.data.userinfo);
uni.setStorageSync('token', res.data.userinfo.token);
}).catch(err => {
})
},
fail(res){
uni.login({
provider: '',
success: function(loginRes) {
http.post('/api/user/toutiaoLogin', {
'code': loginRes.code,
'anonymousCode': loginRes.anonymousCode,
'share_uid': share_uid
}).then(res => {
if (res.code == 0) {
uni.showToast({
icon: 'none',
title: res.msg
})
return false;
} else {
try {
uni.setStorageSync('userInfo', res.data.userinfo);
uni.setStorageSync('token', res.data.userinfo.token);
} catch (e) {
uni.showToast({
icon: 'none',
title: '登录授权存储失败,请稍后重试~'
})
}
}
}).catch(err => {
})
},
fail(res) {
uni.showToast({
icon: 'none',
title: '登录授权失败,请稍后重试~'
})
}
})
}
})
开始php后端处理
请求获取openid接口post 要用json格式
$share_uid = input('share_uid/d',0);
$code=input('code');
$anonymousCode=input('anonymousCode');
if (empty($code)) {
$this->error('请传入正确参数');
}
$url='https://developer.toutiao.com/api/apps/v2/jscode2session';
$data=[
'appid'=>'888',
'secret'=>'888',
'code'=>$code,
//'anonymous_code'=>$anonymousCode,
];
$res=json_post($url,$data);
$res=json_decode($res,true);
if($res['err_no']==0){
$openid=$res['data']['openid'];
$isRegUser=Db::name('user')->where('openid',$openid)->find();
if(empty($isRegUser)){
//查询上级用户
$p1=0;
$p2=0;
$puser=Db::name('user')->find($share_uid);
if(!empty($puser)){
$p1=$puser['id'];
$puser_father=Db::name('user')->find($puser['p1']);
if(!empty($puser_father)){
$p2=$puser_father['id'];
}
}
$mobile=Random::alnum();
$ret = $this->auth->register($mobile.Random::alnum(), Random::alnum().Random::alnum(), Random::alnum().'@qq.com', $mobile, [
'openid'=>$openid,
'p1'=>$p1,
'p2'=>$p2,
'group_id'=>2,
]);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Sign up successful'), $data);
} else {
$this->error($this->auth->getError());
}
}else{
//模拟一次登录
$ret=$this->auth->direct($isRegUser['id']);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success('登录成功',$data);
} else {
$this->error($this->auth->getError());
}
}
}else{
$this->error($res['err_tips']);
}
function json_post($url, $data = [])
{
//$data=http_build_query($data);
//$data=json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if(!$data){
return 'data is null';
}
if(is_array($data))
{
$data = json_encode($data);
}
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data),
'Cache-Control: no-cache',
'Pragma: no-cache'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if ($errorno) {
return $errorno;
}
curl_close($curl);
return $res;
}