微信公众平台设置
1.【基本设置】回调url路径
2.公众号基础设置
3.代码体现
public function wxCallback(Request $request)
{
$app = app(‘wechat.official_account.default’);
$accessToken = $app->access_token->getToken()[‘access_token’];
// //设置菜单
$menuList = a p p − > m e n u − > c u r r e n t ( ) ; / / L o g : : i n f o ( ′ 微信生成菜单拉取结果 : ′ . j s o n e n c o d e ( app->menu->current(); // Log::info('微信生成菜单拉取结果:' . json_encode( app−>menu−>current();//Log::info(′微信生成菜单拉取结果:′.jsonencode(menuList));
$buttons = t h i s − > g e t B u t t o n s ( ) ; / / L o g : : i n f o ( ′ 微信生成菜单自定义数据 : ′ . j s o n e n c o d e ( this->getButtons(); // Log::info('微信生成菜单自定义数据:' . json_encode( this−>getButtons();//Log::info(′微信生成菜单自定义数据:′.jsonencode(buttons));
$res = a p p − > m e n u − > c r e a t e ( app->menu->create( app−>menu−>create(buttons);
// Log::info(‘微信生成菜单结果:’ . json_encode($res));
//微信添加接口信息校验if ($request->isMethod('get')) {$response = $app->server->serve();$response->send();return $response;}//接收事件推送if ($request->isMethod('post')) {$message = $app->server->getMessage();$isCallApplet = false;//扫码事件if ($message['MsgType'] == 'event' && $message['Event'] == 'SCAN' && !empty($message['EventKey'])) {if (substr($message['EventKey'],0,10) == 'customerId'){//绑定功能(new WeChatLogic())->bind($message['FromUserName'],explode('_',$message['EventKey'])[1],1,3);}else{$isCallApplet = $this->doRecordLogin($message);}}//关注事件if ($message['MsgType'] == 'event' && $message['Event'] == 'subscribe') {if (!empty($message['EventKey'])) {$message['EventKey'] = ltrim($message['EventKey'], 'qrscene_');if (substr($message['EventKey'],0,9) == 'ustomerId'){(new WeChatLogic())->bind($message['FromUserName'],explode('_',$message['EventKey'])[1],1,3);}else{$isCallApplet = $this->doRecordLogin($message);}} else {//首先发一条关注信息$sendMsg = 'hi终于等到你!e测试公众号粉丝扫描海报中的抽奖二维码参与抽奖,即可获取专属现金红包、精品课程、干货礼包
随时随地轻松做测试,体验一站式科研服务’;
s e n d M s g . = ′ < a d a t a − m i n i p r o g r a m − a p p i d = " ′ . e n v ( ′ E C E S H I W E C H A T M I N I P R O G R A M A P P I D ′ ) . ′ " d a t a − m i n i p r o g r a m − p a t h = " p a g e s / i n d e x / i n d e x ? s c e n e C o d e = ′ . sendMsg .= '<a data-miniprogram-appid="'.env('ECESHI_WECHAT_MINI_PROGRAM_APPID').'" data-miniprogram-path="pages/index/index?sceneCode='. sendMsg.=′<adata−miniprogram−appid="′.env(′ECESHIWECHATMINIPROGRAMAPPID′).′"data−miniprogram−path="pages/index/index?sceneCode=′.message[‘EventKey’].‘">点击跳小程序’;
$sendMsg .= ‘新注册账户更有首样免单200元优惠券待你领取!’;
a p p − > c u s t o m e r s e r v i c e − > m e s s a g e ( n e w T e x t ( app->customer_service->message(new Text( app−>customerservice−>message(newText(sendMsg))->to($message[‘FromUserName’])->send();
}
//发一条图片$mediaId = Redis::get(RedisEnum::WX_MEDIA_ID . WechatEnum::WX_REPLAY_SUBSCRIBE_PIC_URL);if (!$mediaId) {$mediaId = $app->material->uploadImage(base_path(WechatEnum::WX_REPLAY_SUBSCRIBE_PIC_URL))['media_id'];Redis::set(RedisEnum::WX_MEDIA_ID . WechatEnum::WX_REPLAY_SUBSCRIBE_PIC_URL, $mediaId);}$app->customer_service->message(new Image($mediaId))->to($message['FromUserName'])->send();}if ($message['MsgType'] == 'event' && $message['Event'] == 'unsubscribe') {(new WeChatLogic())->bind($message['FromUserName'],0,0,1);}//是否调起小程序if (true == $isCallApplet) {//临时素材media_id$upThumbRes = $app->media->uploadImage(base_path("/public/wechat/images/applet_thumb.png"));(new Wechat())->sendMsg($message, $upThumbRes['media_id'], $message['EventKey'], $accessToken);dispatch((new ShenCeEventJob(['distinctId'=> $message['FromUserName'],'productName'=> 'web','eventName'=>ShenceEnum::REGISTER_WX_SCAN,'func'=>'commonTrack',])));}//微信信息自动回复if ($message['MsgType'] == 'text' && !empty($message['Content'])) {$this->wechatReply($app, $message);}//微信模板推送回调事件if ($message['MsgType'] == 'event' && $message['Event'] == 'TEMPLATESENDJOBFINISH') {(new LatentOrderSniffLogic())->wechatCallback($message);}}return $app->server->serve();
}