zoukankan      html  css  js  c++  java
  • 微信授权登录

    加链接:<a class="ico_wx" href="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$cfg_weixi_appkey}&redirect_uri={$main_host}/pub/thirdlogin/?type=wechat&response_type=code&scope=snsapi_base&state={$code}#wechat_redirect"><em></em><span>微信</span></a>

    点击微信图标后,会跳转到pub/thirdlogin这个方法中去,并且将code码带过去,然后再通过微信的接口方法,获取用户信息进行保存

    /**
         * 第三方登陆跳转,这里就是跳转的方法
         */
        public function action_thirdlogin()
        {
            $type = Arr::get($_GET, 'type');//$cookei = $_COOKIE;
            //print_R($sesharecode);die;
            //如果是微信登录
            if($type == "wechat"){
    
                $appid = $GLOBALS['cfg_weixi_appkey'];
                $appsecret = $GLOBALS['cfg_weixi_appsecret'];
                $code = Arr::get($_GET, 'code');//这就是获取传递过来的code码
                $fx_code = Arr::get($_GET, 'state');
                if (empty($code)) {
                    Common::session('login_num', 1);
                    $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
                    Common::message_t(array('message' => '登陆失败', 'jumpUrl' =>  $refer_url ));
                    die;
                }
                $rs = Common::wechat_login($appid, $appsecret,$code);//print_r($rs);die;这里就是调用微信的方法进行获取用户信息,到这里就能获取到用户信息了,下面的就是进行按照需求存储用户信息即可,不用看了
                if (empty($rs)) {
                    Common::session('login_num', 1);
                    $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
                    Common::message_t(array('message' => '登陆失败', 'jumpUrl' =>  $refer_url ));
                    die;
                }
               // Common::session('captcha_response', null);
    
                $member = DB::select('*')->from('member')->where('wechatcode', '=', $rs['openid'])->execute()->current();//判断用户是否存在
                if (empty($member)) //用户不存在,注册之
                {
                    // $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/register?code=".$fx_code."&openid=".$rs['openid'];
                    // Common::message_t(array('message' => '请补充剩余资料完成注册', 'jumpUrl' =>  $refer_url ));
                    // die;
                    //获取包含的url
                    //$redirecturl = $_GET['redirecturl'];
                    //检测分享码是否存在
                    //$sharing_code = Common::session('wx_fx_code');//Common::get_fx_code($redirecturl);
                    //检测上下级关系
                   /* if($sharing_code){
                        $data = Common::get_sharing_data($sharing_code);
                        if ($data == false) {
                            $data['pp_level'] = null;
                            $data['p_level'] = null;
                            $data['distribution'] = 0;
                        }
                    }else{
                        $data['pp_level'] = null;
                        $data['p_level'] = null;
                        $data['distribution'] = 0;
                    };*/
                    //生成本人的code码
                    $usercode = Common::get_rand_string($len=10,$type='',$addChars='');
                    //获取用户的openid
                    $wechatcode = $rs['openid'];
                    //用户头像
                    $litpic = $rs['headimgurl'];
                    //分享类型
                    $userType = 'wechat';
                    //用户昵称
                    //$nickname = $rs['nickname'];
                    $regtype = 0;
                    if ($userType == 'wechat')
                    {
                        $isPhone = false;
                        $nickname = $rs['nickname'];
                        //$nickname='wechat'.substr($wechatcode,0,4).'***';
                    } else
                    {
                        $regtype = 1;
                        $nickname = 'wechat******';
                    }
                    $addtime = time();
                    //新增数据
                    list($insertId, $rows) = DB::insert('member', array('nickname', 'litpic', 'jointime', 'logintime', 'regtype','usercode','p_level','pp_level','wechatcode','distribution'))
                    ->values(array($nickname, $litpic, $addtime, $addtime, $regtype,$usercode,$data['p_level'],$data['pp_level'],$wechatcode,$data['distribution']))->execute();
                    
                    if ($rows > 0)
                    {
                        Common::session('crsf_code', null);
                        Common::session('captcha_response', null);
                        Common::session('msg_code', null);
                        Common::session('wx_fx_code',null);
                        //发送注册成功信息
                        if ($isPhone)
                        {
                            St_SMSService::send_member_msg($user,NoticeCommon::MEMBER_REG_MSGTAG,$user,$pwd,"");
                        } else
                        {
                            St_EmailService::send_member_email($user,NoticeCommon::MEMBER_REG_MSGTAG,$pwd,"");
                        }
                        //注册送积分
                        $jifen = Model_Jifen::reward_jifen('sys_member_register',$insertId);
                        if(!empty($jifen))
                        {
                            St_Product::add_jifen_log($insertId,"注册赠送积分{$jifen}",$jifen,2);
                        }
                        //登录状态
                        Model_Member::write_session(Model_Member::get_member_byid($insertId));
                        $message = array('url' => Cookie::get('referer', $this->cmsurl), 'status' => 1);
                        $ucsynlogin = '';
                        if (defined('UC_API') && @include_once BASEPATH . '/uc_client/client.php')
                        {
                            $uid = uc_user_register($user, $pwd, $user);
                            if ($uid > 0)
                            {
                                $ucsynlogin = uc_user_synlogin($uid);
                            }
                        }
                        $message['js'] = $ucsynlogin;
                        Plugin_Core_Factory::factory()->add_listener('on_member_register', ORM::factory('member', $insertId)->as_array())->execute();
                        $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member";
                        Common::message_t(array('message' => '登陆成功', 'jumpUrl' =>  $refer_url ));
                    }
    
                }
                else
                {   
                    /*********************[生日判断]****************************/
                    $a = $member['birth_date'];
                    $b = explode('-',$a);
                    array_shift($b);
                    $str = implode('-', $b);
                    $now = date('m-d');
                    if ($now == $str) { //确认过生日
                        $result       = DB::select('addtime')->from('message')->where('memberid','=',$member['mid'])->and_where('type','=','104')->execute()->current();
                        if (!empty($result['addtime'])) { //有生日记录
                            if (date('Y',$result['addtime']) != date('Y')) { //判断是否为今年的生日祝福
                                $column       = array('type','content','memberid','addtime','status');
                                $column_value = array('104','生日快乐',$member['mid'],time(),0);
                                DB::insert('message', $column)->values($column_value)->execute();
                            }
                        }else{ //没有生日记录
                            $column       = array('type','content','memberid','addtime','status');
                            $column_value = array('104','生日快乐',$member['mid'],time(),0);
                            DB::insert('message', $column)->values($column_value)->execute();
                        }
                    }
                    /*********************[生日判断]****************************/
                    //用户存在
                    Model_Member::write_session($member, $member['nickname']);
                    //清空登录次数
                    Common::session('login_num', null);
                    //删除Cookie
                    $message = array('url' => Cookie::get('referer', $this->cmsurl), 'status' => 1);
                    #api{{
                    $ucsynlogin = '';
                    if (defined('UC_API') && include_once BASEPATH . '/uc_client/client.php')
                    {
                        //检查帐号
                        list($uid, $loginname, $password, $email) = uc_user_login($member['nickname'], $member['password']);
    
                        if ($uid > 0)
                        {
                            //同步登录的代码
                            $ucsynlogin = uc_user_synlogin($uid);
                        }
                        else if ($uid == -1)
                        {
                            $uid = uc_user_register($loginname, $member['password'], '');
                            if ($uid > 0)
                            {
                                $ucsynlogin = uc_user_synlogin($uid);
                            }
                        }
                    }
                    $refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member";
                    Common::message_t(array('message' => '登陆成功', 'jumpUrl' =>  $refer_url ));
                }
            }
    
        }

    下面是封装的获取用户信息的微信方法:拿过来直接用就行

    /**
         *
         * 微信授权
         */
        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));
            //var_dump($token);die;
            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;
            }
            //var_dump($user_info);die;
            $result = json_decode(json_encode($user_info), true);//返回的json数组转换成array数组
            //var_dump($result);die;
            return $result;
        }
  • 相关阅读:
    响应式布局,流式布局与固定布局
    垃圾回收机制
    形象讲解时间复杂度
    数据结构和算法简介
    数据结构之栈和队列
    十、str的索引、切片和str常用的操作方法(重点)
    九、基础数类型总览和str、int、bool之间的相互转化
    八、编码的初识和进阶
    七、格式化输出和运算符
    六、while循环
  • 原文地址:https://www.cnblogs.com/yszr/p/9564830.html
Copyright © 2011-2022 走看看