<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                慢慢的你會發現除了獲取參數的地方不一樣,登錄流程都差不多,可能是接口路徑啥的不太一致 但是獲取用戶信息,**出奇的一至**:`https://api.weixin.qq.com/sns/userinfo` **獲取openid的卻不一樣** 小程序的是:`https://api.weixin.qq.com/sns/jscode2session` 掃碼的、公眾號的、app的是:`https://api.weixin.qq.com/sns/oauth2/access_token` ## pc掃碼 開始解說 先來個公共的curl函數 ```php function geturl($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true); return $output; } ``` 1、獲取二維碼 ```php $appid="wx1e1275cb2e70e359"; //掃碼后回調地址 $url=urlencode("http://www.taianyuannongmu.com/business/login/weixin2"); $headerurl="https://open.weixin.qq.com/connect/qrconnect?appid=".$appid; $headerurl.="&redirect_uri=".$url; $headerurl.="&response_type=code&scope=snsapi_login&state=wechat_redirect" header("Location:".$headerurl); ``` 2、成功回調獲取openid ```php $appid=""; $AppSecret=""; $url=urlencode("http://www.taianyuannongmu.com/business/login/weixin2"); $weixinurl= "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid; $weixinurl.="&secret=".$AppSecret; $weixinurl.="&code=".$_GET["code"]; $weixinurl.="&grant_type=authorization_code"; //這里是curl $res=$this->geturl($weixinurl); $_SESSION["access_token"]=$res['access_token']; $_SESSION["openid"]=$res['openid']; ``` 3.獲取用戶信息 ```php $access_token=$_SESSION["access_token"]; $openid=$_SESSION["openid"]; /*獲取用戶信息*/ $get_user_info_url='https://api.weixin.qq.com/sns/userinf'; $get_user_info_url.='?access_token='.$access_token; $get_user_info_url.='&openid='.$openid.'&lang=zh_CN'; $weixin3res= $this->geturl($get_user_info_url); ``` 剩下的就是邏輯代碼處理 ## app登錄 ![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210415141316813.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd6aGFl,size_16,color_FFFFFF,t_70) 前端調取授權 后端根據前端的code獲取OPENID和身份信息 先來個公共的curl函數 ```php function geturl($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output,true); return $output; } ```php $appid = ""; //開發平臺申請 $appsecret = ""; //開發平臺申請 $code = $_GET['code']; //安卓端提供用戶同意登入后的code //認證 $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code ."&grant_type=authorization_code"; //調用微信api $rs = $this -> geturl($url); if(!$rs)$this -> error('獲取OPENID失敗'); $rt = json_decode($rs, 1); if($rt['errcode'])$this -> error('授權失敗:'.$rt['errmsg']); // 拉取用戶信息 $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$rt['access_token']."&openid=".$rt['openid']."&lang=zh_CN "; $wechat_info = $this -> geturl($url); if(!$wechat_info)$this -> error('獲取用戶資料失敗:CURL '.$http -> errmsg); $wechat_info = json_decode($wechat_info, 1); if($wechat_info['errcode']){ $this -> error("獲取用戶資料失敗".$wechat_info['errmsg']); } $user_info = array( "headimg"=>$wechat_info['headimgurl'], //頭像 "nickname"=>$wechat_info['nickname'], //昵稱 "sex"=>$wechat_info['sex'], //性別 "openid"=>$wechat_info['openid'], //app唯一 "unionid"=>$wechat_info['unionid'] //微信內部唯一,小程序, 公眾號, web, 移動應用都是一致的 ); ``` ## 小程序 ![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210415140958556.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd6aGFl,size_16,color_FFFFFF,t_70) 1、前端獲取到code后傳給后端 2、通過code獲取openid 接口路徑https://api.weixin.qq.com/sns/jscode2session? 3、通過openid獲取用戶信息 接口路徑 https://api.weixin.qq.com/sns/userinfo? 整合代碼 ```php <?php namespace real; /** * 小程序登錄類 */ class WeChatMiniProgram { public $APPID=""; public $SECRET=""; // 1、前端獲取到 code 后傳給后端 // 2、通過 code 獲取 openid public function getOpenid($code){ $httpcurl="https://api.weixin.qq.com/sns/jscode2session?"; $httpcurl.="appid=".$this->APPID; $httpcurl.="&secret=".$this->SECRET; $httpcurl.="&js_code=".$code; $httpcurl.="&grant_type=authorization_code"; $zz=$this->geturl($httpcurl); return $zz; } //3、通過openid獲取用戶信息 public function getUserinfo($access_token,$openid){ $get_user_info_url='https://api.weixin.qq.com/sns/userinfo'; $get_user_info_url.='?access_token='.$access_token; $get_user_info_url.='&openid='.$openid.'&lang=zh_CN'; $data=$this->geturl($get_user_info_url); return $data; } //getcurl public function geturl($url){ $headerArray =array("Content-type:application/json;","Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); $output = curl_exec($ch); //關閉URL請求 if($output === FALSE ){ echo "CURL Error:".curl_error($ch); } curl_close($ch); $output = json_decode($output,true); return $output; } } ``` ## 公眾號 **寫代碼前先得服務器配置** 服務器配置驗證用 ```php $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = 'weixuegu'; $signature = $_GET['signature']; $array = array($timestamp,$nonce,$token); sort($array); //將排序后的三個參數拼接之后參數拼接之后進行sha1加密 $tmpstr = implode('',$array); $tmpstr = sha1($tmpstr); // $file_write = file_put_contents("./".time().'.text',$signature.",".json_encode($tmpstr)); //將加密后的字符串與signature進行對比; if($tmpstr == $signature && isset($_GET['echostr'])){ echo $_GET['echostr']; exit; }else{ $this->responseMsg(); } ``` **去權限設置,授權登錄,設置域名** ![在這里插入圖片描述](https://img-blog.csdnimg.cn/2020061717411070.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd6aGFl,size_16,color_FFFFFF,t_70) ![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210415141227508.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd6aGFl,size_16,color_FFFFFF,t_70) ![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210415141239731.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd6aGFl,size_16,color_FFFFFF,t_70) 來我們解釋代碼了 **微信公眾號第一步:獲取code** ```php $appid= $this->appid; $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="; //appid $url.=$appid; $url.="&redirect_uri="; // 這里是回調的域名和地址 $url.="http://".$_SERVER['SERVER_NAME']."/api/Weixinlogin"; //下面的都是固定參數 $url.="&response_type=code"; $url.="&scope=snsapi_userinfo"; $url.="&state="."123"; $url.="#wechat_redirect"; header("location: ".$url) ; exit(); ``` **微信公眾號第二步:根據code獲取openid** ```php $appid= $this->appid; $secret= $this->secret; //第一步返回的code $code = $_GET['code']; $get_token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='; $get_token_url.=$appid; $get_token_url.='&secret='; $get_token_url.=$secret; $get_token_url.='&code='; $get_token_url.=$code; $get_token_url.='&grant_type=authorization_code'; //通過curl獲取到數據access_token和openid $curl = curl_init(); // 啟動一個CURL會話 curl_setopt($curl, CURLOPT_URL, $get_token_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// 跳過證書檢查 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); if($res === FALSE ){ echo "CURL Error:".curl_error($curl); } $json_obj=json_decode($res,true); $_SESSION["access_token"]=$json_obj['access_token']; $_SESSION["openid"]=$json_obj['openid']; ``` **微信公眾號第四步:根據openid獲取用戶信息** 這里不用第三步,沒必要哈 ```php $access_token=$_SESSION['access_token']; $openid=$_SESSION["openid"]; $get_user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $curl1 = curl_init(); // 啟動一個CURL會話 curl_setopt($curl1, CURLOPT_URL, $get_user_info_url); curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);// 跳過證書檢查 curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false); $res1 = curl_exec($curl1); //返回api的json對象 //關閉URL請求 if($res1 === FALSE ){ echo "CURL Error:".curl_error($curl1); } ////解析json $user_obj=json_decode($res1,true); $data=$user_obj; ``` **然后我們可以根據openid,進行登錄,如果沒有賬戶,讓客戶綁定手機或者進行注冊** ```php <?php namespace app\api\controller; header('Access-Control-Allow-Origin:*'); // 響應類型 header('Access-Control-Allow-Methods:*'); // 響應頭設置 header('Access-Control-Allow-Headers:*'); header('Access-Control-Allow-Credentials: true'); use app\common\controller\Api; use app\common\library\Ems; use app\common\library\Sms; use fast\Random; use think\Validate; use think\Db; use think\Config; use \app\common\model\User; use app\common\model\UserRule; use app\common\library\Token; use think\Exception; use think\Hook; use think\Request; /** * 會員接口 */ class Weixinlogin extends Api { public $appid=""; public $secret=""; protected $noNeedLogin = ['*']; protected $noNeedRight = '*'; public function index(){ if(isset($_GET)&&!empty($_GET)){ $this->weiixnhuidiao(); }else{ $this->weilogin(); } } public function weilogin(){ $appid= $this->appid; $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="; $url.=$appid; $url.="&redirect_uri="; // $url.="http://admin.weixuegu.cn/api/Weixinlogin"; $url.="http://".$_SERVER['SERVER_NAME']."/api/Weixinlogin"; $url.="&response_type=code"; $url.="&scope=snsapi_userinfo"; $url.="&state="."123"; $url.="#wechat_redirect"; header("location: ".$url) ; exit(); } public function weiixnhuidiao(){ //第二步 if(!isset($_SESSION["openid"]) || empty($_SESSION["openid"])){ $appid= $this->appid; $secret= $this->secret; $code = $_GET['code']; $get_token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='; $get_token_url.=$appid; $get_token_url.='&secret='; $get_token_url.=$secret; $get_token_url.='&code='; $get_token_url.=$code; $get_token_url.='&grant_type=authorization_code'; $curl = curl_init(); // 啟動一個CURL會話 curl_setopt($curl, CURLOPT_URL, $get_token_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// 跳過證書檢查 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); if($res === FALSE ){ echo "CURL Error:".curl_error($curl); } $json_obj=json_decode($res,true); // var_dump($json_obj);die; $_SESSION["access_token"]=$json_obj['access_token']; $_SESSION["openid"]=$json_obj['openid']; } //第四步 $access_token=$_SESSION['access_token']; $openid=$_SESSION["openid"]; $get_user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $curl1 = curl_init(); // 啟動一個CURL會話 curl_setopt($curl1, CURLOPT_URL, $get_user_info_url); curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);// 跳過證書檢查 curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false); $res1 = curl_exec($curl1); //返回api的json對象 //關閉URL請求 if($res1 === FALSE ){ echo "CURL Error:".curl_error($curl1); } ////解析json $user_obj=json_decode($res1,true); $data=$user_obj; $url =" http://swa.weixuegu.cn"; $i=0; foreach ($data as $key=>$value) { if($i==0&&!is_array($value)){ $i++; $url.="?".$key."=".urldecode($value); }else if(!is_array($value)){ $i++; $url.="&".$key."=".urldecode($value); } } echo " <script>"; echo ' window.open("'.$url.'");'; echo " </script>"; echo ' <script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script> <script> // init vConsole var vConsole = new VConsole(); console.log(res); </script> '; //die; } public function login($openid,$password=""){ //username mobile $user = User::get(['weixinopenid' => $openid]); if (!$user) { $this->error("沒有賬戶"); } if ($user['status'] == 'hidden') { $this->error("用戶被禁用"); } if ($user) { $ret = $this->auth->weixinopenid_login($user->username); if ($ret) { $data = ['userinfo' => $this->auth->getUserinfo()]; $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } }else { $this->error("失敗2"); } } /** * 設置錯誤信息 * * @param $error 錯誤信息 * @return Auth */ public function setError($error) { $this->_error = $error; return $this; } public function Binding_user(){ //username mobile $mobile = $this->request->request("mobile"); $password = $this->request->request("password"); $openid = $this->request->request("openid"); $headimgurl = $this->request->request("headimgurl"); if (!$password ) { $this->error(__('密碼不能為空')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('賬戶格式錯誤')); } $user = \app\common\model\User::getByMobile($mobile); if (!$user) { // 檢測用戶名或郵箱、手機號是否存在 $this->error(__('你還沒有賬號哦,請在pc端注冊并綁定微信')); } if ($user->password != md5(md5($password) . $user->salt)) { $this->error(__('密碼錯誤')); } $user->weixinopenid = $openid; $user->avatar = $headimgurl; $user->save(); // $ret = $this->auth->login($mobile, $password); $this->login($openid); //$this->success(__('Logged in successful'), $ret); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看