簡單實用 微信授權登陸(網頁版)

備註:使用該方法前要將你的微信公衆號綁定到你的微信開放平臺上面


    private $APP_ID = 'wx7b2a3-----------' ; //APP_ID
    private $APP_SECRET = '3dcaf80d6adf6-----------------'; //$APP_SECRET


    //獲取Access Token
    private function getAccessToken($code){

        $ACCESS_TOKEN = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->APP_ID.'&secret='.$this->APP_SECRET.'&code='.$code.'&grant_type=authorization_code');
        return $ACCESS_TOKEN;
    }


    //獲取用戶信息方法
    private function getUserInfo($access_token,$openid){
        return(file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid));
    }


    //獲取登錄;
    public function login(Request $request){
        $code = request('code');
        $language = request('language'); //中英文
        //獲取access_token等信息
        $access_token=$this->getAccessToken($code);
        $assess_tokenArr = json_decode($access_token,true);
        //獲取用戶信息
        $userinfo = $this->getUserInfo($assess_tokenArr['access_token'],$assess_tokenArr['openid']);
        $wxinfo = json_decode($userinfo,true);
 
        $user = Users::where('wx_unionid',$wxinfo['unionid'])->first();

        if($user){
            if(empty($user->pic)){
                $user->pic = $wxinfo['headimgurl'];
            }
            $wxinfo['nickname'] = $this->removeEmoji($wxinfo['nickname']);
            if(!Users::where('nickname',$wxinfo['nickname'])->first()){
                $user->nickname = $wxinfo['nickname'];
            }

            Redis::hdel('wap_token:'.$user->wap_token,'wapUserInfo');
            $tokenKey = str_random(64);
            $user->wx_unionid = $wxinfo['unionid'];
            $user->login_ip = $request->ip();
            $user->login_time = time();
            $user->wap_token = $tokenKey;
            unset($user->password);
            if(empty($user['phone'])){
                $checkphone = 0;
            }else{
                $checkphone = 1;
            }
            if ($user->save()) {
                unset($user->wap_token);
                if (Redis::hmset('wap_token:' . $tokenKey, ['wapUserInfo' => $user]) == 'OK' && Redis::expire('wap_token:' . $tokenKey, 7200)) {
                    if($language == 'zh'){
                        $vdata = [
                            'status' => 1,
                            'msg' => '登錄成功',
                            'wap_token' => $tokenKey,
                            'checkphone'=>$checkphone
                        ];
                    }else{
                        $vdata = [
                            'status' => 1,
                            'msg' => 'Login successfully',
                            'wap_token' => $tokenKey,
                            'checkphone'=>$checkphone
                        ];
                    }
                }
            }
        }else{
            $data['wx_unionid'] = $wxinfo['unionid'];
            $data['reg_time'] = time();
            $data['pic'] = $wxinfo['headimgurl'];
            $data['wap_token'] =  str_random(64);
            $data['username'] = 'mooby_'.uniqid();
            $wxinfo['nickname'] = $this->removeEmoji($wxinfo['nickname']);
            if(!Users::where('nickname',$wxinfo['nickname'])->first()) {
                $data['nickname'] = $wxinfo['nickname'];
            }else{
                $data['nickname'] = substr(md5(microtime(true)), 0, 8);
            }
            $data['login_ip'] = $request->ip();
            $data['login_time'] = time();
            if($insert_id = Users::create($data)){
                $userinfo = Users::find($insert_id->id);
                unset($userinfo->password);
                if (Redis::hmset('wap_token:' . $userinfo->wap_token, ['wapUserInfo' => $userinfo]) == 'OK' && Redis::expire('wap_token:' . $userinfo->wap_token, 7200)) {
                    $vdata = [
                        'msg' => 'Success',
                        'status' => 1,
                        'wap_token'=>$userinfo->wap_token,
                        'checkphone'=>0,
                    ];
                }else{
                    $vdata = [
                        'msg' => 'Error',
                        'status' => -1
                    ];
                }
            }
        }
        return response()->json($vdata);
    }

    //用戶名稱過濾不能保存的表情與字符
    function removeEmoji($clean_text) {
   
        // Match Emoticons
        $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
        $clean_text = preg_replace($regexEmoticons, '', $clean_text);
        // Match Miscellaneous Symbols and Pictographs
        $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
        $clean_text = preg_replace($regexSymbols, '', $clean_text);
        // Match Transport And Map Symbols
        $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
        $clean_text = preg_replace($regexTransport, '', $clean_text);
        // Match Miscellaneous Symbols
        $regexMisc = '/[\x{2600}-\x{26FF}]/u';
        $clean_text = preg_replace($regexMisc, '', $clean_text);
        // Match Dingbats
        $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
        $clean_text = preg_replace($regexDingbats, '', $clean_text);
        return $clean_text;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章