Commit 4a6d76ce authored by clone's avatar clone

微信公众号

parent 529c98fb
p9Ei8khm4IKSYIJa
\ No newline at end of file
<?php
namespace app\api\controller;
/**
* 微信jsSdk
* Created by zw.
* User: zw
* email 452436132@qq.com
* Date: 2018/06/04
* Time: 21:30
*/
use app\api\extend\Basic;
use app\api\untils\WxCallbackUntils;
class WxSdk extends Basic
{
private $wxCallbackApi;
public function __construct()
{
parent::__construct();
$this->wxCallbackApi = new WxCallbackUntils();
}
}
<?php
namespace app\api\untils;
/**
* 微信接口api
* Created by zw.
* User: zw
* email 452436132@qq.com
* Date: 2018/06/04
* Time: 21:30
*/
class WxCallbackUntils
{
private $redis;
private $appId;
private $appSecret;
public function __construct()
{
$this->appId = "wxbbeb8cd2f4b72aea";
$this->appSecret = "9ac6d1680553f4b554f3b46d263fbfb3";
}
/**
* 跳授权页
* @param $redirect_url "http://www.zhuwei.site/index.php/Home/Index/getList";
*/
public function getWxCode($redirect_url)
{
$redirect_url = urlencode($redirect_url);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appId . "&redirect_uri="
. $redirect_url . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
header("Location:" . $url);
}
/**
* 获取access_token
* @param $code
* @return mixed
*/
public function getAccessTokenByCode($code)
{
$oauth = 'https://api.weixin.qq.com/sns/oauth2/access_token';
$params['appid'] = $this->appId;
$params['secret'] = $this->appSecret;
$params['code'] = $code;
$params['grant_type'] = 'authorization_code';
$result = $this->curl($oauth, $params);
return $result;
}
/**
* 获取用户信息
* @param $code
* @return array
*/
public function getUserInfoByAccessToken($code)
{
$access_token_info = $this->getAccessTokenByCode($code);
$access_token_info = json_decode($access_token_info);
$access_token = $access_token_info->access_token;
$open_id = $access_token_info->openid;
session("access_token", $access_token);
session("openid",$open_id);
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo';
$params['access_token'] = $access_token;
$params['openid'] = $open_id;
$params['lang'] = 'zh_CN';
$user_info = $this->curl($user_info_url, $params);
$user_info = json_decode($user_info);
$data = array(
'wx_open_id' => $open_id,
'buyer_nick' => $user_info->nickname,
'sex' => $user_info->sex,
'province' => $user_info->province,
'city' => $user_info->city,
'buyer_img' => $user_info->headimgurl
);
session("userInfo",$data);
return $data;
}
public function curl($url, $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, []);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, null);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($ch);
return $content;
}
}
<?php
namespace app\index\controller;
use app\api\untils\WxCallbackUntils;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/6/4
* Time : 18:06
* Intro:
*/
class WxTest
{
private $url = "https://pre2.tonglianjituan.com/index/test";
private $_wxApi;
public function __construct()
{
$this->_wxApi = new WxCallbackUntils();
}
public function test()
{
$userInfo = session("userInfo");
dump($userInfo);
if (!$userInfo) {
$this->_wxApi->getWxCode($this->url);
} else {
return view("test");
}
}
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>11</title>
</head>
<body>
123123123
</body>
</html>
\ No newline at end of file
...@@ -235,6 +235,12 @@ Route::group('index', [ ...@@ -235,6 +235,12 @@ Route::group('index', [
'getTaxesById' => ['index/Finance/getTaxesById', [ 'method' => 'POST|GET' ] ], //财务结单 'getTaxesById' => ['index/Finance/getTaxesById', [ 'method' => 'POST|GET' ] ], //财务结单
'test' => ['index/WxTest/test', [ 'method' => 'POST|GET' ] ], //wx
]); ]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment