前端方法
function GetRequest()
? {
? ? ? var url = location.search;? //獲取url中"?"符后的字串?
? ? ? var theRequest = new Object();
? ? ? if (url.indexOf("?") != -1)
? ? ? {
? ? ? ? ? var str = url.substr(1);
? ? ? ? ? strs = str.split("&");
? ? ? ? ? for (var i = 0; i < strs.length; i++)
? ? ? ? ? {
? ? ? ? ? ? ? theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
? ? ? ? ? }
? ? ? }
? ? ? return theRequest;
? }
? if (!GetRequest().call) {
? $.ajax({
? ? ? url: 'http://api.lejia.com/api/test',
? ? ? type: "GET",
? ? ? success: function (data) {
? ? ? ? console.log(data)
? ? ? ? window.location.href = data;
? ? ? },
? ? ? error: function(xhr){
? ? ? ? console.log(xhr)
? ? ? }
? ? ? ? ? ? });
? }else{
? eval(GetRequest().call);
? }
? function callback(){
? $.ajax({
? ? ? url: 'http://api.lejia.com/api/userList',
? ? ? type: "GET",
? ? ? success: function (data) {
? ? ? ? console.log(JSON.parse(data))
? ? ? },
? ? ? error: function(xhr){
? ? ? ? console.log(xhr)
? ? ? }
? ? ? ? ? ? });
? return false;
? }
?
index控制器
<?php
namespace app\api\controller;
use app\BaseController;
use lib\Character;
use think\facade\Db;
use think\Request;
use \Firebase\JWT\JWT;
use app\repository\WxService;
class Index
{
? ? public function test(request $request){
? ? ? ? $code = $request->param();
? ? ? ? error_reporting(1);
? ? ? ? header('Content-type:text/html; Charset=utf-8');
? ? ? ? $appid? = 'wxd4bcd8b1ab028596';
? ? ? ? $appKey = '7db30a7bd9750e3d4c1be405ea37b919';
? ? ? ? $wxPay? = new WxService($appid, $appKey);
? ? ? ? $data? = $wxPay->GetOpenid();
? ? ? ? redis()->hSet('wechat', 'data', json_encode($data, true));
? ? ? ? if (array_key_exists('code', $code)) {
? ? ? ? ? ? // 獲取openid
? ? ? ? ? ? $wechatInfo = redis()->hGet('wechat', 'data');
? ? ? ? ? ? $wechatInfo = json_decode($wechatInfo);
? ? ? ? ? ? // 獲取用戶信息
? ? ? ? ? ? $user = $wxPay->getUserInfo($data['openid'], $data['access_token']);
? ? ? ? ? ? redis()->hSet('wechat', 'user', json_encode($user, true));
? ? ? ? ? ? // 獲取回調(diào)域名
? ? ? ? ? ? $url = redis()->hGet('wechat', 'callback');
? ? ? ? ? ? redis()->hSet('wechat', 'Juser', $res);
? ? ? ? ? ? $url = $url.'?code='.$code['code'].'&state='.$code['state']."&call=callback()";
? ? ? ? ? ? return "<script>window.location.href = '{$url}'</script>";
? ? ? ? ? ? exit;
? ? ? ? }else{
? ? ? ? ? ? $url = getallheaders()['Referer'];
? ? ? ? ? ? redis()->hSet('wechat', 'callback', $url);
? ? ? ? ? ? redis()->hSet('wechat', 'url', json_encode($data, true));
? ? ? ? ? ? return json($data);
? ? ? ? }
? ? }
}
curl
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
namespace app\repository;
use think\facade\Db;
class WxService
{
? ? protected $appid;
? ? protected $appKey;
? ? public $data = null;
? ? public function __construct($appid, $appKey)
? ? {
? ? ? ? $this->appid? = $appid; //微信支付申請對應的公眾號的APPID
? ? ? ? $this->appKey = $appKey; //微信支付申請對應的公眾號的APP Key
? ? }
? ? /**
? ? * 通過跳轉(zhuǎn)獲取用戶的openid,跳轉(zhuǎn)流程如下:
? ? * 1、設置自己需要調(diào)回的url及其其他參數(shù),跳轉(zhuǎn)到微信服務器https://open.weixin.qq.com/connect/oauth2/authorize
? ? * 2、微信服務處理完成之后會跳轉(zhuǎn)回用戶redirect_uri地址,此時會帶上一些參數(shù),如:code
? ? *
? ? * @return 用戶的openid
? ? */
? ? public function GetOpenid()
? ? {
? ? ? ? //通過code獲得openid
? ? ? ? if (!isset($_GET['code'])) {
? ? ? ? ? ? //觸發(fā)微信返回code碼
? ? ? ? ? ? $baseUrl = $this->getCurrentUrl();
? ? ? ? ? ? $url? ? = $this->__CreateOauthUrlForCode($baseUrl);
? ? ? ? ? ? return $url;
? ? ? ? ? ? exit();
? ? ? ? } else {
? ? ? ? ? ? //獲取code碼,以獲取openid
? ? ? ? ? ? $code? = $_GET['code'];
? ? ? ? ? ? $openid = $this->getOpenidFromMp($code);
? ? ? ? ? ? return $openid;
? ? ? ? }
? ? }
? ? public function getCurrentUrl()
? ? {
? ? ? ? $scheme = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
? ? ? ? $uri? ? = $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'];
? ? ? ? if ($_SERVER['REQUEST_URI']) {
? ? ? ? ? ? $uri = $_SERVER['REQUEST_URI'];
? ? ? ? }
? ? ? ? $baseUrl = urlencode($scheme . $_SERVER['HTTP_HOST'] . $uri);
? ? ? ? return $baseUrl;
? ? }
? ? /**
? ? * 通過code從工作平臺獲取openid機器access_token
? ? * @param string $code 微信跳轉(zhuǎn)回來帶上的code
? ? * @return openid
? ? */
? ? public function GetOpenidFromMp($code)
? ? {
? ? ? ? $url? ? ? ? = $this->__CreateOauthUrlForOpenid($code);
? ? ? ? $res? ? ? ? = self::curlGet($url);
? ? ? ? $data? ? ? = json_decode($res, true);
? ? ? ? $this->data = $data;
? ? ? ? return $data;
? ? }
? ? /**
? ? * 構(gòu)造獲取open和access_toke的url地址
? ? * @param string $code,微信跳轉(zhuǎn)帶回的code
? ? * @return 請求的url
? ? */
? ? private function __CreateOauthUrlForOpenid($code)
? ? {
? ? ? ? $urlObj["appid"]? ? ? = $this->appid;
? ? ? ? $urlObj["secret"]? ? = $this->appKey;
? ? ? ? $urlObj["code"]? ? ? = $code;
? ? ? ? $urlObj["grant_type"] = "authorization_code";
? ? ? ? $bizString? ? ? ? ? ? = $this->ToUrlParams($urlObj);
? ? ? ? return "https://api.weixin.qq.com/sns/oauth2/access_token?" . $bizString;
? ? }
? ? /**
? ? * 構(gòu)造獲取code的url連接
? ? * @param string $redirectUrl 微信服務器回跳的url,需要url編碼
? ? * @return 返回構(gòu)造好的url
? ? */
? ? private function __CreateOauthUrlForCode($redirectUrl)
? ? {
? ? ? ? $urlObj["appid"]? ? ? ? = $this->appid;
? ? ? ? $urlObj["redirect_uri"]? = "$redirectUrl";
? ? ? ? $urlObj["response_type"] = "code";
? ? ? ? $urlObj["scope"]? ? ? ? = "snsapi_userinfo";
? ? ? ? $urlObj["state"]? ? ? ? = "STATE";
? ? ? ? $bizString? ? ? ? ? ? ? = $this->ToUrlParams($urlObj);
? ? ? ? return "https://open.weixin.qq.com/connect/oauth2/authorize?" . $bizString;
? ? }
? ? /**
? ? * 拼接簽名字符串
? ? * @param array $urlObj
? ? * @return 返回已經(jīng)拼接好的字符串
? ? */
? ? private function ToUrlParams($urlObj)
? ? {
? ? ? ? $buff = "";
? ? ? ? foreach ($urlObj as $k => $v) {
? ? ? ? ? ? if ($k != "sign") {
? ? ? ? ? ? ? ? $buff .= $k . "=" . $v . "&";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? $buff = trim($buff, "&");
? ? ? ? return $buff;
? ? }
? ? /**
? ? * 獲取用戶信息
? ? * @param string $openid 調(diào)用【網(wǎng)頁授權(quán)獲取用戶信息】接口獲取到用戶在該公眾號下的Openid
? ? * @return string
? ? */
? ? public function getUserInfo($openid, $access_token)
? ? {
? ? ? ? $response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN');
? ? ? ? return json_decode($response, true);
? ? }
? ? public static function curlGet($url = '', $options = array())
? ? {
? ? ? ? $ch = curl_init($url);
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
? ? ? ? curl_setopt($ch, CURLOPT_TIMEOUT, 30);
? ? ? ? if (!empty($options)) {
? ? ? ? ? ? curl_setopt_array($ch, $options);
? ? ? ? }
? ? ? ? //https請求 不驗證證書和host
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
? ? ? ? $data = curl_exec($ch);
? ? ? ? curl_close($ch);
? ? ? ? return $data;
? ? }
? ? public static function 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, $postData);
? ? ? ? curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設置cURL允許執(zhí)行的最長秒數(shù)
? ? ? ? if (!empty($options)) {
? ? ? ? ? ? curl_setopt_array($ch, $options);
? ? ? ? }
? ? ? ? //https請求 不驗證證書和host
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
? ? ? ? $data = curl_exec($ch);
? ? ? ? curl_close($ch);
? ? ? ? return $data;
? ? }
}