第一步:注册账号
首先注册微信开放平台账户并创建第三方平台 地址
配置公证号权限集
配置开发资料如下图 测试的话可以不用全网发布
第二步: 安装开发依赖包
我用的是EasyWeChat包, 安装 - 按照官网文档配置 官网地址
$ composer require overtrue/wechat:~4.0 -vvv
第三步:开发流程
1、首先启动票据推送服务 文档地址
2、获取验证票据 获取授权码 文档地址
提示:二维码链接必须在配置的域名网站下跳转,不然会限制跳转。
(包含PC端二维码如下图,移动端h5链接快速授权 移动H5链接)
移动端扫码授权界面
3、使用授权码获取授权信息 文档地址
4、自行处理第三方平台推送事件包含 (授权成功事件;授权更新事件;授权取消事件)并根据业务需求自行处理;
具体代码如下:
<?php
namespace App\Http\Repositories\SmallShop\Store;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;
use EasyWeChat\OpenPlatform\Server\Guard;
use App\Model\SmallShop\SmallPlatformAccount;
class SmallOpenPlatformRepository
{
protected $request;
protected $account;
protected $openPlatform;
/**
* @param Request $request
* @param SmallPlatformAccount $account
*/
public function __construct(Request $request, SmallPlatformAccount $account)
{
$this->request = $request;
$this->account = $account;
//$config = config('wechat.open_platform.default');
$config = [
'app_id' => 'wx5355*******',
'secret' => 'e212abd70b********',
'token' => 'wsa2qw86b86k********',
'aes_key' => 'pak2g7mwgz1stw6********',
];
$this->openPlatform = Factory::openPlatform($config);
//如果是多台服务器负载均衡 ticket缓存到redis
$predis = app('redis')->connection()->client(); //connection($name), $name 默认为 `default`
$cache = new RedisAdapter($predis);
$this->openPlatform->rebind('cache', $cache);
}
//启动票据推送服务
public function ticket()
{
$post_data = ['component_appid' => 'wx5355cd83e30f****', 'component_secret' => 'e212abd70b0288844db6a**********'];
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => json_encode($post_data),
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_start_push_ticket';
echo file_get_contents($url, false, $context);
}
//第三方平台推送事件
public function index()
{
// 第三方平台接入处理
$server = $this->openPlatform->server;
// 处理授权成功事件
$server->push(function ($message) {
//Log::channel('x_log')->info('微信公众平台授权成功事件' . json_encode($message) . '====');
//获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
$res = $this->openPlatform->handleAuthorize($message['AuthorizationCode']);
$appid = $res['authorization_info']['authorizer_appid'];
//获取授权方的帐号基本信息并记录授权的开放平台账户
$this->record_account($this->openPlatform->getAuthorizer($appid), $res);
}, Guard::EVENT_AUTHORIZED);
// 处理授权更新事件
$server->push(function ($message) {
$res = $this->openPlatform->handleAuthorize($message['AuthorizationCode']);
$appid = $res['authorization_info']['authorizer_appid'];
//获取授权方的帐号基本信息并记录授权的开放平台账户
$remarks = date('Y-m-d H:i:s') . ' 更新了公众号权限集';
$this->record_account($this->openPlatform->getAuthorizer($appid), $res, $remarks);
}, Guard::EVENT_UPDATE_AUTHORIZED);
// 处理授权取消事件
$server->push(function ($message) {
$data = ['status' => 2, 'cancel_at' => date('Y-m-d H:i:s')];
$this->update_account($message['AuthorizerAppid'], $data);
}, Guard::EVENT_UNAUTHORIZED);
return $server->serve();
}
//记录授权的公众号信息
public function record_account($data, $res, $remarks = '')
{
$info = $data['authorizer_info'];
$auth = $data['authorization_info'];
$where = ['appid' => $auth['authorizer_appid']];
$account = [
'appid' => $auth['authorizer_appid'],
'nick_name' => $info['nick_name'],
'head_img' => $info['head_img'],
'user_name' => $info['user_name'],
'principal_name' => $info['principal_name'],
'qrcode_url' => $info['qrcode_url'],
'service_type_info' => $info['service_type_info']['id'],
'verify_type_info' => $info['verify_type_info']['id'],
'refresh_token' => $auth['authorizer_refresh_token'],
'status' => 1,
'remarks' => $remarks,
'content' => json_encode($res)
];
return $this->account->updateOrCreate($where, $account);
}
//更新授权的公众号信息
public function update_account($appid, $data)
{
return $this->account->where('appid', $appid)->update($data);
}
//获取用户授权页 URL
public function get_url()
{
$type = $this->request->get('type');
if ($type) {
$url = $this->openPlatform->getPreAuthorizationUrl('http://' . $_SERVER['SERVER_NAME'] . '/agent/#/users');//PC端二维码授权
} else {
$url = $this->openPlatform->getMobilePreAuthorizationUrl('http://' . $_SERVER['SERVER_NAME'] . '/mall/#/invitation/index');//H5链接授权
}
return ["message" => "获取成功!", "data" => $url];
}
//根据授权码获取授权信息绑定商家
public function authorizer()
{
$code = request('auth_code');
//Log::channel('wx_third')->info('根据授权码绑定商家公众号' . $code);
if ($code) {
$info = $this->openPlatform->handleAuthorize($code);
$appid = $info['authorization_info']['authorizer_appid'];
$store_id = get_store_id();
$account = $this->account->where(['store_id' => $store_id])->first(['id', 'appid']);
if ($account && $account->appid != $appid) {
$this->account->where(['store_id' => $store_id])->update(['store_id' => null, 'remarks' => 'H5修改前store_id=' . $store_id]);
}
if ($this->update_account($appid, ['store_id' => $store_id])) {
/*if($this->check_menu($store_id)){
$this->smallWechatRepository->create_menu($appid, $store_id);
}*/
return ["message" => "公众号授权绑定小店成功!"];
}
Log::channel('wx_third_fail')->info('H5绑定小店失败 APPID:' . $appid . ' store_id:' . $store_id);
return ["message" => "绑定小店失败!", "code" => 202];
} else {
return ["message" => "参数错误auth_code!", "code" => 201];
}
}
//获取授权的公众号列表
public function lists()
{
$lists = $this->openPlatform->getAuthorizers();
return ["message" => "获取成功", "data" => $lists];
}
}
移动端扫码授权后 会跳转一个链接 后面默认给你拼了两个参数(一个是授权码另一个是有效时间)这两个参数可以获取公证号信息 文档地址
//根据授权码获取授权信息绑定商家
public function authorizer()
{
$code = request('auth_code');
//Log::channel('wx_third')->info('根据授权码绑定商家公众号' . $code);
if ($code) {
$info = $this->openPlatform->handleAuthorize($code);
$appid = $info['authorization_info']['authorizer_appid'];
$store_id = get_store_id();
$account = $this->account->where(['store_id' => $store_id])->first(['id', 'appid']);
if ($account && $account->appid != $appid) {
$this->account->where(['store_id' => $store_id])->update(['store_id' => null, 'remarks' => 'H5修改前store_id=' . $store_id]);
}
if ($this->update_account($appid, ['store_id' => $store_id])) {
/*if($this->check_menu($store_id)){
$this->smallWechatRepository->create_menu($appid, $store_id);
}*/
return ["message" => "公众号授权绑定小店成功!"];
}
Log::channel('wx_third_fail')->info('H5绑定小店失败 APPID:' . $appid . ' store_id:' . $store_id);
return ["message" => "绑定小店失败!", "code" => 202];
} else {
return ["message" => "参数错误auth_code!", "code" => 201];
}
}
目前只能在微信公众号后台取消授权
微信开放平台(第三方平台)代公众号发起网页授权