1、安装easywechat
composer require "overtrue/laravel-wechat:^6.0"
2、生成配置文件
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
3、在config目录找到wechat.php完成配置信息(主要official_account)
4、添加路由
Route::any(‘wechat‘, [App\Http\Controllers\WeChatController::class, ‘serve‘]);
5、创建控制器
<?php
namespace App\Http\Controllers;
use Log;
class WeChatController extends Controller
{
/**
* 处理微信的请求消息
*
* @return string
*/
public function serve()
{
Log::info(‘request arrived.‘); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志
$app = app(‘wechat.official_account‘);
$app->server->push(function($message){
return "欢迎关注 overtrue!";
});
return $app->server->serve();
}
}
6、在middleware的Verify里面添加 wechat 路由,取消csrf限制
protected $except = [
‘wechat‘
];
7、获取二维码
测试:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
在config下wechat.php修改返回数据格式
‘response_type‘ => ‘object‘
定义路由
Route::any(‘qrcode‘, [App\Http\Controllers\WeChatController::class, ‘qrcode‘]);
定义控制器
public function qrcode()
{
$app = app(‘wechat.official_account‘);
$qrcode = $app->qrcode;
$chang = Str::uuid()->toString()
$result = $qrcode->temporary($chang,86400);//第一是场景值,第二是有效时间
$url = $qrcode->url($result->ticket);
return response()->json([
‘scen_value‘ => $chang,
‘url‘ => $url,
‘status‘ => ‘success‘
])
}
8、获取微信返回的数据(用户扫码之后)
数据在serve控制器里面能获取到具体信息
public function serve()
{
$app = app(‘wechat.official_account‘);
$app->server->push(function($message){
switch ($message->MsgType){
case ‘event‘:
return $this->eventHandler();
default:
return ‘‘;
}
});
return $app->server->serve();
}
public function eventHandler()
{
switch($this->message->Event){
case ‘subscribe‘:
$eventKey = $this->message->EventKey;
//判断$eventKey是否qrscene开头
if(Str::startsWidth($eventkey,‘qrscene‘)){
//提取qrscene_后面的值
$sceneValue = Str::after($eventKey,‘qrscene_‘);
//存进缓存系统,键,值,时间(默认一小时)
cache()-put(‘scene1‘,$sceneValue,now()->addHour());
//取数据是:cache()->get(‘scene1‘);
}
return ‘欢迎关注‘;
case ‘SCAN‘:
$eventKey = $this->message->EventKey;
cache()-put(‘scene2‘,$eventKey,now()->addHour());
return ‘扫码时间‘;
default :
return ‘‘;
}
}
9、存取用户数据
public function eventHandler()
{
switch($this->message->Event){
case ‘subscribe‘:
$openId = $this->message->FromUserName;
$outh = $this->app->user->get($openId);//可以查看里面数据,方便其他使用
$user = User::firstOrCreate([
//查找User表open_id是否存在$openId,存在就返回
‘open_id‘:$openId
],[
//不存在就进行插入
‘name‘ : $outh->nickname,
‘open_id‘ : $openId //这个可以不写,自动会插入
]);
$eventKey = $this->message->EventKey;
//判断$eventKey是否qrscene开头
if(Str::startsWidth($eventkey,‘qrscene‘)){
//提取qrscene_后面的值
$sceneValue = Str::after($eventKey,‘qrscene_‘);
//存进缓存系统,键,值,时间(默认一小时)
cache()-put(‘scene1‘,$sceneValue,now()->addHour());
//取数据是:cache()->get(‘scene1‘);
}
return ‘欢迎关注‘;
case ‘SCAN‘:
default :
return ‘‘;
}
}
10、在获取完二维码后,可以每3秒钟查询一次结果
this.i_timer = setInterVal(()=>{
axios.get(‘{{route(‘scan.check‘)}}‘,{
params:{
‘scene_value‘ : $this.scene_value
}
}).then(res=>{
let result = res.data;
if(result.status === ‘success‘){
window.location.href = ‘‘;
}
})
},3000)
11、定义查询结果接口
路由
Route::any(‘scan‘, [App\Http\Controllers\WeChatController::class, ‘checkScan‘])->name(‘scan.check‘);
控制器
public function checkScan(REQURST $request)
{
$scene_value = $request->get(‘scene_value‘);
//若是接收不到$scene_value,或者缓存中没有$scene_value就返回fail
if(!$scene_value || (!$user = cache()->get($scene_value) )){
return response()->json([
‘status‘ : ‘fail‘
])
}
//登录成功
Auth::login($user,true);
//删除缓存
cache()->forget($scene_value);
return response()->json([
‘status‘ : ‘success‘
])
}