一、准备微信开发平台账号(需要认证¥300)

二、看代码(注意:其中有自己建的数据表)

1、EasyWeChat 3.0 微信开发平台第三方平台接入

<?php
namespace App\Http\Controllers;use Illuminate\Http\Request;
use EasyWeChat\Foundation\Application;
use Illuminate\Support\Facades\Redis;
use DB;
use App\Models\Wechat\WechatTicket;
use App\Models\Account;class OpenWeixinController extends Controller
{protected $options = ['open_platform' => ['app_id'   => 'xx','secret'   => 'xx','token'    => 'xxx','aes_key'  => 'xxx'],];public function index(Request $request) {$this->request = $request;try{$app = new Application($this->options);$openPlatform = $app->open_platform; // 自定义处理$server = $openPlatform->server;$server->setMessageHandler(function($event) use($openPlatform){switch ($event->InfoType) {//授权成功case 'authorized':                $res = $openPlatform->getAuthorizationInfo($event->AuthorizationCode);$appid2 = $res->authorization_info['authorizer_appid'];$refresh_token = $res->authorization_info['authorizer_refresh_token'];$data = $openPlatform->getAuthorizerInfo($appid2);$account = Account::where('appid', $appid2)->first();$info2 = $data['authorizer_info'];$info1 = $data['authorization_info'];if(is_null($account)){$account = Account::create(['appid' => $info1['authorizer_appid'],'refresh_token' => $refresh_token,'name' => $info2['nick_name'],'head_img' => $info2['head_img'],'original' => $info2['user_name'],'com_main_body' => $info2['principal_name'],'headimg' => $info2['qrcode_url'], //二维码'status' => 1,'level' => $this->getLevel($info2),'type' => 0, 'token' => random(32),'encodingaeskey' => random(43)]);}else{$account->refresh_token = $refresh_token;$account->name =  $info2['nick_name'];$account->type = 0;$account->original = $info2['user_name'];$account->level = $this->getLevel($info2);$account->head_img = $info2['head_img'];$account->save();}break;//取消授权case 'unauthorized':$appid = $event->AuthorizerAppid;$account = Account::where('appid', $appid)->first();if(!is_null($account)){$account->refresh_token = '';$account->save();}//推送component_verify_ticket协议case 'component_verify_ticket':$appid = $event->AppId;         $ticket = $event->ComponentVerifyTicket;Redis::set($appid . '_component_verify_ticket',$ticket);//存储起来方便后续多台服务器并发调用$wechatTicket = WechatTicket::where('appid', $appid)->orderBy('id')->first();if(is_null($wechatTicket)){$wechatTicket = WechatTicket::create(['appid' => $appid,'ticket' => $ticket,'create_time' => time(),'update_time' => time()]);}else{$wechatTicket->ticket = $ticket;$wechatTicket->update_time = time();$wechatTicket->save();}}});              return $server->serve();}catch(\Exception $e){if(env('APP_DEBUG')){echo $e->getMessage();} else {abort(404);}}}public function auth(Request $request) {$app = new Application($this->options);$openPlatform = $app->open_platform;$response = $openPlatform->pre_auth->redirect('http://xxx//index');//return $response;// 获取跳转的链接$url = $response->getTargetUrl();echo '<a href="'.$url.'">点击第三方授权</a>';}}

2、EasyWeChat 4.0微信开发平台第三方平台接入

<?php
namespace App\Http\Controllers\news;use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use DB;use App\Models\Account;
use App\Models\Wechat\WechatTicket;
use EasyWeChat\Factory;
use EasyWeChat\OpenPlatform\Server\Guard;/*** 微信开发平台第三方平台接入* EasyWeChat 4.0*/
class OpenWeixinController extends Controller
{protected $account;protected $app;/*** Display a listing of the resource.** @return Response*/public function index(Request $request) {$this->request = $request;try{$openPlatform = Factory::openPlatform(config('wechat.open_platform.default'));// 第三方平台接入处理$server = $openPlatform->server;// 处理授权成功事件$server->push(function ($event) use($openPlatform){//获取(刷新)授权公众号或小程序的接口调用凭据(令牌)              $res = $openPlatform->handleAuthorize($event->AuthorizationCode);$appid2 = $res->authorization_info['authorizer_appid'];$refresh_token = $res->authorization_info['authorizer_refresh_token'];//获取授权方的帐号基本信息$data = $openPlatform->getAuthorizer($appid2);$account = Account::where('appid', $appid2)->first();$info2 = $data['authorizer_info'];$info1 = $data['authorization_info'];if(is_null($account)){$account = Account::create(['appid' => $info1['authorizer_appid'],'refresh_token' => $refresh_token,'name' => $info2['nick_name'],'head_img' => $info2['head_img'],'original' => $info2['user_name'],'com_main_body' => $info2['principal_name'],'headimg' => $info2['qrcode_url'], //二维码'status' => 1,'level' => $this->getLevel($info2),'type' => 0, 'token' => random(32),'encodingaeskey' => random(43)]);}else{$account->refresh_token = $refresh_token;$account->name =  $info2['nick_name'];$account->type = 0;$account->original = $info2['user_name'];$account->level = $this->getLevel($info2);$account->head_img = $info2['head_img'];$account->save();}}, Guard::EVENT_AUTHORIZED);// 处理授权更新事件$server->push(function ($event) use($openPlatform){}, Guard::EVENT_UPDATE_AUTHORIZED);// 处理授权取消事件$server->push(function ($event) {$appid = $event->AuthorizerAppid;$account = Account::where('appid', $appid)->first();if(!is_null($account)){$account->refresh_token = '';$account->save();}}, Guard::EVENT_UNAUTHORIZED);//VerifyTicket component_verify_ticket协议推送$server->push(function ($event) use($openPlatform){$appid = $event->AppId;         $ticket = $event->ComponentVerifyTicket;//保存component_verify_ticket协议Redis::set($appid . '_component_verify_ticket', $ticket);//存储起来方便后续多台服务器并发调用$wechatTicket = WechatTicket::where('appid', $appid)->orderBy('id')->first();if(is_null($wechatTicket)){$wechatTicket = WechatTicket::create(['appid' => $appid,'ticket' => $ticket,'create_time' => time(),'update_time' => time()]);}else{$wechatTicket->ticket = $ticket;$wechatTicket->update_time = time();$wechatTicket->save();}}, Guard::EVENT_COMPONENT_VERIFY_TICKET);return $server->serve();}catch(\Exception $e){if(env('APP_DEBUG')){echo $e->getMessage();} else {abort(404);}}}public function auth(Request $request) {try{      $openPlatform = Factory::openPlatform(config('wechat.open_platform.default'));// 获取跳转的链接$url = $openPlatform->getPreAuthorizationUrl('url');// 获取跳转的链接echo '<a href="'.$url.'">点击第三方授权</a>';}catch(\Exception $e){//echo $e->getMessage();$url = '';}//return view('admin.auth.note', compact('url'));}public function callback(Request $request) {$filter =  $request->all();//return redirect()->to(site_path('account/wechat', 'admin'))->with('message', '第三方接入成功!');return redirect()->to('url')->with('message', '第三方接入成功!');}// 公众号分组public function getLevel($info = array()){$level = 0;if(!empty($info['MiniProgramInfo'])){$level = 5;}else if($info['service_type_info']['id'] == 0 || $info['service_type_info']['id'] == 1){//订阅号if($info['verify_type_info']['id'] >= 0){//已认证$level = 3;}else{$level = 1;}}else if($info['service_type_info']['id'] == 2){//服务号if($info['verify_type_info']['id'] >= 0){//已认证$level = 4;}else{$level = 2;}}return $level;}}

调用auth函数进行第三方授权

3、自定义推送component_verify_ticket协议

EasyWeChat自动缓存了component_verify_ticket。

想在不同的服务器将进行操作,必须将component_verify_ticket保存到数据库(Redis,Mysql)。

看下面的代码:EasyWeChat4.0

            $openPlatform = Factory::openPlatform(config('wechat.open_platform.default'));  $verify_ticket = $openPlatform->verify_ticket;//重设第三方平台verify_ticket 方便多台机器用$app_id = $openPlatform->config->app_id;//Redis$ticketCache = Redis::get($app_id . '_component_verify_ticket');if(is_null($ticketCache) || empty($ticketCache)){//mysql$wechatTicket = WechatTicket::where('appid', $app_id)->orderBy('id')->first();if(!is_null($wechatTicket)){$ticketCache = $wechatTicket->ticket;}}if($ticketCache){$verify_ticket->setTicket($ticketCache);} //代公众号实现业务$app = $openPlatform->officialAccount($account->appid, $account->refresh_token);//代小程序实现业务//$app = $openPlatform->miniProgram($account->appid, $account->refresh_token);

三、问题

1、在授权URL测试的时候,如果报AesException: 签名验证错误,可能是代码你参数填写有错。一定要注意是填写第三方平台的信息,不是公众号的信息。

2、在授权URL测试的时候,如果报Illegal key size,那么就是指密钥长度是受限制的,java运行时环境读到的是受限的policy文件。去掉这种限制需要下载Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files(https://yunpan.cn/cSrtk3Fk6WgAs  访问密码 244a)。替换${java_home}/jre/lib/security/ 下面的local_policy.jar和US_export_policy.jar(jdk,jre最好都替换。)。注意一定要重启TOMCAT才能生效。

3、component ticket is exprie ticket过期,每10分钟会微信服务器会推送到授权事件的URL中。这里可以不用处理设置过期时间(因为这里的ticket是无接口调用权限的,而且是微信服务器自己推送的)

4、注意公众号消息处理的URL必须加上$APPID$

5、component_access_token过期时间为2个小时,和公众号的Token一样(我处理的是存储过期时间,超过就请求。而不是定时刷新)

6、可以选择你自己需要第三方托管的权限集,比如你其他消息只走微信公众号,只有客服消息走第三方平台。

7、异常:请确认授权入口页所在域名,与授权后回调页所在域名相同,并且,此两者都必须与申请第三方平台时填写的授权发起页域名相同。授权入口页所在域名:空

(改地址不能直接方法,需要<a href="授权地址"></a>标签进行跳转)

当配置正常,<a href="授权地址"></a>不能正常访问时,可以试试以下

第一种方式:<meta name="referrer" content="never">

第二种方式:<meta http-equiv="refresh" content="0; url=授权地址"> 重定向

8、有可能会出现你每次启动项目的时候,由于每10分钟才会验证一次Ticket,那么直接调用接口,如果服务器还没推送,会出现ticket失效(不过在正式环境还好)(就算全网通过后也会推送component_verify_ticket协议)。

9、每次授权后,想要获取最新的权限必须重新扫码授权。否则无法获取最新的消息。

10、获取令牌的官网接口

11、第三方中的授权的URL一级接收公众号消息的URL都只能在同一个一级域名下

12、如果是并发量大,做集群的话,一定要把所有服务器的ip都加到白名单中

EasyWeChat微信开发平台第三方接入(Laravel5+,EasyWeChat3.0/EasyWeChat4.0)相关推荐

  1. EasyWeChat微信开放平台第三方平台接入流程

    授权流程技术说明: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Authorization_Process ...

  2. EasyWeChat微信开放平台第三方平台接入

    目录 实例化 微信服务器推送事件 预授权 获取预授权 Code 获取预授权 URL API 列表 使用授权码换取公众号的接口调用凭据和授权信息 获取授权方的公众号帐号基本信息 获取授权方的选项设置信息 ...

  3. 微信开发平台第三方平台86004无效微信号

    快速注册企业小程序功能在第三方平台 微信文档:快速注册企业小程序 | 微信开放文档 接口返回86004 无效微信号 解决办法: 1.先查看tocke是否能够正确获取到. 2.在去验证传值参数是否正确 ...

  4. 微信开放平台-第三方平台开发配置及常见的问题

    目录 概述 参考文档 开源项目&工具 第三方平台设置 问题及解决方法 概述 本实例:第三方平台+微信公众号(服务号) 微信开放平台-第三方平台,为广大公众号和小程序提供运营服务和行业解决方案. ...

  5. 微信公众平台-第三方平台开发(一)准备工作、开发配置

    平台概述 微信开放平台-第三方平台(简称第三方平台),由微信团队面向所有通过开发者资质认证的第三方开发者提供提供的官方平台. 在得到公众号或小程序管理员授权后,基于该平台,第三方服务商可以通过调用官方 ...

  6. 微信开放平台第三方接口集成

    介绍 在做微信开放平台第三方应用开发的时候,每次都要登录到微信开放平台中操作,如果多了的话,很不方便,所以想到集成到自己后台操作,这样就可以同时管理n个开放平台里面的n个第三方应用了. 犹豫代码涉及逻 ...

  7. API 开放接口设计之 appId,appSecret,accessToken (同微信开发平台接口)

    前篇:如何设计开放 Api 以下链接来源于网络素材: 需要考虑点摘录一: https://blog.csdn.net/weixin_34414196/article/details/92105613 ...

  8. 微信开放平台 第三方平台获取推送的component_verify_ticket php

    在thinkphp6中调用 php7.4 下载微信开发文档里的sdk代码 php版本 但里面有些函数已经弃用 需要修改些(sdk有些地方能与开发环境等稍有出入 需要适当修改 兼容 查看另一篇文档 修改 ...

  9. 使用.net 操作 微信公众平台 —— 第三方登录

    目录 1. 使用.net 操作 微信公众平台 -- 接入 2. 使用.net 操作 微信公众平台 -- 生成微信菜单 3. 使用.net 操作 微信公众平台 -- 接收并回复用户消息 4. 使用.ne ...

最新文章

  1. ASP.NET中的加密方法介绍
  2. 服务发现存储仓库 etcd 使用简介
  3. 【HDU 1269】迷宫城堡 (Tarjan算法)
  4. maven不能拉取私服的原因
  5. 我在富士康挨踢了七年(八.出国Support)
  6. Java代码发送POST请求
  7. 各种乐器与人声的频率特性说明
  8. 五月职场胜如火 Java讲座陪您过
  9. android模拟机型,(安卓)牛X分身 — 支持位置模拟机型修改
  10. exchange创建邮箱组_exchange2010批量添加用户并创建邮箱并加入部门并添加到通讯组...
  11. 传输线阻抗方程的推导
  12. 嵌入式学习之QT学习----3 制作简单的QT界面(如:QQ登录界面)
  13. 2018.05.11 种花小游戏
  14. 丰巢取快递系统(一)
  15. Python练习猜拳,利用while循环自定义函数,结果数据存入excel表格
  16. 无需注册登录NVIDIA官网下载CUDNN
  17. 作为Senior Recruiter想跟大家聊聊求职,希望对大家有所帮助~
  18. 操作系统----进程管理(C语言)
  19. python调用word_Python调用win32com.client.Dispatch('Word.Application')报错汗血宝马
  20. FEDORA UT4418开发板ANDROID环境搭建

热门文章

  1. linux shell的for循环语法是怎样的?
  2. r,w,a 与 r+, w+, a+的区别(转)
  3. uniapp onReachBottom不触发 原因
  4. chrome谷歌浏览器:您使用的是不受支持的命令行标记:--extensions-on-chrome-urls
  5. 写在“二更食堂”被关停之后
  6. TPMS方案 接收器 STM8L篇
  7. 【skLearn 回归模型】多项式回归 PolynomialFeatures
  8. 2022 主站及创作侧年度总结 - 相信未来、期待未来
  9. 数据结构课程设计之员工通讯录
  10. 跳槽前恶补面试题,成功上岸阿里,拿到33k的测开offer