easywechat 网页授权登录

使用easyWeChat进行微信网页的授权登录

public function __construct ( Request $request = null ){
        parent::__construct();
        $this->config = [
            'app_id' => 'wx11ca*****98df5',
            'secret' => 'f860284********2d732d0beae9',
            // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
            // 'response_type' => 'array',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/wx_callback',
            ],
        ];
    }
    //登录操作
    public function login(){
        if(session('wechat_user')){
            $this->redirect('/index2',302);
        }
        $app = Factory::officialAccount($this->config);
        // 未登录,跳转到登录界面
        if (!session('wechat_user')) {
            $oauth = $app->oauth;
            return $oauth->redirect();
        }
    }
    //保存登录信息
    public function wx_callback(){
        $code = input('code','');
        $app = Factory::officialAccount($this->config);
        $user = $app->oauth->user()->toArray();
        session('wechat_user',$user);
        $user_data['openid'] = $user['id'];
        $user_data['name'] = $user['name'];
        $user_data['pic'] = $user['avatar'];
        $user_data['ctime'] = time();
        $user_data['ip'] = request()->ip();
        $user_data['sex'] = $user['original']['sex'];
        $user_data['address'] = $user['original']['country'].'-'.$user['original']['province'].'='.$user['original']['city'];
        $is = Db::name('user')->where(['openid'=>$user_data['openid']])->find();
        if(!$is){
            $res = Db::name('user')->insert($user_data);
            $is = Db::name('user')->where(['openid'=>$user_data['openid']])->find();
        }
        if($is){
            $this->redirect('/index2',302);
        }else{
            echo 'err';
        }

    }

    //首页
    public function index2(){
        if(!session('wechat_user')){
            $this->redirect('/login',302);
        }
        $seo['title'] = config()['web']['site_title'];
        $seo['keywords'] = config()['web']['site_keywords'];
        $seo['description'] = config()['web']['site_description'];
        $this->assign('seo', $seo);
        $app = Factory::officialAccount($this->config);
        $this->assign('app', $app);
        return view($this->tpl);
    }

出现的问题

打开的页面出现如下信息

image-20200826150321541

找到如下目录,可以隐藏或者删除html

image-20200826150539104

高颜值后台管理系统免费使用 ### 子枫后台管理系统 ###,可在宝塔面板直接安装

欢迎关注我的公众号:子枫的奇妙世界,获得独家整理的学习资源和日常干货推送。 如果您对我的其他专题内容感兴趣,直达我的个人博客:www.wangmingchang.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员子枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值