作用:过去公众号登录code,通过code来换取access_token进一步获得用户的微信个人信息

操作流程:

  • 1.登录https://mp.weixin.qq.com/
  • 2.左侧设置与开发-功能设置-js接口安全域名
  • 3.将你的域名填入,同时下载微信的.txt文件
  • 4.将下载的文件放入public目录下(tp5为例)

 

public function getCode()
{
//重定向地址,域名填写你在js接口安全域名处填写的域名,api/Member/index为模块/控制器名/方法名(tp5为例),访问另一个方法index
$redirect_uri = urlencode(https://123456.com/api/member/index’);
//scope为snsapi_base只能获取用户的openid
//scope为snsapi_userinfo 可以获取用户的基本信息(需要获得用户授权)
$url = https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8e7e57419b37e8de&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect”;
header(Location:” . $url);
}
//在微信的页面(微信开发者工具/或者微信聊天框点击链接–访问getcode方法)获得code
public function index()
{
dump(request()->param());
}

在微信聊天框/微信开发者工具访问getCode方法,就可以看到code