PHP抖音小程序登录接口及微信和抖音数据互通

小程序静默登录

    /**
     *
     * @ApiTitle    (抖音静默登录)
     * @ApiSummary  (抖音静默登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiParams   (name="anonymous_code", type="string", required=true, description="anonymous_code")
     * @ApiParams   (name="phone", type="string", required=true, description="手机号")
     */
    public function douyinlogin()
    {
        $config = Config::get('douyin');
        $code = $this->request->param('code');
        $anonymous_code = $this->request->param('anonymous_code');
        $phone = $this->request->param('phone');
        if (empty($code) && empty($anonymous_code)) {
            $this->error('参数错误');
        }
        $params = [
            "appid" => $config['appid'],
            "secret" => $config['secret'],
            "anonymous_code" => $anonymous_code,
            "code" => $code
        ];
        $configs = new Resource();
        $url = "https://developer.toutiao.com/api/apps/v2/jscode2session";
        $res = $configs->json_post($url, $params);
        $res = json_decode($res, true);
        if ($res['err_no'] == 0) {
            if (isset($res['data']['openid'])) {
                $userId = Db::name('third')->where(['apptype' => 'douyin', 'openid' => $res['data']['openid']])->value('user_id');
//                dump($userId);
                $auth = Auth::instance();
                $ret = $auth->direct($userId);
//                dump($ret);
                if ($ret) {  //假如登录上后获取信息
                    $data = $auth->getUserinfo();
                    $aut = \db('user')->where('id', $userId)->find();
                    $data['avatar'] = cdnurl($data['avatar']);  //cdnurl第三方存储
                    $data['mobile'] = $aut['mobile'];
                    $this->success('登录成功', $data);
                } elseif (empty($ret)) {
//                    dump(2220);
                    /*注册一个用户*/
                    //根据输入的手机号查询用户
                    $find = \db('user')->where('mobile', $phone)->find();
                    if (empty($find)) {    //用户表没记录
                        $data = [
                            'nickname' => '抖音养殖户',
                            'username' => '抖音养殖户',
                            'avatar' => '/assets/img/avatar.png',
                            'status' => 'normal',
                            'mobile' => $phone,
                            'jointime' => time(),
                            'createtime' => time(),
                            'platform' => 2
                        ];
                        //插入user
                        $id = \db('user')->insertGetId($data);
                    } else {  //用户表有记录
                        $data = [
                            'nickname' => '抖音微信养殖户',
                            'username' => '抖音微信养殖户',
                            'platform' => 3
                        ];
                        //插入user
                        \db('user')->where('id', $find['id'])->update($data);
                        $id = $find['id'];
                    }
                    \db('third')->insert(['user_id' => $id, 'apptype' => 'douyin', 'openname' => '抖音用户',
                        'access_token' => $res['data']['session_key'], 'openid' => $res['data']['openid']]);
                    $userId = Db::name('third')->where(['apptype' => 'douyin', 'openid' => $res['data']['openid']])->value('user_id');
                    $auth = Auth::instance();
                    $ret = $auth->direct($userId);
                    if ($ret) {
                        $data = $auth->getUserinfo();
                        $aut = \db('user')->where('id', $userId)->find();
                        $data['avatar'] = cdnurl($data['avatar']);
                        $data['mobile'] = $aut['mobile'];
                        $this->success('登录成功', $data);
                    } else {
                        $this->error('连接失败');
                    }
                }
            } else {
                $this->error('获取openid失败');
            }
        } 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;

    }

数据互通是通过手机号判断,上述代码中有示例

原则:用户表中一条数据,第三方表中两条数据对应一个用户ID

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值