zoukankan      html  css  js  c++  java
  • 微信公众号网页授权登录

    /**
    *用户授权登录
    */
    public function getOauthRedirect()
    {
    $appid = $this->appid; //appid
    $callback = ''; //回调地址
    $res = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid .
    '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=snsapi_userinfo
    &state=1&connect_redirect=1#wechat_redirect';
    header("Location:" . $res);
    }

    /**
    * 获取用户信息
    * @param Request $request
    * @return string
    */
    public function getOauthUserinfo(Request $request)
    {
    $code = $request->input('code') ? $request->input('code') : ''; //获取code

    //使用code和appid与秘钥 获取openid和access_token
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' .
    $this->appid . '&secret=' . $this->appsecret . '&code=' . $code . '&grant_type=authorization_code';

    $data = file_get_contents($url); //获取openid
    $data1 = json_decode($data, TRUE); //转数组
    if(!isset($data1['access_token'])) return jsonReturn(-1,'授权失败');
    $url1 = 'https://api.weixin.qq.com/sns/userinfo?access_token=' .
    $data1['access_token'] . '&openid=' . $data1['openid'];
    $data2 = file_get_contents($url1); //获取用户信息
    $data3 = json_decode($data2, TRUE);
    $openid = $data3['openid'];
    $nickname = $data3['nickname'];
    $sex = $data3['sex'];

    if ($openid != '' && $nickname != '') {
    $request->session()->put('openid', $openid);
      try{
              //逻辑操作
    }catch (Exception $e){
    return $e->getMessage();
    }
    }else{
    $request->session()->put('member_id', $member['id']);
    }
          $target_url =empty(session('target_url')) ? '/web' : session('target_url');

            header("Location:" . $target_url); //可能会失效 可改用js跳转
            // echo "<script language='javascript' type='text/javascript'>

    window.location.href=$target_url;</script>";return false;
            } else {
    // $this->getOauthRedirect();
    return jsonReturn(-1,'授权失败');

    }

    }
  • 相关阅读:
    算法导论图论图的表示 课后题答案
    MFC 如何添加快捷键
    全排序算法permutation分析与总结
    java k++ 和C/C++ k++的区别
    找回失去的快捷方式向导
    解开注册表中U盘禁止拷贝的限制
    锐捷多网卡解决方案 与当前环境冲突(Code 2)
    Delphi中的Access技巧集
    Delphi MessageBox对话框
    另一个博客,不知道好用不
  • 原文地址:https://www.cnblogs.com/hua-nuo/p/13431771.html
Copyright © 2011-2022 走看看