小程序静默登录

    /**
     *
     * @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) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部