公众号授权第三方平台

EasyWeChat SDK

获取授权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);

公众号授权成功以后会根据第三方平台配置的链接每十分钟访问一次,并发送票据,代公众号实现业务用。
验证票据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值