技术饭
微信开放平台中第三方平台账号申请与配置
之前做微信的开发都是公司自己一个公众号获取用户信息、获取授权等操作,但是为了使用其他公众号来做这时候就必须开通微信的第三方平台,最近正好在做微信开放平台中的第三方平台,在申请的过程中也踩了不少的坑。
微信开放平台:https://open.weixin.qq.com/
一、创建第三方平台
二、填写资料
三、选择相应的权限
四、配置开发环境,这部最为关键
五、等待审核,第一次审核可能比较慢,第一次审核通过之后,后续审核就秒过了
六、授权url与事件接收的方法,接口类api请自行开发
/**
* [sysmessage 授权事件接收URL]
*/
public function sysmessage() {
//实例化第三方平台类
$Thirdplatform = new \wechat\Thirdplatform($this->prefix);
//消息解密
$data = $Thirdplatform->decryptMsg();
if(!empty($data)){
switch ($data['InfoType']) {
//授权凭证
case 'component_verify_ticket':
$component_verify_ticket = $data['ComponentVerifyTicket'];
//票据写入数据库
$update_data = [];
$update_data['id'] = 1;
$update_data['ticket'] = $component_verify_ticket;
$update_data['update_time'] = date("Y-m-d H:i:s");
\Db::name('wechat_thirdplatform_ticket')->update($update_data);
//返回成功消息
echo 'success';
break;
//取消授权
case 'unauthorized':
//查询数据是否存在
if(isset($data['AppId']) && isset($data['AuthorizerAppid'])){
$where = [];
$where[] = ['appid', '=', $data['AppId']];
$where[] = ['authorizer_appid', '=', $data['AuthorizerAppid']];
$thirdplatform = \Db::name('wechat_thirdplatform')->field('id')->where($where)->find();
if(!empty($thirdplatform)){
//更新授权状态为-1
$update_data = [];
$update_data['id'] = $thirdplatform['id'];
$update_data['status'] = -1;
$update_data['update_time'] = date("Y-m-d H:i:s");
\Db::name('wechat_thirdplatform')->update($update_data);
}
}
break;
//授权
case 'authorized':
//无操作
break;
//更新授权
case 'updateauthorized':
//查询数据是否存在
if(isset($data['AppId']) && isset($data['AuthorizerAppid'])){
$where = [];
$where[] = ['appid', '=', $data['AppId']];
$where[] = ['authorizer_appid', '=', $data['AuthorizerAppid']];
$thirdplatform = \Db::name('wechat_thirdplatform')->field('id')->where($where)->find();
if(!empty($thirdplatform)){
//更新授权次数
$update_data = [];
$update_data['id'] = $thirdplatform['id'];
$update_data['auth_count'] = \Db::raw('auth_count+1');
$update_data['update_time'] = date("Y-m-d H:i:s");
\Db::name('wechat_thirdplatform')->update($update_data);
}
}
break;
}
}
}
/**
* [eventmsg 消息与事件接收URL]
*/
public function eventmsg() {
//获取参数
$params = $this->request->param();
if(!isset($params['appid']) || empty($params['appid'])){
exit();
}
//实例化第三方平台类
$Thirdplatform = new \wechat\Thirdplatform($this->prefix);
//消息解密
$data = $Thirdplatform->decryptMsg();
//处理消息事件
if(!empty($data)){
//第三方平台全网审核发布模拟测试普通消息
if($data['Content'] == 'TESTCOMPONENT_MSG_TYPE_TEXT'){
$Thirdplatform->response('TESTCOMPONENT_MSG_TYPE_TEXT_callback', $data);
}
//第三方平台全网审核发布模拟测试Api消息
if(preg_match('/QUERY_AUTH_CODE:/', $data['Content'])){
//获得模拟的auth_code
$query_auth_code = str_replace("QUERY_AUTH_CODE:", "", $data['Content']);
//通过code获取授权信息
$auth_code_info = $Thirdplatform->get_query_auth($query_auth_code);
//发送客服消息
$data['query_auth_code'] = $query_auth_code;
$res = $Thirdplatform->custom_send($auth_code_info['authorizer_access_token'], $data);
exit();
}
}
}


文明上网理性发言!