zoukankan      html  css  js  c++  java
  • 微信登录操作流程

    1:先获取微信code直接将本链接点击后,微信会提示请通过认证页面,点击后,就会自动跳转到http://www.ssss.com/pub/thirdlogin/这个方法中去,并且会在连接中带有code参数

     <a class="ico_wx" href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=787878778&redirect_uri=http://www.ssss.com/pub/thirdlogin/?type=wechat&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"><em></em><span>微信</span></a>

    2:通过上步点击链接,同意授权后,他会自动跳转到本方法中

    /**
         * 第三方登陆跳转
         */
        public function action_thirdlogin()
        {
    
            $type = Arr::get($_GET, 'type');
            //如果是微信登录
            if($type == "wechat"){
                $appid = $GLOBALS['cfg_weixi_appkey'];
                $appsecret = $GLOBALS['cfg_weixi_appsecret'];
                $code = Arr::get($_GET, 'code');//这就是获取的code
                if (empty($code)) {
                    Common::session('login_num', 1);
                    $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
                    Common::message(array('message' => '登陆失败!', 'jumpUrl' =>  $refer_url ));
                    die;
                }
                $rs = Common::wechat_login($appid, $appsecret,$code);//这就是通过code获取用户信息的方法,只需调用即可,里面包含用户信息,opemid
                if (empty($rs)) {
                    Common::session('login_num', 1);
                    $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
                    Common::message(array('message' => '登陆失败!', 'jumpUrl' =>  $refer_url ));
                    die;
                }
                Common::session('captcha_response', null);
    //在数据库用户表中建立wechatcode字段,在注册后,让用户绑定微信信息,在绑定的时候也是获取code,然后通过,方法获取openid,存进去存到这个字段中去
    //在这里通过openid查询wechat这个字段用户信息(本openid有没有在这个字段中),进行判短 ,本登录的微信有没有在用户表里面存在,如果没存在让他跳转登录界面,如果存在了,
    //那么给他吧用户信息查询出来存到session中去
    //然后跳转到个人中心,微信登录就这么简单 $member = DB::select('*')->from('member')->where('wechatcode', '=', $rs['openid'])->execute()->current(); if (empty($member)) { Common::session('login_num', 1); $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login"; Common::message(array('message' => '当前微信未绑定!', 'jumpUrl' => $refer_url ));//如果查不到这个微信信息,跳转登录让他注册并绑定微信 die; } else { Model_Member::write_session($member, $member['nickname']);//存session //清空登录次数 Common::session('login_num', null); $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member"; Common::message(array('message' => '登陆成功!', 'jumpUrl' => $refer_url ));//注册成功 } } }

      

    :通过code换取网页授权access_token,:拉取用户信息(需scope为 snsapi_userinfo)

    已经整理了方法:本方法是在获取code跳转到thirdlogin这个方法后,调用本方法进行获取用户信息用的,请看下文

     /**
         *
         * 微信授权
         */
        public static function wechat_login($appid, $appsecret,$code)
        {
            $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
    
            $token = json_decode(file_get_contents($token_url));
            if (isset($token->errcode)) {
                return false;
            }
            $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
            //转成对象
            $access_token = json_decode(file_get_contents($access_token_url));
            if (isset($access_token->errcode)) {
                return false;
            }
            $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
            //转成对象
            $user_info = json_decode(file_get_contents($user_info_url));
            if (isset($user_info->errcode)) {
                return false;
            }
            $result =  json_decode(json_encode($user_info),true);//返回的json数组转换成array数组
            return $result;
    //最后返回的就是个人信息
        }
    

      

  • 相关阅读:
    Android 手势&触摸事件
    vim常用命令总结
    关于背景中的雪花
    博客园美化——看板娘
    组合数
    扩展欧几里得和求逆元
    dfs序
    RMQ问题
    NOIP2017 列队
    线段树详解
  • 原文地址:https://www.cnblogs.com/yszr/p/9111823.html
Copyright © 2011-2022 走看看