古詩詞大全網 - 個性簽名 - 分離前後端,獲得微信網頁授權。

分離前後端,獲得微信網頁授權。

前端方法

函數GetRequest()

?{

?var url = location.search?//獲取“?”在url中。運算符後的字符串?

?var the request = new Object();

?if (url.indexOf("?) != -1)

?{

?var str = URL . substr(1);

?strs = str . split(& amp;);

?for(var I = 0;我& ltstrs .長度;i++)

?{

?請求,請求。split(" = "[0]]= unescape(strs[I])。split(" = "[1]);

?}

?}

?返回請求;

?}

如果(!GetRequest()。呼叫){

?$.ajax({

?URL:“/API/test”,

?類型:“獲取”,

?成功:函數(數據){

console.log(數據)

window . location . href = data;

?},

?錯誤:函數(xhr){

console.log(xhr)

?}

});

?}否則{

?eval(GetRequest()。打電話);

?}

函數回調(){

?$.ajax({

?URL:“/API/用戶列表”,

?類型:“獲取”,

?成功:函數(數據){

console.log(JSON.parse(data))

?},

?錯誤:函數(xhr){

console.log(xhr)

?}

});

?返回false

?}

索引控制器

& lt?服務器端編程語言(Professional Hypertext Preprocessor的縮寫)

命名空間app \ api \ controller

使用app \ BaseController

使用lib \ Character

使用think \ facade \ Db

使用思考\請求;

使用\ Firebase \ JWT \ JWT;

使用app \ repository \ WxService

類指數

{

公共功能測試(請求$request){

$ code = $ request-& gt;param();

報錯(1);

header(' Content-type:text/html;charset = utf-8 ');

$appid?= ' wxd 4 BCD 8 b 1ab 028596 ';

$ appKey = ' 7 db 30 a 7 BD 9750 E3 D4 c 1be 405 ea 37 b 919 ';

$wxPay?= new WxService($appid,$ appKey);

$data?= $ wx pay-& gt;get OpenID();

redis()-& gt;hSet('微信','數據',json_encode($data,true));

if (array_key_exists('code ',$code)) {

//獲取openid

$ wechatInfo = redis()-& gt;hGet('微信','數據');

$微信info = JSON _ decode($微信info);

//獲取用戶信息

$ user = $ wx pay-& gt;getUserInfo($data['openid'],$ data[' access _ token ']);

redis()-& gt;hSet('微信','用戶',json_encode($user,true));

//獲取回調域名

$ URL = redis()-& gt;hGet('微信','回撥');

redis()-& gt;hSet('微信',' Juser ',$ RES);

$url = $url。?代碼= '。$code['code']。& amp狀態= '。$code['state']。“& ampcall = callback()";

return " & lt腳本& gtwindow . location . href = ' { $ URL } ' & lt;/script >;

退出;

}否則{

$ URL = getall headers()[' Referer '];

redis()-& gt;hSet('微信','回撥',$ URL);

redis()-& gt;hSet('微信','網址',json_encode($data,true));

返回JSON($ data);

}

}

}

卷曲

函數geturl($url){

$ header array = 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,$ header array);

$ output = curl _ exec($ ch);

curl _ close($ ch);

$output = json_decode($output,true);

返回$ output

}

壹種獲取微信信息方法

& lt?服務器端編程語言(Professional Hypertext Preprocessor的縮寫)

命名空間app \ repository

使用think \ facade \ Db

WxService類

{

受保護的$ appid

受保護的$ appKey

public $ data = null

公共函數_ _構造($appid,$appKey)

{

$ this-& gt;appid?= $ appid//微信支付應用對應的微信官方賬號的APPID。

$ this-& gt;appKey = $ appKey//微信支付應用對應的微信官方賬號的APP Key。

}

/**

*通過跳轉獲取用戶的openid。跳轉過程如下:

* 1.設置需要回撥的網址等參數,跳轉到微信服務器/connect/oauth2/authorize。

* 2.微信服務處理後會跳回用戶的redirect_uri地址,此時會帶壹些參數,比如代碼。

*

* @返回用戶的openid。

*/

公共函數GetOpenid()

{

//通過代碼獲取openid

如果(!isset($ _ GET[' code ']){

//觸發微信返回代碼code。

$ base URL = $ this-& gt;getCurrentUrl();

$ URL = $ this-& gt;_ _ CreateOauthUrlForCode($ base URL);

返回$ url

exit();

}否則{

//獲取代碼code獲取openid。

$code?= $ _ GET[' code '];

$ OpenID = $ this-& gt;getopenidfromp($ code);

返回$ openid

}

}

公共函數getCurrentUrl()

{

$ scheme = $ _ SERVER[' HTTPS ']= ' on '?/sns/oauth2/access_token?" .$ bizString

}

/**

*構造url連接以獲取代碼。

* @param string $redirectUrl微信服務器,Url編碼必輸。

* @return返回構造的url。

*/

私有函數_ _ CreateOauthUrlForCode($ redirectUrl)

{

$ URL obj[" appid "]= $ this-& gt;appid

$urlObj["redirect_uri"]?= " $ redirectUrl

$ URL obj[" response _ type "]= " code ";

$ URL obj[" scope "]= " snsapi _ userinfo ";

$ URL obj[" STATE "]= " STATE ";

$bizString?= $ this-& gt;ToUrlParams($ URL obj);

return“/connect/oauth 2/authorize?”。$ bizString

}

/**

*縫合簽名字符串

* @ param array$URL obj

* @return返回拼接後的字符串。

*/

私有函數ToUrlParams($urlObj)

{

$ buff =

foreach($ URL obj as $ k = & gt;$v) {

如果($k!= "sign") {

$buff。= $k . "= "。$ v。" & amp;;

}

}

$buff = trim($buff," & amp);

返回$ buff

}

/**

*獲取用戶信息

* @param string $openid調用網頁授權訪問用戶信息接口獲取用戶在微信官方賬號下的openid。

* @返回字符串

*/

公共函數getUserInfo($openid,$access_token)

{

$ response = self::curl get('/SNS/userinfo?access_token= '。$access_token。& ampopenid= '。$openid。& amplang = zh _ CN’);

返回json_decode($response,true);

}

公共靜態函數curlGet($url = ' ',$options = array())

{

$ ch = curl _ init($ URL);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_TIMEOUT,30);

如果(!empty($options)) {

curl_setopt_array($ch,$ options);

}

//https請求不驗證證書和主機。

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

$ data = curl _ exec($ ch);

curl _ close($ ch);

返回$ data

}

公共靜態函數curlPost($url = ' ',$postData = ' ',$options = array())

{

if (is_array($postData)) {

$ postData = http _ build _ query($ postData);

}

$ ch = curl _ init();

curl_setopt($ch,CURLOPT_URL,$ URL);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_POST,1);

curl_setopt($ch,CURLOPT_POSTFIELDS,$ post data);

curl_setopt($ch,CURLOPT_TIMEOUT,30);//設置允許cURL執行的最大秒數。

如果(!empty($options)) {

curl_setopt_array($ch,$ options);

}

//https請求不驗證證書和主機。

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

$ data = curl _ exec($ ch);

curl _ close($ ch);

返回$ data

}

}