小程序静默登录 public function douyinlogin() { $config = Config::get('douyin'); $code = $this->request
小程序静默登录
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
来源地址:https://blog.csdn.net/qq_53457276/article/details/131375568
--结束END--
本文标题: PHP抖音小程序登录接口及微信和抖音数据互通
本文链接: https://lsjlt.com/news/390037.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0