获取授权url
//获取第三方平台信息
$open = config("open");
//获取第三方平台操作对象
$openPlatform = Factory::openPlatform($open);
//组通知链接URL
$url = "http://www.baidu.com";
获取授权二维码链接
$url = $openPlatform->getPreAuthorizationUrl($url);
跳转到授权链接,使用微信扫描授权二维码
扫码之后选择需要授权的公众号并选择需要授权的权限。
处理授权回调
//获取授权码
$auth_code = $_POST["auth_code"];
//授权解密
$authorize = $openPlatform->hanleAuthorize($auth_code);
//获取授权账号信息
$authorizerData = $openPlatform->getAuthorizer($authorize["authorization_info"]["authorizer_appid"]);
//保存授权账号信息到数据库,丢失后需要重新获取
//组装数据
$data = [
"appid" => $this->handleAuthorize["authorization_info"]["authorizer_appid"],
"authorizer_refresh_token" => $this->handleAuthorize["authorization_info"]["authorizer_refresh_token"],
"verify_type" => $this->getAuthorizer["authorizer_info"]["verify_type_info"]["id"] >= 0 ? 1 : 0,
];
authorizer_appid和authorizer_refresh_token是重点,必须保存,代公众号发起授权、自定义菜单等所有功能都需要用到这两个参数。
取消授权
// 处理授权取消事件
$this->openPlatform->server->push(function ($message) {
//修改自己的数据库配置 可删除可修改,看个人怎么编写
$editConfig = Config->where()->delete();
#判断成功还是失败 并记录日志
if ($editConfig === false) {
file_put_contents("authorize_unbind_false.log", "appId:" . $message["AuthorizerAppid"] . "\r\n", FILE_APPEND);
} else {
file_put_contents("authorize_unbind_true.log", "appId:" . $message["AuthorizerAppid"] . "\r\n", FILE_APPEND);
}
}, Guard::EVENT_UNAUTHORIZED);
return $this->openPlatform->server->serve();
文章中提到的$openPlatform
//获取第三方平台配置信息
$open = config("open");
//获取第三方平台操作对象
$openPlatform = Factory::openPlatform($open);
公众号授权成功以后会根据第三方平台配置的链接每十分钟访问一次,并发送票据,代公众号实现业务用。