登录的php代码 接口开发

 1  public function index()
 2     {
 3         //code值
 4         $code=input('code');
 5         //用户 昵称
 6         $nickname=input('nickname');
 7         //开发者的appid  secret  微信公众平台
 8         $appid="xxxxxxxx";
 9         $secret="xxxxxxxxxxxxxxx";
10         //发送请求获取用户的oppid与sessionkey
11         $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
12         $res=\Qiniu\json_decode(file_get_contents($url),true);
13         $userid=Users::where('openid',$res['openid'])->find();
14         if ($userid){
15             return json(['code'=>200,'msg'=>'success','data'=>$userid]);
16         }
17         //组装用户数据入库添加
18         $info=[
19             'nickname'=>$nickname,
20             'openid'=>$res['openid'],
21             'sessionkey'=>$res['session_key']
22         ];
23         //执行添加入库
24         $data=Users::create($info);
25         $info['id']=$data['id'];
26 //        print_r($info);
27         if ($data){
28             return json(['code'=>200,'msg'=>'success','data'=>$info]);
29         }else{
30             return json(['code'=>500,'msg'=>'error','data'=>""]);
31         }
32 
33     }

 

posted @ 2020-12-27 16:34  Conqueror·  阅读(196)  评论(0编辑  收藏  举报