栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > thinkphp

小程序不需要授权的简单登录api接口用法

thinkphp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小程序不需要授权的简单登录api接口用法

下面以Thinkphp框架为示例,写一个小程序免授权的登录接口,通过小程序穿过来的code直接登录。


简单登录模式

// 简单登录
public function simple(){
    $xcx = Config::get('config_api.xcx_user');
    $code = input('code');
 
    // expires_in,openid,session_key
    $url = "https://api.weixin.qq.com/sns/jscode2session?app&secret=".$xcx['app_secret']."&js_code=".$code."&grant_type=authorization_code";
    $content = file_get_contents_by_curl($url);
    $res = object_array(json_decode($content)); //返回openid,expires_in,session_key

    if(!isset($res)) return json(['code'=>1,'msg'=>'请求失败','res'=>$res]);
    if(isset($res['errcode'])){
        return json(['code'=>1,'errmsg'=>$res['errmsg']]);
    }

    if(empty($res['openid'])) return json(['code'=>1,'msg'=>'获取openid错误']);

    $userInfo = UserModel::where('openid_xcx',$res['openid'])->find();
    
    if(empty($userInfo)){
        $createData['nickname'] = '';
        $createData['avatar_url'] = '';
        $createData['sex'] = '';
        $createData['unionid'] = $unionid;
        $createData['openid_xcx'] = $res['openid'];
        $createData['referrer'] = $referrer;
        $createData['last_login_time'] = time();
        $createData['last_login_ip'] = time();
        $createData['login_count'] = 1;
        $createData['subscribe'] = $subscribe;
        $createData['auth'] = 0;
        $userObj = UserModel::createUser($createData);
        $userInfo = UserModel::get($userObj->id);
    }

    return json(['code'=>0,'msg'=>'登录成功','userInfo'=>$userInfo,'res'=>$res]);

}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/824950.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号