抖音小程序 php登录获取openid

今天用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;

}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易拉菲的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值