1申请微信测试号
申请微信测试号地址
http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
2配置微信测试号
配置需要用到公网地址 没有的 可以下载内网穿透工具
内网下载方式以及代理:https://natapp.cn/
这里用的是natapp 具体用法---Google、百度
![1](https://i-blog.csdnimg.cn/blog_migrate/bff41590ea0881f51bf877273840e9bf.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a052d2f4a307c5b4e3e8cce402714967.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0c851c4caa08a27628364d4330b7b37e.png)
3前端:
vue :
$('.list-box').on('click', '#goBind', function() {
var APPID = $('#wx_app_id').val();
var REDIRECT_URI = $('#wx_url').val();
var code = 'code';
var SCOPE = 'snsapi_userinfo';
var STATE = '123';
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + REDIRECT_URI + '&response_type=' + code + '&scope=' + SCOPE + '&state=' + STATE + '#wechat_redirect';
window.location.href = url;
});
4后端:
PHP:
public function indexAction()
{
$wx_code = $this->getRequest()->getQuery('code', '');
$state = $this->getRequest()->getQuery('state', '');
if($wx_code && $state == '123' )
{
$oauth = new WeChatAuth();
$wx_info = $oauth->getUserOpentId($wx_code);
$openid = $wx_info['openid'];
$accountOauth = new accountOauthModel();
$check_member = $accountOauth->findUserInfo($openid);
}
}
class WeChatAuth
{
private $appid = '';
private $appsecret = '';
public function __construct()
{
$globalConfig = \Yaf\Registry::get('globalConfig');
$config = $globalConfig['weixinpay_config'];
$this->appid = $config["app_id"];
$this->appsecret = $config["app_secret"];
}
public function getUserOpentId($code = ''){
if(!$code) return [];
$appid = $this->appid;
$secret = $this->appsecret;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$res = $this->http_curl($url);
$access_token = $res['access_token'];
$getopenid = $res['openid'];
$urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$getopenid."&lang=zh_CN";
$resinfos = $this->http_curl($urltoc);
return $resinfos;
}
public function http_curl($url,$type='get',$res='json',$arr='')
{
$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);
if ($type == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
$output = curl_exec($ch);
curl_close($ch);
if ($res == 'json') {
return json_decode($output, true);
}
}
}